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 less 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 elements is 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
#include <bits/stdc++.h>
using namespace std;
int rankLessThanK( int * arr, int k, int n)
{
int rank = 1;
int position = 1;
sort(arr, arr + n);
for ( int i = n - 1; i >= 0; i--)
{
if (i == n - 1 || arr[i] != arr[i + 1])
{
rank = position;
if (rank > k)
return position - 1;
}
position++;
}
return n;
}
int main()
{
int arr[5] = { 2, 2, 3, 4, 5 };
int N = 5;
int K = 4;
cout << rankLessThanK(arr, K, N);
return 0;
}
|
Java
import java.util.*;
import java.lang.*;
class GFG {
static int rankLessThanK( int [] arr,
int k, int n)
{
int rank = 1 ;
int position = 1 ;
Arrays.sort(arr);
for ( int i = n - 1 ; i >= 0 ; i--) {
if (i == n - 1
|| arr[i] != arr[i + 1 ]) {
rank = position;
if (rank > k)
return position - 1 ;
}
position++;
}
return n;
}
public static void main(String[] args)
{
int arr[] = { 2 , 2 , 3 , 4 , 5 };
int N = arr.length;
int K = 4 ;
System.out.println(
rankLessThanK(arr, K, N));
}
}
|
Python3
def rankLessThanK(arr, k, n):
rank = 1 ;
position = 1 ;
arr = sorted (arr)
for i in range (n - 1 ,
- 1 , - 1 ):
if (i = = n - 1 or
arr[i] ! = arr[i + 1 ]):
rank = position;
if (rank > k):
return position - 1 ;
position + = 1 ;
return n;
if __name__ = = '__main__' :
arr = [ 2 , 2 , 3 , 4 , 5 ];
N = len (arr);
K = 4 ;
print (rankLessThanK(arr, K, N));
|
C#
using System;
class GFG{
static int rankLessThanK( int [] arr,
int k, int n)
{
int rank = 1;
int position = 1;
Array.Sort(arr);
for ( int i = n - 1; i >= 0; i--)
{
if (i == n - 1 || arr[i] != arr[i + 1])
{
rank = position;
if (rank > k)
return position - 1;
}
position++;
}
return n;
}
public static void Main()
{
int [] arr = { 2, 2, 3, 4, 5 };
int N = arr.Length;
int K = 4;
Console.WriteLine(rankLessThanK(
arr, K, N));
}
}
|
Javascript
<script>
function rankLessThanK(arr, k, n)
{
let rank = 1;
let position = 1;
arr.sort();
for (let i = n - 1; i >= 0; i--)
{
if (i == n - 1 || arr[i] != arr[i + 1])
{
rank = position;
if (rank > k)
return position - 1;
}
position++;
}
return n;
}
let arr = [ 2, 2, 3, 4, 5 ];
let N = arr.length;
let K = 4;
document.write(rankLessThanK(arr, K, N));
</script>
|
Time Complexity: O(N*log N)
Auxiliary Space: O(1)
Feeling lost in the world of random DSA topics, wasting time without progress? It's time for a change! Join our DSA course, where we'll guide you on an exciting journey to master DSA efficiently and on schedule.
Ready to dive in? Explore our Free Demo Content and join our DSA course, trusted by over 100,000 geeks!
Last Updated :
30 Nov, 2022
Like Article
Save Article