Find Median for each Array element by excluding the index at which Median is calculated
Given an array A[] of N integers where N is even, . the task is to generate an array of medians where the median of the array is calculated by taking the median of array A[] excluding A[i]-th element.
Examples:
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]
Naive Approach: For each i in the range [0, N) remove the current element and sort the remaining array then calculate the median of the new array.
Implementation:-
C++
#include <bits/stdc++.h>
using namespace std;
void findMedianExcludedArray( int A[], int N)
{
for ( int i = 0; i < N; i++) {
int j=0;
int B[N-1];
for ( int k=0;k<N;k++)
{
if (k==i) continue ;
B[j]=A[k];
j++;
}
sort(B,B+N-1);
cout<<B[(N-1)/2]<< " " ;
}
}
int main()
{
int N = 4;
int A[] = { 2, 4, 4, 3 };
findMedianExcludedArray(A, N);
}
|
Java
import java.util.Arrays;
public class GFG
{
static void findMedianExcludedArray( int A[], int N)
{
for ( int i = 0 ; i < N; i++)
{
int j= 0 ;
int B[] = new int [N- 1 ];
for ( int k= 0 ;k<N;k++)
{
if (k==i) continue ;
B[j]=A[k];
j++;
}
Arrays.sort(B);
System.out.print(B[(N- 1 )/ 2 ]+ " " );
}
}
public static void main(String args[])
{
int N = 4 ;
int A[] = { 2 , 4 , 4 , 3 };
findMedianExcludedArray(A, N);
}
}
|
Python3
def findMedianExcludedArray(A, N):
for i in range (N):
j = 0
B = [ 0 ] * (N - 1 )
for k in range (N):
if k = = i:
continue
B[j] = A[k]
j + = 1
B.sort()
print (B[(N - 1 ) / / 2 ], end = " " )
N = 4
A = [ 2 , 4 , 4 , 3 ]
findMedianExcludedArray(A, N)
|
C#
using System;
public static class GFG
{
static void findMedianExcludedArray( int [] A, int N)
{
for ( int i = 0; i < N; i++)
{
int j=0;
int [] B = new int [N-1];
for ( int k=0;k<N;k++)
{
if (k==i) continue ;
B[j]=A[k];
j++;
}
Array.Sort(B);
Console.Write(B[(N-1)/2]+ " " );
}
}
public static void Main()
{
int N = 4;
int [] A = { 2, 4, 4, 3 };
findMedianExcludedArray(A, N);
}
}
|
Javascript
function findMedianExcludedArray(A, N) {
for (let i = 0; i < N; i++) {
let j = 0;
let B = new Array(N - 1);
for (let k = 0; k < N; k++) {
if (k == i) continue ;
B[j] = A[k];
j++;
}
B.sort( function (a, b) { return a - b });
process.stdout.write(B[(Math.floor((N - 1) / 2))] + " " );
}
}
let N = 4;
let A = [2, 4, 4, 3];
findMedianExcludedArray(A, N);
|
Time Complexity: O(N*N*log(N))
Auxiliary Space: O(N)
Efficient Approach: As N is even, there are two central elements in the current array which can be the median of the current array. On removing any one of the elements then there will be an odd number of elements and one of the center elements will always be the answer. Let us denote the two center values be L and R. There are 2 possible cases – When the current A[i] <=L then R will be the final median of the array. Otherwise, when current A[i] >= R then L will be the final median of the array. Follow the steps below to solve the problem:
- Initialize an array B[] to store the original positions of the elements in the array.
- Iterate over the range [0, N) using the variable i and perform the following steps:
- Store A[i] in the new array B[i].
- Sort the array A[] to find the central elements.
- Iterate over the range [0, N) using the variable i and perform the following steps:
- If B[i] is less than equal to L, then R will be the median of the array excluding A[i].
- Else, L will be the required median.
Below is the implementation of the above approach.
C++
#include <bits/stdc++.h>
using namespace std;
int findMedianExcludedArray( int A[], int N)
{
int B[N];
for ( int i = 0; i < N; i++) {
B[i] = A[i];
}
sort(A, A + N);
int L = A[N / 2 - 1];
int R = A[N / 2];
for ( int i = 0; i < N; i++) {
if (B[i] <= L) {
cout << R;
}
else {
cout << L;
}
if (i != N - 1) {
cout << ", " ;
}
}
return 0;
}
int main()
{
int N = 4;
int A[] = { 2, 4, 4, 3 };
findMedianExcludedArray(A, N);
}
|
Java
import java.util.*;
class GFG{
static int findMedianExcludedArray( int [] A, int N)
{
int [] B = new int [N];
for ( int i = 0 ; i < N; i++) {
B[i] = A[i];
}
Arrays.sort(A);
int L = A[N / 2 - 1 ];
int R = A[N / 2 ];
for ( int i = 0 ; i < N; i++) {
if (B[i] <= L) {
System.out.print(R);
}
else {
System.out.print(L);
}
if (i != N - 1 ) {
System.out.print( ", " );
}
}
return 0 ;
}
public static void main(String[] args)
{
int N = 4 ;
int [] A = { 2 , 4 , 4 , 3 };
findMedianExcludedArray(A, N);
}
}
|
Python3
def findMedianExcludedArray(A, N):
B = []
for i in range (N):
B.append(A[i])
A.sort()
L = A[N / / 2 - 1 ]
R = A[N / / 2 ]
for i in range (N):
if B[i] < = L:
print (R, end = "")
else :
print (L, end = "")
if i ! = N - 1 :
print ( ", " , end = "")
N = 4
A = [ 2 , 4 , 4 , 3 ]
findMedianExcludedArray(A, N)
|
C#
using System;
class GFG{
static int findMedianExcludedArray( int [] A, int N)
{
int [] B = new int [N];
for ( int i = 0; i < N; i++) {
B[i] = A[i];
}
Array.Sort(A);
int L = A[N / 2 - 1];
int R = A[N / 2];
for ( int i = 0; i < N; i++) {
if (B[i] <= L) {
Console.Write(R);
}
else {
Console.Write(L);
}
if (i != N - 1) {
Console.Write( ", " );
}
}
return 0;
}
static void Main()
{
int N = 4;
int [] A = { 2, 4, 4, 3 };
findMedianExcludedArray(A, N);
}
}
|
Javascript
<script>
function findMedianExcludedArray(A, N)
{
let B = new Array(N);
for (let i = 0; i < N; i++) {
B[i] = A[i];
}
A.sort();
let L = A[N / 2 - 1];
let R = A[N / 2];
for (let i = 0; i < N; i++) {
if (B[i] <= L) {
document.write(R);
}
else {
document.write(L);
}
if (i != N - 1) {
document.write( ", " );
}
}
return 0;
}
let N = 4;
let A = [ 2, 4, 4, 3 ];
findMedianExcludedArray(A, N);
</script>
|
Time Complexity: O(N*log(N))
Auxiliary Space: O(N)
Last Updated :
24 Mar, 2023
Like Article
Save Article
Share your thoughts in the comments
Please Login to comment...