# Find Kth largest element from right of every element in the array

Given an array **arr[]** of size **N **and an integer **K**. The task is to find the** Kth** **largest** element from the **right** of every element in the array. If there are not enough elements to the right then print the same element.

**Examples:**

Input:N = 6, K = 3, arr[] = {4, 5, 3, 6, 7, 2}Output:5 3 2 6 7 2Explanation:The elements right to 4 are {5, 3, 6, 7, 2}.

So 3rd largest element to the right of 4 is 5.

Similarly, repeat the process for the rest of the elements.

And 7 and 2 does not have sufficient element to the right.

So, they are kept as it is.

Input:N = 5, K = 2, arr[] = {-4, 7, 5, 3, 0}Output:5 3 0 3 0

**Naive Approach:** The naive approach is to **sort** every **subarray** to the right of every element and check whether the **Kth** **largest** element exists or not. If it exists, print the Kth largest element, else print the same element.

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 the Kth` `// largest element to the right` `// of every element` `int` `getKLargest(` `int` `* arr, ` `int` `r,` ` ` `int` `l, ` `int` `& K)` `{` ` ` `// Elements to the right` ` ` `// of current element` ` ` `vector<` `int` `> v(arr, arr + l + 1);` ` ` `// There are greater than K elements` ` ` `// to the right` ` ` `if` `(l - r >= K) {` ` ` `// Sort the vector` ` ` `sort(v.begin() + r + 1, v.end());` ` ` `return` `v[l - K + 1];` ` ` `}` ` ` `else` ` ` `return` `v[r];` `}` `// Driver Code` `int` `main()` `{` ` ` `int` `arr[] = { -4, 7, 5, 3, 0 };` ` ` `int` `N = ` `sizeof` `(arr) / ` `sizeof` `(arr[0]);` ` ` `int` `K = 2;` ` ` `for` `(` `int` `i = 0; i < N; i++)` ` ` `cout << getKLargest(arr, i, N - 1, K)` ` ` `<< ` `" "` `;` ` ` `return` `0;` `}` |

## Python3

`# Python code for the above approach` `# Function to find the Kth` `# largest element to the right` `# of every element` `def` `getKLargest(arr, r, l, K):` ` ` `# Elements to the right` ` ` `# of current element` ` ` `v ` `=` `arr[` `0` `: l ` `+` `1` `]` ` ` `# There are greater than K elements` ` ` `# to the right` ` ` `if` `(l ` `-` `r >` `=` `K):` ` ` `# Sort the vector` ` ` `temp1 ` `=` `v[` `0` `: r ` `+` `1` `]` ` ` `temp ` `=` `v[r ` `+` `1` `:]` ` ` `temp.sort()` ` ` `v ` `=` `temp1 ` `+` `temp` ` ` `return` `v[l ` `-` `K ` `+` `1` `]` ` ` `else` `:` ` ` `return` `v[r]` `# Driver Code` `arr ` `=` `[` `-` `4` `, ` `7` `, ` `5` `, ` `3` `, ` `0` `]` `N ` `=` `len` `(arr)` `K ` `=` `2` `for` `i ` `in` `range` `(N):` ` ` `print` `(getKLargest(arr, i, N ` `-` `1` `, K), end` `=` `" "` `)` `# This code is contributed by Saurabh Jaiswal` |

## Javascript

`<script>` ` ` `// JavaScript code for the above approach` ` ` `// Function to find the Kth` ` ` `// largest element to the right` ` ` `// of every element` ` ` `function` `getKLargest(arr, r,` ` ` `l, K)` ` ` `{` ` ` ` ` `// Elements to the right` ` ` `// of current element` ` ` `let v = arr.slice(0, l + 1);` ` ` `// There are greater than K elements` ` ` `// to the right` ` ` `if` `(l - r >= K) {` ` ` `// Sort the vector` ` ` `let temp1 = v.slice(0, r + 1);` ` ` `let temp = v.slice(r + 1);` ` ` `temp.sort(` `function` `(a, b) { ` `return` `a - b })` ` ` `v = temp1.concat(temp)` ` ` `return` `v[l - K + 1];` ` ` `}` ` ` `else` ` ` `return` `v[r];` ` ` `}` ` ` `// Driver Code` ` ` `let arr = [-4, 7, 5, 3, 0];` ` ` `let N = arr.length;` ` ` `let K = 2;` ` ` `for` `(let i = 0; i < N; i++)` ` ` `document.write(getKLargest(arr, i, N - 1, K) + ` `" "` `)` ` ` `// This code is contributed by Potta Lokesh` ` ` `</script>` |

**Output**

5 3 0 3 0

**Time Complexity****: **O(N^{2} * logN)**Auxiliary Space****: **O(N)

**Approach based on Sets:** Another approach is to use** sets**. Though both the approaches have same the **time complexity**, sets have an **inbuilt** sorting feature, it doesn’t require explicit sorting.

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 the kth largest` `// element to the right` `int` `getKLargest(vector<` `int` `>& arr, ` `int` `r,` ` ` `int` `l, ` `int` `& k)` `{` ` ` `set<` `int` `> s(arr.begin() + r + 1,` ` ` `arr.end());` ` ` `if` `(l - r >= k) {` ` ` `set<` `int` `>::iterator it = s.end();` ` ` `advance(it, -k);` ` ` `return` `*it;` ` ` `}` ` ` `else` ` ` `return` `arr[r];` `}` `// Driver code` `int` `main()` `{` ` ` `int` `arr[] = { -4, 7, 5, 3, 0 };` ` ` `int` `N = ` `sizeof` `(arr) / ` `sizeof` `(arr[0]);` ` ` `int` `i, K = 2;` ` ` `vector<` `int` `> a(arr, arr + N);` ` ` `for` `(i = 0; i < N; i++)` ` ` `cout << getKLargest(a, i, N - 1, K)` ` ` `<< ` `" "` `;` ` ` `return` `0;` `}` |

**Output**

5 3 0 3 0

**Time Complexity****: **O(N^{2} * logN)**Auxiliary Space****: **O(N)