VOOZH about

URL: https://www.geeksforgeeks.org/dsa/find-the-maximum-score-achieved-by-at-least-k-students/

⇱ Find the Maximum Score Achieved by at Least K Students - GeeksforGeeks


  • Courses
  • Tutorials
  • Interview Prep

Find the Maximum Score Achieved by at Least K Students

Last Updated : 23 Jul, 2025

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:

  • At least k students achieved a score of k or more.
  • Each of those k students scored at least k marks.

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:

  • Sort the scores array in descending order.
  • Initialize k to 0.
    • Iterate through the sorted scores array:
    • For each score at index i, check if the score is greater than or equal to i + 1 (since i is zero-based).
    • If the score is sufficient, update k to i + 1.
    • Otherwise, break the loop.
  • Return the value of k.

Below is the implementation of the above approach:


Output
4

Time Complexity: O(n log n), where n is the length of given score array.
Auxiliary Space: O(1)

Comment