2860. Happy Students LeetCode Solution

In this guide, you will get 2860. Happy Students LeetCode Solution with the best time and space complexity. The solution to Happy Students problem is provided in various programming languages like C++, Java, and Python. This will be helpful for you if you are preparing for placements, hackathons, interviews, or practice purposes. The solutions provided here are very easy to follow and include detailed explanations.

Table of Contents

  1. Problem Statement
  2. Complexity Analysis
  3. Happy Students solution in C++
  4. Happy Students solution in Java
  5. Happy Students solution in Python
  6. Additional Resources
2860. Happy Students LeetCode Solution image

Problem Statement of Happy Students

You are given a 0-indexed integer array nums of length n where n is the total number of students in the class. The class teacher tries to select a group of students so that all the students remain happy.
The ith student will become happy if one of these two conditions is met:

The student is selected and the total number of selected students is strictly greater than nums[i].
The student is not selected and the total number of selected students is strictly less than nums[i].

Return the number of ways to select a group of students so that everyone remains happy.

Example 1:

Input: nums = [1,1]
Output: 2
Explanation:
The two possible ways are:
The class teacher selects no student.
The class teacher selects both students to form the group.
If the class teacher selects just one student to form a group then the both students will not be happy. Therefore, there are only two possible ways.

Example 2:

Input: nums = [6,0,3,3,6,7,2,7]
Output: 3
Explanation:
The three possible ways are:
The class teacher selects the student with index = 1 to form the group.
The class teacher selects the students with index = 1, 2, 3, 6 to form the group.
The class teacher selects all the students to form the group.

See also  2564. Substring XOR Queries LeetCode Solution

Constraints:

1 <= nums.length <= 105
0 <= nums[i] < nums.length

Complexity Analysis

  • Time Complexity: O(\texttt{sort})
  • Space Complexity: O(\texttt{sort})

2860. Happy Students LeetCode Solution in C++

class Solution {
 public:
  int countWays(vector<int>& nums) {
    nums.push_back(-1);
    nums.push_back(INT_MAX);
    ranges::sort(nums);

    int ans = 0;

    // i := the number of the selected numbers
    for (int i = 0; i + 1 < nums.size(); ++i)
      if (nums[i] < i && i < nums[i + 1])
        ++ans;

    return ans;
  }
};
/* code provided by PROGIEZ */

2860. Happy Students LeetCode Solution in Java

class Solution {
  public int countWays(List<Integer> nums) {
    nums.add(-1);
    nums.add(Integer.MAX_VALUE);
    Collections.sort(nums);

    int ans = 0;

    // i := the number of the selected numbers
    for (int i = 0; i + 1 < nums.size(); ++i)
      if (nums.get(i) < i && i < nums.get(i + 1))
        ++ans;

    return ans;
  }
}
// code provided by PROGIEZ

2860. Happy Students LeetCode Solution in Python

class Solution:
  def countWays(self, nums: list[int]) -> int:
    return sum(a < i < b
               for i, (a, b) in  # i := the number of the selected numbers
               enumerate(itertools.pairwise([-1] + sorted(nums) + [math.inf])))
# code by PROGIEZ

Additional Resources

Happy Coding! Keep following PROGIEZ for more updates and solutions.