Given an array arr[] consisting of N integers and an integer K, the task is to find the count of array elements having rank at most K.
Equal array elements will have equal ranks. Any array element numerically smaller than its immediate greater array element will be ranked one greater than the total number of array elements greater than it.
Examples:
Input: N = 4, arr[] = {100, 50, 50, 25}, K = 3
Output: 3
Explanation: Rank of the players will be {1, 2, 2, 4}.
There are 3 array elements whose ranks are less than or equal to 3.
Therefore, the answer is 3.Input: N = 5, arr[] = {2, 2, 3, 4, 5}, K = 4
Output: 5
Explanation: Rank of the players will be {4, 4, 3, 2, 1}.
There are 5 array elements whose ranks are les than or equal to 4.
Therefore, the answer is 5.
Naive Approach: The simplest approach is to find the current maximum element in the array and compare it with the previous maximum element. If they are equal, then the rank of the previous element and the current element must be equal. Otherwise, assign the rank of the current maximum element as one greater than the number of maximums obtained previously. Repeat this until the given array becomes empty or the rank becomes greater than K, whichever is earlier. After traversing, print the total number of deleted elements.
Time Complexity: O(N2)
Auxiliary Space: O(N)
Efficient Approach: The idea is to use the Sorting Algorithm. After sorting the given array in non-increasing order and for each element, if the current and previous element is unequal, then the rank of the current element must be one greater than the previous element. Otherwise, it stays the same as that of the previous one. Follow the steps below to solve the problem:
- Sort the given arr[] in ascending order.
- Initialize variable rank and position with 1 to store the rank and the position of the current array element respectively.
- Traverse the sorted array from i = (N – 1) to 0 and perform the following operations:
- Update rank with position when adjacent array element are not equal or when i is equal to (N – 1).
- Return position – 1 if rank becomes greater than K.
- Increment position in each traversal.
- Print N, if all the array elements have ranks not exceeding K.
Below is the implementation of the above approach:
C++14
// C++ program for the above approach #include <bits/stdc++.h>using namespace std;// Function to find count of array// elements with rank less than or// equal to kint rankLessThanK(int* arr, int k, int n){ // Initialize rank and position int rank = 1; int position = 1; // Sort the given array sort(arr, arr + n); // Traverse array from right to left for(int i = n - 1; i >= 0; i--) { // Update rank with position, if // adjacent elements are unequal if (i == n - 1 || arr[i] != arr[i + 1]) { rank = position; // Return position - 1, if // rank greater than k if (rank > k) return position - 1; } // Increase position position++; } return n;}// Driver Codeint main(){ // Given array int arr[5] = { 2, 2, 3, 4, 5 }; int N = 5; // Given K int K = 4; // Function Call cout << rankLessThanK(arr, K, N); return 0;}// This code is contributed by hemanth gadarla |
Java
// Java program for the above approachimport java.util.*;import java.lang.*;class GFG { // Function to find count of array // elements with rank less than or // equal to k static int rankLessThanK(int[] arr, int k, int n) { // Initialize rank and position int rank = 1; int position = 1; // Sort the given array Arrays.sort(arr); // Traverse array from right to left for (int i = n - 1; i >= 0; i--) { // Update rank with position, if // adjacent elements are unequal if (i == n - 1 || arr[i] != arr[i + 1]) { rank = position; // Return position - 1, if // rank greater than k if (rank > k) return position - 1; } // Increase position position++; } return n; } // Driver Code public static void main(String[] args) { // Given array int arr[] = { 2, 2, 3, 4, 5 }; int N = arr.length; // Given K int K = 4; // Function Call System.out.println( rankLessThanK(arr, K, N)); }} |
C#
// C# program for the above approach using System; class GFG{ // Function to find count of array// elements with rank less than or// equal to kstatic int rankLessThanK(int[] arr, int k, int n){ // Initialize rank and position int rank = 1; int position = 1; // Sort the given array Array.Sort(arr); // Traverse array from right to left for(int i = n - 1; i >= 0; i--) { // Update rank with position, if // adjacent elements are unequal if (i == n - 1 || arr[i] != arr[i + 1]) { rank = position; // Return position - 1, if // rank greater than k if (rank > k) return position - 1; } // Increase position position++; } return n;} // Driver Codepublic static void Main(){ // Given array int[] arr = { 2, 2, 3, 4, 5 }; int N = arr.Length; // Given K int K = 4; // Function Call Console.WriteLine(rankLessThanK( arr, K, N));}}// This code is contributed by sanjoy_62 |
5
Time Complexity: O(N*log N)
Auxiliary Space: O(N)
Attention reader! Don’t stop learning now. Get hold of all the important DSA concepts with the DSA Self Paced Course at a student-friendly price and become industry ready.
Recommended Posts:
- Construct a Matrix with no element exceeding X and sum of two adjacent elements not exceeding Y
- Split an Array to maximize subarrays having equal count of odd and even elements for a cost not exceeding K
- Maximize count of pairs (i, j) from two arrays having element from first array not exceeding that from second array
- Count array elements exceeding sum of preceding K elements
- Maximize subsequences having array elements not exceeding length of the subsequence
- Maximum possible sum of non-adjacent array elements not exceeding K
- Count of N-digit numbers with absolute difference of adjacent digits not exceeding K
- Count of N-digit numbers with absolute difference of adjacent digits not exceeding K | Set 2
- Count of all possible numbers not exceeding M having suffix N
- Minimize count of Subsets with difference between maximum and minimum element not exceeding K
- Count number of triangles possible with length of sides not exceeding N
- Maximum students to pass after giving bonus to everybody and not exceeding 100 marks
- Largest possible value of M not exceeding N having equal Bitwise OR and XOR between them
- Count pairs with bitwise XOR exceeding bitwise AND from a given array
- Rank of all elements in an array
- Count pairs from two arrays with difference exceeding K
- Rank of all elements in a Stream in descending order when they arrive
- Split array into minimum number of subarrays having GCD of its first and last element exceeding 1
- Check if an array can be split into subarrays with GCD exceeding K
- Replace each element of Array with it's corresponding rank
If you like GeeksforGeeks and would like to contribute, you can also write an article using contribute.geeksforgeeks.org or mail your article to contribute@geeksforgeeks.org. See your article appearing on the GeeksforGeeks main page and help other Geeks.
Please Improve this article if you find anything incorrect by clicking on the "Improve Article" button below.

