![]() |
VOOZH | about |
Given an array of integers scores[], where scores[i] represents the score of the i-th student in a class, return the maximum value of k such that:
Examples:
Input: scores = [3, 5, 2, 6, 4, 5]
Output: 4
Explanation: The maximum value of k is 4 because there are at least 4 students who scored 4 or more marks, and each of those students scored at least 4 marks.Input: scores = [1, 2, 3, 4, 5]
Output: 3
Explanation: The maximum value of k is 3 because there are at least 3 students who scored 3 or more marks, and each of those students scored at least 3 marks.
Approach:
The idea is to sort the array in descending order and iterating through it while counting the number of students with scores at least as high as the current index.
Steps-by-step approach:
Below is the implementation of the above approach:
4
Time Complexity: O(n log n), where n is the length of given score array.
Auxiliary Space: O(1)