 Open in App
Not now

# Find Median for each Array element by excluding the index at which Median is calculated

• Difficulty Level : Medium
• Last Updated : 25 Jan, 2023

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: New sorted array will be [3, 4, 4]. Median = 4.
Median after removing A: New sorted array will be [2, 3, 4]. Median = 3.
Median after removing A: New sorted array will be [2, 3, 4]. Median = 3.
Median after removing A: 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++

 `// C++ Program for the above approach` `#include ` `using` `namespace` `std;`   `// Function to return Median array after` `// excluding each individual element` `void` `findMedianExcludedArray(``int` `A[], ``int` `N)` `{` `      ``//iterating over array A` `    ``for` `(``int` `i = 0; i < N; i++) {` `      `  `          ``//Taking pointer to 0 for array B` `        ``int` `j=0;` `      `  `          ``//Taking array B of size N-1` `          ``int` `B[N-1]; ` `      `  `          ``//Taking all elements of A into B except element at index i` `          ``for``(``int` `k=0;k

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++

 `// 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

 ``

Output:

`4, 3, 3, 4`

Time Complexity: O(N*log(N))
Auxiliary Space: O(N)

My Personal Notes arrow_drop_up
Related Articles