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

• Last Updated : 21 Jan, 2022

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 2
Explanation: 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 using namespace std; // Function to find the Kth// largest element to the right// of every elementint getKLargest(int* arr, int r,                int l, int& K){    // Elements to the right    // of current element    vector 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 Codeint main(){    int arr[] = { -4, 7, 5, 3, 0 };    int N = sizeof(arr) / sizeof(arr);    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 elementdef 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 Codearr = [-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



Output
5 3 0 3 0

Time Complexity: O(N2 * 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 using namespace std; // Function to find the kth largest// element to the rightint getKLargest(vector& arr, int r,                int l, int& k){    set s(arr.begin() + r + 1,               arr.end());    if (l - r >= k) {        set::iterator it = s.end();        advance(it, -k);        return *it;    }    else        return arr[r];} // Driver codeint main(){    int arr[] = { -4, 7, 5, 3, 0 };    int N = sizeof(arr) / sizeof(arr);    int i, K = 2;     vector 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(N2 * logN)
Auxiliary Space: O(N)

My Personal Notes arrow_drop_up