# Number of ways to choose an integer such that there are exactly K elements greater than it in the given array

Given an array arr[] of N elements and an 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: 2
X can be chosen as 4 or 5

Input: arr[] = {1, 1, 1}, K = 2
Output: 0
No matter what integer you choose as X, it’ll never have exactly 2 elements greater than it in the given array.

## Recommended: Please try your approach on {IDE} first, before moving on to the solution.

Approach:
We count total number of distinct elements in the array. If the count of distinct elements is less than or equal to k, then 0 permutations possible. Else count is equal to number of distinct elements minus k.

Below is the implementation of the above approach:

## C++

 `// C++ implementation of the approach ` `#include ` `using` `namespace` `std; ` ` `  `// Function to returns the required count of integers ` `int` `countWays(``int` `n, ``int` `arr[], ``int` `k) ` `{ ` ` `  `    ``if` `(k <= 0 || k >= n) ` `        ``return` `0; ` ` `  `    ``unordered_set<``int``> s(arr, arr+n); ` `    ``if` `(s.size() <= k) ` `       ``return` `0; ` ` `  `    ``// Return the required count ` `    ``return` `s.size() - k; ` `} ` ` `  `// Driver code ` `int` `main() ` `{ ` `    ``int` `arr[] = { 100, 200, 400, 50 }; ` `    ``int` `k = 3; ` `    ``int` `n = ``sizeof``(arr) / ``sizeof``(arr); ` `    ``cout << countWays(n, arr, k); ` `} `

## Java

 `// Java implementation of the approach ` `import` `java.util.*; ` ` `  `class` `GFG ` `{ ` ` `  `// Function to returns the  ` `// required count of integers ` `static` `int` `countWays(``int` `n, ``int` `arr[], ``int` `k) ` `{ ` ` `  `    ``if` `(k <= ``0` `|| k >= n) ` `        ``return` `0``; ` `    ``Set s = ``new` `HashSet(); ` `    ``for``(``int` `i = ``0``; i < n; i++) ` `        ``s.add(arr[i]); ` `         `  `    ``if` `(s.size() <= k) ` `        ``return` `0``; ` ` `  `    ``// Return the required count ` `    ``return` `s.size() - k; ` `} ` ` `  `// Driver code ` `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)); ` `} ` `} ` ` `  `// This code id contributed by ` `// Surendra_Gangwar `

## Python3

 `# Python 3 implementation of the approach  ` ` `  `# Function to returns the required count of integers  ` `def` `countWays(n, arr, k) : ` ` `  `    ``if` `(k <``=` `0` `or` `k >``=` `n) : ` `        ``return` `0` ` `  `    ``s ``=` `set``() ` `    ``for` `element ``in` `arr : ` `        ``s.add(element) ` `         `  `    ``if` `(``len``(s) <``=` `k) : ` `        ``return` `0``;  ` ` `  `    ``# Return the required count  ` `    ``return` `len``(s) ``-` `k;  ` ` `  ` `  `# Driver code  ` `if` `__name__ ``=``=` `"__main__"` `: ` `     `  `    ``arr ``=` `[ ``100``, ``200``, ``400``, ``50` `]  ` `    ``k ``=` `3``;  ` `    ``n ``=` `len``(arr) ; ` `    ``print``(countWays(n, arr, k))  ` ` `  `# This code is contributed by Ryuga `

## C#

 `// C# implementation of the approach ` `using` `System; ` `using` `System.Collections.Generic; ` ` `  `class` `GFG ` `{ ` ` `  `    ``// Function to returns the  ` `    ``// required count of integers ` `    ``static` `int` `countWays(``int` `n, ``int` `[]arr, ``int` `k) ` `    ``{ ` ` `  `        ``if` `(k <= 0 || k >= n) ` `            ``return` `0; ` `        ``HashSet<``int``> s = ``new` `HashSet<``int``>(); ` `        ``for``(``int` `i = 0; i < n; i++) ` `            ``s.Add(arr[i]); ` ` `  `        ``if` `(s.Count <= k) ` `            ``return` `0; ` ` `  `        ``// Return the required count ` `        ``return` `s.Count - k; ` `    ``} ` ` `  `    ``// Driver code ` `    ``public` `static` `void` `Main(String []args) ` `    ``{ ` `        ``int` `[]arr = { 100, 200, 400, 50 }; ` `        ``int` `k = 3; ` `        ``int` `n = arr.Length; ` `        ``Console.WriteLine(countWays(n, arr, k)); ` `    ``} ` `} ` ` `  `// This code is contributed by Rajput-Ji `

## PHP

 `= ``\$n``)  ` `        ``return` `0; ` ` `  `    ``\$s` `= ``array``(); ` `    ``foreach` `(``\$arr` `as` `\$value``) ` `        ``array_push``(``\$s``, ``\$value``); ` `    ``\$s` `= ``array_unique``(``\$s``); ` `         `  `    ``if` `(``count``(``\$s``) <= ``\$k``) ` `        ``return` `0;  ` ` `  `    ``// Return the required count  ` `    ``return` `count``(``\$s``) - ``\$k``;  ` `} ` ` `  `// Driver code  ` `\$arr` `= ``array``(100, 200, 400, 50);  ` `\$k` `= 3;  ` `\$n` `= ``count``(``\$arr``); ` `print``(countWays(``\$n``, ``\$arr``, ``\$k``));  ` ` `  `// This code is contributed by mits ` `?> `

Output:

```1
```

Time Complexity: O(N * log(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.

My Personal Notes arrow_drop_up Check out this Author's contributed articles.

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.

Article Tags :
Practice Tags :

Be the First to upvote.

Please write to us at contribute@geeksforgeeks.org to report any issue with the above content.