# Number of players whose rank is equal to or less than a given cutoff rank

• Last Updated : 23 Jul, 2021

Given an array arr[] consisting of N integers and an integer R, denoting the cutoff rank, the task is to count the number of array elements with rank at most R such that the equal array element are ranked the same and distinct array elements are ranked based on their positions in the array arr[].

Examples:

Input: arr[] = {100, 50, 50, 25}, R = 3
Output: 3
Explanation:
The players are ranked as: {1, 2, 2, 4}. The players having ranked at most R(= 3) is {1, 2, 2}. Therefore, the total count is 3.

Input: arr[] = {2, 2, 3, 4, 5}, R = 4
Output: 5

Approach: The given problem can be solved by using the concept of Sorting. Follow the below step to solve this problem:

• Sort the given array arr[] in decreasing order.
• Initialize two variables, say rank as 1 to store the rank of the array elements and say count as 0 to store the required result.
• Traverse the given array arr[], using the variable i, and perform the following steps:
• If the arr[i] is equal to the previous element then assign the same rank as the previous rank to the current element.
• Otherwise, assign the value of (count + 1)th rank to the current element.
• If the rank is greater than R then break. Otherwise, increment the count by 1.
• After completing the above steps, print the value of count as the answer.

Below is the implementation of the above approach:

## C++

 `// C++  program for above approach``#include ``#include ``using` `namespace` `std;` `// Function to find the count of array``// elements having rank at most R``int` `countElements(``int` `R, ``int` `N, ``int` `arr[])``{` `  ``// Sort the array arr[] in the``  ``// decreasing order``  ``sort(arr, arr + N, greater<``int``>());` `  ``// Stores the rank and required``  ``// count of array elements``  ``int` `rank = 1, count = 0;` `  ``// store the previou element``  ``int` `prevScore = arr, score;` `  ``// Traverse the array``  ``for` `(``int` `i = 0; i < N; i++) {``    ``score = arr[i];` `    ``// If score is less than the``    ``// prevScore``    ``if` `(score < prevScore) {``      ``rank = count + 1;``    ``}` `    ``// If the rank is greater than R``    ``if` `(rank > R) {``      ``break``;``    ``}` `    ``// Increment count by 1``    ``count++;` `    ``// update prevscore``    ``prevScore = score;``  ``}` `  ``// return count``  ``return` `count;``}` `// Driver code``int` `main()``{``  ``int` `arr[] = { 100, 50, 50, 25 };``  ``int` `R = 2;``  ``int` `N = ``sizeof``(arr) / ``sizeof``(arr);``  ``cout << countElements(R, N, arr);``  ``return` `0;``}` `// This code is contributed by Parth Manchanda`

## Java

 `// Java program for the above approach``import` `java.util.*;` `class` `GFG``{``  ``static` `void` `reverse(``int` `a[])``  ``{``    ``int` `n = a.length;``    ``int``[] b = ``new` `int``[n];``    ``int` `j = n;``    ``for` `(``int` `i = ``0``; i < n; i++) {``      ``b[j - ``1``] = a[i];``      ``j = j - ``1``;``    ``}``  ``}` `  ``// Function to find the count of array``  ``// elements having rank at most R``  ``static` `int` `countElements(``int` `R, ``int` `N, ``int``[] arr)``  ``{` `    ``// Sort the array arr[] in the``    ``// decreasing order``    ``Arrays.sort(arr);``    ``reverse(arr);` `    ``// Stores the rank and required``    ``// count of array elements``    ``int` `rank = ``1``;``    ``int` `count = -``1``;` `    ``// Stores the previous element``    ``int` `prevScore = arr[``0``];` `    ``// Traverse the array``    ``for``(``int` `score : arr)``    ``{` `      ``// If score is less than the``      ``// prevScore``      ``if` `(score < prevScore)``        ``rank = count + ``1``;` `      ``// If the rank is greater than R``      ``if` `(rank > R)``        ``break``;` `      ``// Increment count by 1``      ``count = count + ``1``;` `      ``// Update prevScore``      ``prevScore = score;``    ``}` `    ``// Return the result``    ``return` `count;``  ``}` `  ``// Driver Code``  ``public` `static` `void` `main(String[] args)``  ``{``    ``int``[] arr = { ``100``, ``50``, ``50``, ``25` `};``    ``int` `R = ``2``;``    ``int` `N = arr.length;` `    ``// Function Call``    ``System.out.println(countElements(R, N, arr));``  ``}``}` `// This code is contributed by sanjoy_62.`

## Python3

 `# Python program for the above approach` `# Function to find the count of array``# elements having rank at most R``def` `countElements(R, N, arr):` `    ``# Sort the array arr[] in the``    ``# decreasing order``    ``arr.sort(reverse ``=` `True``)` `    ``# Stores the rank and required``    ``# count of array elements``    ``rank ``=` `1``    ``count ``=` `0` `    ``# Stores the previous element``    ``prevScore ``=` `arr[``0``]` `    ``# Traverse the array``    ``for` `score ``in` `arr:` `        ``# If score is less than the``        ``# prevScore``        ``if` `score < prevScore:``            ``rank ``=` `count ``+` `1` `        ``# If the rank is greater than R``        ``if` `rank > R:``            ``break``            ` `        ``# Increment count by 1``        ``count ``+``=` `1` `        ``# Update prevScore``        ``prevScore ``=` `score` `    ``# Return the result``    ``return` `count`  `# Driver Code``arr ``=` `[``100``, ``50``, ``50``, ``25``]``R ``=` `2``N ``=` `len``(arr)` `# Function Call``print``(countElements(R, N, arr))`

## C#

 `// C# program for the above approach``using` `System;` `class` `GFG{``    ` `// Function to find the count of array``// elements having rank at most R``static` `int` `countElements(``int` `R, ``int` `N, ``int``[] arr)``{``    ` `    ``// Sort the array arr[] in the``    ``// decreasing order``    ``Array.Sort(arr);``    ``Array.Reverse(arr);` `    ``// Stores the rank and required``    ``// count of array elements``    ``int` `rank = 1;``    ``int` `count = 0;` `    ``// Stores the previous element``    ``int` `prevScore = arr;` `    ``// Traverse the array``    ``foreach``(``int` `score ``in` `arr)``    ``{``        ` `        ``// If score is less than the``        ``// prevScore``        ``if` `(score < prevScore)``            ``rank = count + 1;` `        ``// If the rank is greater than R``        ``if` `(rank > R)``            ``break``;` `        ``// Increment count by 1``        ``count = count + 1;` `        ``// Update prevScore``        ``prevScore = score;``    ``}``    ` `    ``// Return the result``    ``return` `count;``}`  `// Driver code``static` `public` `void` `Main()``{``    ``int``[] arr = { 100, 50, 50, 25 };``    ``int` `R = 2;``    ``int` `N = arr.Length;` `    ``// Function Call``    ``Console.WriteLine(countElements(R, N, arr));``}``}` `// This code is contributed by target_2.`

## Javascript

 ``

Output:

`3`

Time Complexity: O(N*log N)
Auxiliary Space: O(1)

My Personal Notes arrow_drop_up