Given an array arr[] of N distinct integers and a positive integer K, the task is to find the number of ways of choosing an integer X such that there are exactly K elements in the array that are greater than X.
Examples:
Input: arr[] = {1, 3, 4, 6, 8}, K = 2
Output: 3
Explanation: X can be chosen as 4 or 5
Input: arr[] = {1, 2, 3}, K = 1
Output: 2
Explanation: No matter what integer you choose as X, it’ll never have exactly 2 elements greater than it in the given array.
Approach:
Count total number of distinct elements in the array. If the count of distinct elements is less than or equal to k, then 0 ways are possible, Else the total possible ways will be equal to 1 + the difference between (k+1)th largest element and kth largest element .
Follow the below steps to Implement the approach:
- If n is less than k return 0
- Else sort the array in increasing order and return the value arr[n-k] – arr[n-k-1] + 1.
Below is the Implementation of the above approach:
C++
#include <bits/stdc++.h>
using namespace std;
int countWays( int n, vector< int >v, int k)
{
if (n <= k)
return 0;
sort(v.begin(),v.end());
return v[n-k]-v[n-k-1]+1;
}
int main()
{
vector< int >arr = { 100, 200, 400, 50 };
int k = 3;
int n = arr.size();
cout << countWays(n, arr, k);
}
|
Java
import java.util.*;
class GFG
{
static int countWays( int n, int arr[], int k)
{
if (n <= k)
return 0 ;
Arrays.sort(arr);
return arr[n-k]-arr[n-k- 1 ]+ 1 ;
}
public static void main(String args[])
{
int arr[] = { 100 , 200 , 400 , 50 };
int k = 3 ;
int n = arr.length;
System.out.println(countWays(n, arr, k));
}
}
|
Python
def countWays(n, v, k):
if (n < = k):
return 0
v.sort()
return v[n - k] - v[n - k - 1 ] + 1
arr = [ 100 , 200 , 400 , 50 ]
k = 3
n = len (arr)
print (countWays(n, arr, k))
|
C#
using System;
class GFG {
static int countWays( int n, int [] arr, int k)
{
if (n <= k)
return 0;
Array.Sort(arr);
return arr[n - k] - arr[n - k - 1] + 1;
}
public static void Main()
{
int [] arr = { 100, 200, 400, 50 };
int k = 3;
int n = arr.Length;
Console.Write(countWays(n, arr, k));
}
}
|
Javascript
<script>
function countWays(n, arr, k)
{
if (n <= k)
{
return 0;
}
arr.sort( function (a, b) { return a - b;});
return arr[n - k] - arr[n - k - 1] + 1;
}
var arr = [100, 200, 400, 50];
var k = 3;
var n = arr.length;
console.log(countWays(n, arr, k));
</script>
|
Complexity Analysis:
- Time Complexity: O(N * log(N)), For sorting the array
- Auxiliary Space: O(1)
METHOD 2:Binary Search
APPRAOCH:
We can sort the input array and then for each element in the array, use binary search to find the number of elements that are larger by K.
ALGORITHM:
1.Sort the input array in non-decreasing order.
2.Initialize a count variable to 0.
3.For each element arr[i] in the array:
a. Set low = i + 1 and high = n – 1.
b. While low <= high:
i. Calculate the mid index using mid = (low + high) // 2.
ii. If arr[mid] – arr[i] == K, increment the count variable and break out of the loop.
iii. If arr[mid] – arr[i] > K, set high = mid – 1.
iv. If arr[mid] – arr[i] < K, set low = mid + 1.
4.Return the count variable.
C++
#include <iostream>
#include <algorithm>
#include <vector>
int count_integer(std::vector< int > arr, int K) {
std::sort(arr.begin(), arr.end());
int n = arr.size();
int count = 0;
for ( int i = 0; i < n; i++) {
int low = i+1;
int high = n-1;
while (low <= high) {
int mid = (low+high)/2;
if (arr[mid] - arr[i] == K) {
count++;
break ;
}
else if (arr[mid] - arr[i] > K) {
high = mid-1;
}
else {
low = mid+1;
}
}
}
return count;
}
int main() {
std::vector< int > arr = {1, 3, 4, 6, 8};
int K = 2;
std::cout << count_integer(arr, K) << std::endl;
return 0;
}
|
Java
import java.util.Arrays;
public class Main {
public static int count_integer( int [] arr, int K) {
Arrays.sort(arr);
int n = arr.length;
int count = 0 ;
for ( int i = 0 ; i < n; i++) {
int low = i+ 1 ;
int high = n- 1 ;
while (low <= high) {
int mid = (low+high)/ 2 ;
if (arr[mid] - arr[i] == K) {
count++;
break ;
}
else if (arr[mid] - arr[i] > K) {
high = mid- 1 ;
}
else {
low = mid+ 1 ;
}
}
}
return count;
}
public static void main(String[] args) {
int [] arr = { 1 , 3 , 4 , 6 , 8 };
int K = 2 ;
System.out.println(count_integer(arr, K));
}
}
|
Python3
def count_integer(arr, K):
arr.sort()
n = len (arr)
count = 0
for i in range (n):
low = i + 1
high = n - 1
while low < = high:
mid = (low + high) / / 2
if arr[mid] - arr[i] = = K:
count + = 1
break
elif arr[mid] - arr[i] > K:
high = mid - 1
else :
low = mid + 1
return count
arr = [ 1 , 3 , 4 , 6 , 8 ]
K = 2
print (count_integer(arr, K))
|
C#
using System;
public class Program
{
public static int count_integer( int [] arr, int K)
{
Array.Sort(arr);
int n = arr.Length;
int count = 0;
for ( int i = 0; i < n; i++)
{
int low = i + 1;
int high = n - 1;
while (low <= high)
{
int mid = (low + high) / 2;
if (arr[mid] - arr[i] == K)
{
count++;
break ;
}
else if (arr[mid] - arr[i] > K)
{
high = mid - 1;
}
else
{
low = mid + 1;
}
}
}
return count;
}
public static void Main( string [] args)
{
int [] arr = { 1, 3, 4, 6, 8 };
int K = 2;
Console.WriteLine(count_integer(arr, K));
}
}
|
Javascript
function count_integer(arr, K) {
arr.sort( function (a, b) {
return a - b;
});
var n = arr.length;
var count = 0;
for ( var i = 0; i < n; i++) {
var low = i + 1;
var high = n - 1;
while (low <= high) {
var mid = Math.floor((low + high) / 2);
if (arr[mid] - arr[i] == K) {
count++;
break ;
} else if (arr[mid] - arr[i] > K) {
high = mid - 1;
} else {
low = mid + 1;
}
}
}
return count;
}
var arr = [1, 3, 4, 6, 8];
var K = 2;
console.log(count_integer(arr, K));
|
The time complexity of this approach is O(nlog(n))
The auxiliary space of this approach is O(1)