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 i^th 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
Inputnums = [1,1]
Output2
There are only two valid ways: select no students or select both students.
Example 2
Inputnums = [6,0,3,3,6,7,2,7]
Output3
The valid ways are selecting student 1, selecting students 1, 2, 3, and 6, or selecting all students.

Constraints

  • 1 <= nums.length <= 10^5
  • 0 <= nums[i] < nums.length

Asked at 1 companies

</>

Your Solution

(Ctrl/Cmd + Enter)

Switching Language

Loading template...

Loading...

Sign in to save your progress

AI code evaluation

Get a correctness verdict, missed edge cases, and complexity analysis of your solution.

Sign in to evaluate