从数组中的每个元素的右侧找到第 K 个最大的元素
给定一个大小为N的数组arr[]和一个整数K 。任务是从数组中每个元素的右侧找到第K个最大的元素。如果右侧没有足够的元素,则打印相同的元素。
例子:
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
朴素方法:朴素方法是将每个子数组排序到每个元素的右侧,并检查第 K个最大的元素是否存在。如果存在,则打印第 K 个最大的元素,否则打印相同的元素。
下面是上述方法的实现:
C++14
// C++ program for the above approach
#include
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 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;
}
Java
// Java code for the above approach
import java.util.*;
class GFG{
// Function to sort the elements of the array
// from index a to index b
static void sort(int[] arr, int N, int a, int b)
{
// Variables to store start and end of the index
// range
int l = Math.min(a, b);
int r = Math.max(a, b);
// Temporary array
int[] temp = new int[r - l + 1];
int j = 0;
for (int i = l; i <= r; i++) {
temp[j] = arr[i];
j++;
}
// Sort the temporary array
Arrays.sort(temp);
// Modifying original array with temporary array
// elements
j = 0;
for (int i = l; i <= r; i++) {
arr[i] = temp[j];
j++;
}
}
// Function to find the Kth
// largest element to the right
// of every element
static int getKLargest(int[] arr, int r, int l, int K)
{
// Elements to the right
// of current element
int n = arr.length;
int[] v = new int[l + 1];
for (int i = 0; i < l + 1; i++) {
v[i] = arr[i];
}
// There are greater than K elements
// to the right
if (l - r >= K) {
// Sort the vector
sort(v, n, r + 1, n - 1);
return v[l - K + 1];
}
else
return v[r];
}
// Driver Code
public static void main(String[] args)
{
int arr[] = { -4, 7, 5, 3, 0 };
int N = arr.length;
int K = 2;
for (int i = 0; i < N; i++)
System.out.print(getKLargest(arr, i, N - 1, K)
+ " ");
}
}
// This code is contributed by code_hunt.
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
C#
// C# program for the above approach
using System;
class GFG
{
// Function to sort the elements of the array
// from index a to index b
static void sort(int[] arr, int N, int a, int b)
{
// Variables to store start and end of the index
// range
int l = Math.Min(a, b);
int r = Math.Max(a, b);
// Temporary array
int[] temp = new int[r - l + 1];
int j = 0;
for (int i = l; i <= r; i++) {
temp[j] = arr[i];
j++;
}
// Sort the temporary array
Array.Sort(temp);
// Modifying original array with temporary array
// elements
j = 0;
for (int i = l; i <= r; i++) {
arr[i] = temp[j];
j++;
}
}
// Function to find the Kth
// largest element to the right
// of every element
static int getKLargest(int[] arr, int r, int l, int K)
{
// Elements to the right
// of current element
int n = arr.Length;
int[] v = new int[l + 1];
for (int i = 0; i < l + 1; i++) {
v[i] = arr[i];
}
// There are greater than K elements
// to the right
if (l - r >= K) {
// Sort the vector
sort(v, n, r + 1, n - 1);
return v[l - K + 1];
}
else
return v[r];
}
// Driver Code
public static void Main()
{
int[] arr = { -4, 7, 5, 3, 0 };
int N = arr.Length;
int K = 2;
for (int i = 0; i < N; i++)
Console.Write(getKLargest(arr, i, N - 1, K)
+ " ");
}
}
// This code is contributed by Samim Hossain Mondal.
Javascript
C++14
// C++ program for the above approach
#include
using namespace std;
// Function to find the kth largest
// element to the right
int 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 code
int main()
{
int arr[] = { -4, 7, 5, 3, 0 };
int N = sizeof(arr) / sizeof(arr[0]);
int i, K = 2;
vector a(arr, arr + N);
for (i = 0; i < N; i++)
cout << getKLargest(a, i, N - 1, K)
<< " ";
return 0;
}
Java
// Java program for the above approach
import java.util.*;
class GFG{
// Function to find the kth largest
// element to the right
static int getKLargest(List arr, int r,
int l, int k)
{
HashSet s = new HashSet<>(arr.subList(r+1,arr.size()));
List a = new ArrayList<>(s);
if (l - r >= k) {
return a.get(a.size()-k);
}
else
return arr.get(r);
}
// Driver code
public static void main(String[] args)
{
Integer arr[] = { -4, 7, 5, 3, 0 };
int N = arr.length;
int i=0;
int K = 2;
List a = Arrays.asList(arr);
for (i = 0; i < N; i++)
System.out.print(getKLargest(a, i, N - 1, K)
+ " ");
}
}
// This code is contributed by shikhasingrajput
Python3
# Python3 program for the above approach
# def to find the kth largest
# element to the right
def getKLargest( arr , r , l , k):
s = set()
for i in range(r+1,len(arr)):
s.add(arr[i])
a = []
for p in s:
a.append(p)
if(l - r >= k):
return a[len(a)- k]
else:
return arr[r]
# Driver code
arr = [ -4, 7, 5, 3, 0 ]
N = len(arr)
i = 0
K = 2
a = arr
for i in range(N):
print(getKLargest(a, i, N - 1, K) ,end=" ")
# This code is contributed by shinjanpatra
C#
// C# program for the above approach
using System;
using System.Collections.Generic;
public class GFG{
// Function to find the kth largest
// element to the right
static int getKLargest(List arr, int r,
int l, int k)
{
HashSet s = new HashSet(arr.GetRange(r+1,arr.Count-(r+1)));
List a = new List(s);
a.Reverse();
if (l - r >= k) {
return a[a.Count-k];
}
else
return arr[r];
}
// Driver code
public static void Main(String[] args)
{
int []arr = { -4, 7, 5, 3, 0 };
int N = arr.Length;
int i=0;
int K = 2;
List a = new List(arr);
for (i = 0; i < N; i++)
Console.Write(getKLargest(a, i, N - 1, K)
+ " ");
}
}
// This code is contributed by 29AjayKumar
Javascript
5 3 0 3 0
时间复杂度: O(N 2 * logN)
辅助空间: O(N)
基于集合的方法:另一种方法是使用集合。尽管这两种方法具有相同的时间复杂度,但集合具有内置的排序功能,它不需要显式排序。
下面是上述方法的实现:
C++14
// C++ program for the above approach
#include
using namespace std;
// Function to find the kth largest
// element to the right
int 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 code
int main()
{
int arr[] = { -4, 7, 5, 3, 0 };
int N = sizeof(arr) / sizeof(arr[0]);
int i, K = 2;
vector a(arr, arr + N);
for (i = 0; i < N; i++)
cout << getKLargest(a, i, N - 1, K)
<< " ";
return 0;
}
Java
// Java program for the above approach
import java.util.*;
class GFG{
// Function to find the kth largest
// element to the right
static int getKLargest(List arr, int r,
int l, int k)
{
HashSet s = new HashSet<>(arr.subList(r+1,arr.size()));
List a = new ArrayList<>(s);
if (l - r >= k) {
return a.get(a.size()-k);
}
else
return arr.get(r);
}
// Driver code
public static void main(String[] args)
{
Integer arr[] = { -4, 7, 5, 3, 0 };
int N = arr.length;
int i=0;
int K = 2;
List a = Arrays.asList(arr);
for (i = 0; i < N; i++)
System.out.print(getKLargest(a, i, N - 1, K)
+ " ");
}
}
// This code is contributed by shikhasingrajput
Python3
# Python3 program for the above approach
# def to find the kth largest
# element to the right
def getKLargest( arr , r , l , k):
s = set()
for i in range(r+1,len(arr)):
s.add(arr[i])
a = []
for p in s:
a.append(p)
if(l - r >= k):
return a[len(a)- k]
else:
return arr[r]
# Driver code
arr = [ -4, 7, 5, 3, 0 ]
N = len(arr)
i = 0
K = 2
a = arr
for i in range(N):
print(getKLargest(a, i, N - 1, K) ,end=" ")
# This code is contributed by shinjanpatra
C#
// C# program for the above approach
using System;
using System.Collections.Generic;
public class GFG{
// Function to find the kth largest
// element to the right
static int getKLargest(List arr, int r,
int l, int k)
{
HashSet s = new HashSet(arr.GetRange(r+1,arr.Count-(r+1)));
List a = new List(s);
a.Reverse();
if (l - r >= k) {
return a[a.Count-k];
}
else
return arr[r];
}
// Driver code
public static void Main(String[] args)
{
int []arr = { -4, 7, 5, 3, 0 };
int N = arr.Length;
int i=0;
int K = 2;
List a = new List(arr);
for (i = 0; i < N; i++)
Console.Write(getKLargest(a, i, N - 1, K)
+ " ");
}
}
// This code is contributed by 29AjayKumar
Javascript
5 3 0 3 0
时间复杂度: O(N 2 * logN)
辅助空间: O(N)