通过排除计算 Median 的索引来查找每个 Array 元素的 Median
给定一个包含N个整数的数组A[] ,其中N为偶数, .任务是生成一个中位数数组,其中数组的中位数是通过取数组A[]的中位数(不包括第 A[i] 个元素)来计算的。
例子:
Input N = 4, A = [2, 4, 4, 3]
Output: [4, 3, 3, 4]
Explanation: Median after removing A[0]: New sorted array will be [3, 4, 4]. Median = 4.
Median after removing A[1]: New sorted array will be [2, 3, 4]. Median = 3.
Median after removing A[0]: New sorted array will be [2, 3, 4]. Median = 3.
Median after removing A[0]: New sorted array will be [2, 4, 4]. Median = 4.
Input: N = 6, A = [5, 5, 4, 4, 3, 3]
Output: [4, 4, 4, 4, 4, 4]
朴素方法:对于[0, N)范围内的每个i ,删除当前元素并对剩余数组进行排序,然后计算新数组的中位数。
时间复杂度: O(N*N*log(N))
辅助空间: O(N)
高效方法:由于N是偶数,当前数组中有两个中心元素,可以是当前数组的中位数。在删除任何一个元素时,将有奇数个元素,并且中心元素之一将始终是答案。让我们将两个中心值表示为L和R 。有两种可能的情况——当当前A[i] <=L时, R将是数组的最终中位数。否则,当当前A[i] >= R时, L将是数组的最终中位数。请按照以下步骤解决问题:
- 初始化一个数组B[]以存储数组中元素的原始位置。
- 使用变量i迭代范围[0, N)并执行以下步骤:
- 将 A[i]存储在新数组B[i] 中。
- 对数组A[]进行排序以找到中心元素。
- 使用变量i迭代范围[0, N)并执行以下步骤:
- 如果B[i]小于等于L,则R将是不包括A[i] 的数组的中位数。
- 否则, L将是所需的中位数。
下面是上述方法的实现。
C++
// C++ Program for the above approach
#include
using namespace std;
// Function to return Median array after
// excluding each individual element
int findMedianExcludedArray(int A[], int N)
{
// Store the original array in another
// array to store the sorted version
// and the original position of the array
// element is also important
int B[N];
for (int i = 0; i < N; i++) {
B[i] = A[i];
}
// Sort the original array
sort(A, A + N);
// Stores the left median
int L = A[N / 2 - 1];
// Stores the right median
int R = A[N / 2];
// Iterate over the range
for (int i = 0; i < N; i++) {
// If A[i] is less than equal to L,
// then after removing it, R will become
// the central element, otherwise L
if (B[i] <= L) {
cout << R;
}
else {
cout << L;
}
if (i != N - 1) {
cout << ", ";
}
}
return 0;
}
// Driver Code
int main()
{
int N = 4;
int A[] = { 2, 4, 4, 3 };
findMedianExcludedArray(A, N);
}
Java
// Java code for above approach
import java.util.*;
class GFG{
// Function to return Median array after
// excluding each individual element
static int findMedianExcludedArray(int[] A, int N)
{
// Store the original array in another
// array to store the sorted version
// and the original position of the array
// element is also important
int[] B = new int[N];
for (int i = 0; i < N; i++) {
B[i] = A[i];
}
// Sort the original array
Arrays.sort(A);
// Stores the left median
int L = A[N / 2 - 1];
// Stores the right median
int R = A[N / 2];
// Iterate over the range
for (int i = 0; i < N; i++) {
// If A[i] is less than equal to L,
// then after removing it, R will become
// the central element, otherwise L
if (B[i] <= L) {
System.out.print(R);
}
else {
System.out.print(L);
}
if (i != N - 1) {
System.out.print(", ");
}
}
return 0;
}
// Driver Code
public static void main(String[] args)
{
int N = 4;
int[] A = { 2, 4, 4, 3 };
findMedianExcludedArray(A, N);
}
}
// This code is contributed by target_2.
Python3
# Function to return Median array after
# excluding each individual element
def findMedianExcludedArray(A, N):
# Store the original array in another
# array to store the sorted version
# and the original position of the array
# element is also important
B = []
for i in range(N):
B.append(A[i])
# sort the original array
A.sort()
# Stores the left median
L = A[N//2 - 1]
# Stores the right median
R = A[N//2]
# Iterate over the range
for i in range(N):
# If A[i] is less than equal to L,
# then after removing it, R will become
# the central element, otherwise L
if B[i] <= L:
print(R, end="")
else:
print(L, end="")
if i != N-1:
print(", ", end="")
# Driver code
N = 4
A = [2, 4, 4, 3]
findMedianExcludedArray(A, N)
# This code is contributed by Parth Manchanda
C#
// C# program for above approach
using System;
class GFG{
// Function to return Median array after
// excluding each individual element
static int findMedianExcludedArray(int[] A, int N)
{
// Store the original array in another
// array to store the sorted version
// and the original position of the array
// element is also important
int[] B = new int[N];
for (int i = 0; i < N; i++) {
B[i] = A[i];
}
// Sort the original array
Array.Sort(A);
// Stores the left median
int L = A[N / 2 - 1];
// Stores the right median
int R = A[N / 2];
// Iterate over the range
for (int i = 0; i < N; i++) {
// If A[i] is less than equal to L,
// then after removing it, R will become
// the central element, otherwise L
if (B[i] <= L) {
Console.Write(R);
}
else {
Console.Write(L);
}
if (i != N - 1) {
Console.Write(", ");
}
}
return 0;
}
// Driver Code
static void Main()
{
int N = 4;
int[] A = { 2, 4, 4, 3 };
findMedianExcludedArray(A, N);
}
}
// This code is contributed by sanjoy_62.
Javascript
4, 3, 3, 4
时间复杂度: O(N*log(N))
辅助空间: O(N)