给定一个数组arr []和一个整数K ,任务是找到与数组中位数的绝对差最大的数组K个元素。
注意:如果两个元素的差相等,则将最大元素考虑在内。
例子:
Input : arr[] = {1, 2, 3, 4, 5}, k = 3
Output : {5, 1, 4}
Explanation :
Median m = 3,
Difference of each array elements from median,
1 ==> diff(1-3) = 2
2 ==> diff(2-3) = 1
3 ==> diff(3-3) = 0
4 ==> diff(4-3) = 1
5 ==> diff(5-3) = 2
First K elements are 5, 1, 4 in this array.
Input: arr[] = {1, 2, 3}, K = 2
Output: {3, 1}
方法:
- 对数组进行排序并找到数组的中位数
- 创建一个差值数组以存储每个元素与已排序数组的中位数的差。
- 差异最大的元素将是数组的角元素。因此,将两个指针初始化为数组的两个角元素,即0和N – 1。
- 最后,将数组中的元素与中值最大地一一对应。
下面是上述方法的实现:
C++
// C++ implementation to find first K
// elements whose difference with the
// median of array is maximum
#include
using namespace std;
// Function for calculating median
double findMedian(int a[], int n)
{
// check for even case
if (n % 2 != 0)
return (double)a[n/2];
return (double)(a[(n-1)/2] + a[n/2])/2.0;
}
// Function to find the K maximum absolute
// difference with the median of the array
void kStrongest(int arr[], int n, int k)
{
// Sort the array.
sort(arr, arr + n);
// Store median
double median = findMedian(arr, n);
int diff[n];
// Find and store difference
for (int i = 0; i < n; i++) {
diff[i] = abs(median - arr[i]);
}
int i = 0, j = n - 1;
while (k > 0) {
// If diff[i] is greater print it
// Else print diff[j]
if (diff[i] > diff[j]) {
cout << arr[i] << " ";
i++;
}
else {
cout << arr[j] << " ";
j--;
}
k--;
}
}
// Driver Code
int main()
{
int arr[] = { 1, 2, 3, 4, 5 };
int k = 3;
int n = sizeof(arr) / sizeof(arr[0]);
kStrongest(arr, n, k);
return 0;
}
Java
// Java implementation to find first K
// elements whose difference with the
// median of array is maximum
import java.util.*;
class GFG{
// Function for calculating median
static double findMedian(int a[], int n)
{
// check for even case
if (n % 2 != 0)
return (double)a[n / 2];
return (double)(a[(n - 1) / 2] +
a[n / 2]) / 2.0;
}
// Function to find the K maximum absolute
// difference with the median of the array
static void kStrongest(int arr[], int n, int k)
{
// Sort the array.
Arrays.sort(arr);
// Store median
double median = findMedian(arr, n);
int []diff = new int[n];
// Find and store difference
for (int i = 0; i < n; i++)
{
diff[i] = (int)Math.abs(median - arr[i]);
}
int i = 0, j = n - 1;
while (k > 0)
{
// If diff[i] is greater print it
// Else print diff[j]
if (diff[i] > diff[j])
{
System.out.print(arr[i] + " ");
i++;
}
else
{
System.out.print(arr[j] + " ");
j--;
}
k--;
}
}
// Driver Code
public static void main(String[] args)
{
int arr[] = { 1, 2, 3, 4, 5 };
int k = 3;
int n = arr.length;
kStrongest(arr, n, k);
}
}
// This code is contributed by sapnasingh4991
Python3
# Python3 program to find first K
# elements whose difference with the
# median of array is maximum
# Function for calculating median
def findMedian(a, n):
# Check for even case
if (n % 2 != 0):
return a[int(n / 2)]
return (a[int((n - 1) / 2)] +
a[int(n / 2)]) / 2.0
# Function to find the K maximum
# absolute difference with the
# median of the array
def kStrongest(arr, n, k):
# Sort the array
arr.sort()
# Store median
median = findMedian(arr, n)
diff = [0] * (n)
# Find and store difference
for i in range(n):
diff[i] = abs(median - arr[i])
i = 0
j = n - 1
while (k > 0):
# If diff[i] is greater print
# it. Else print diff[j]
if (diff[i] > diff[j]):
print(arr[i], end = " ")
i += 1
else:
print(arr[j], end = " ")
j -= 1
k -= 1
# Driver code
arr = [ 1, 2, 3, 4, 5 ]
k = 3
n = len(arr)
kStrongest(arr, n, k)
# This code is contributed by sanjoy_62
C#
// C# implementation to find first K
// elements whose difference with the
// median of array is maximum
using System;
class GFG{
// Function for calculating median
static double findMedian(int []a, int n)
{
// Check for even case
if (n % 2 != 0)
return (double)a[n / 2];
return (double)(a[(n - 1) / 2] +
a[n / 2]) / 2.0;
}
// Function to find the K maximum absolute
// difference with the median of the array
static void kStrongest(int []arr, int n,
int k)
{
// Sort the array.
Array.Sort(arr);
int i = 0;
// Store median
double median = findMedian(arr, n);
int []diff = new int[n];
// Find and store difference
for(i = 0; i < n; i++)
{
diff[i] = (int)Math.Abs(median - arr[i]);
}
int j = n - 1;
i = 0;
while (k > 0)
{
// If diff[i] is greater print it
// Else print diff[j]
if (diff[i] > diff[j])
{
Console.Write(arr[i] + " ");
i++;
}
else
{
Console.Write(arr[j] + " ");
j--;
}
k--;
}
}
// Driver Code
public static void Main(String[] args)
{
int []arr = { 1, 2, 3, 4, 5 };
int k = 3;
int n = arr.Length;
kStrongest(arr, n, k);
}
}
// This code is contributed by Rohit_ranjan
输出:
5 1 4