Open In App

Find the Maximum Score Achieved by at Least K Students

Last Updated : 23 Jul, 2025
Comments
Improve
Suggest changes
Like Article
Like
Report

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:

C++
#include <bits/stdc++.h>
using namespace std;

int maxKValue(vector<int>& scores)
{
    // Step 1: Sort the scores in descending order
    sort(scores.begin(), scores.end(), greater<int>());

    int k = 0;

    // Step 2: Iterate through the sorted scores
    for (int i = 0; i < scores.size(); ++i) {
        // Step 3: Check if the current score is at least
        // i+1
        if (scores[i] >= i + 1) {
            k = i + 1; // Update k
        }
        else {
            break; // If the condition fails, we stop
        }
    }

    return k;
}

int main()
{
    vector<int> scores = { 3, 5, 2, 6, 4, 5 };
    cout << maxKValue(scores) << endl;

    return 0;
}
Java
import java.util.*;

public class MaxKValue {

    public static int maxKValue(List<Integer> scores) {
        // Step 1: Sort the scores in descending order
        scores.sort(Collections.reverseOrder());

        int k = 0;

        // Step 2: Iterate through the sorted scores
        for (int i = 0; i < scores.size(); ++i) {
            // Step 3: Check if the current score is at least i+1
            if (scores.get(i) >= i + 1) {
                k = i + 1; // Update k
            } else {
                break; // If the condition fails, we stop
            }
        }

        return k;
    }

    public static void main(String[] args) {
        List<Integer> scores = Arrays.asList(3, 5, 2, 6, 4, 5);
        System.out.println(maxKValue(scores));
    }
}


// This code is contributed by Shivam Gupta

Output
4

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


Article Tags :
Practice Tags :

Similar Reads