Given two arrays **arr1[]** and **arr2[]**. The array **arr1[]** is sorted. The task is to print the change in median after removing each element from array **arr2[]** one by one.

**Note:** The array **arr2[]** has only those elements that are present in array **arr1[]**.

**Examples:**

Input:arr1[] = {2, 4, 6, 8, 10}, arr2[] = {4, 6}

Output:1 1

Explanation:

Initially median is 6.

After removing 4, array becomes arr1[] = {2, 6, 8, 10}, median = 7, therefore the difference is 7 – 6 = 1.

After removing 6, array becomes arr1[] = {2, 8, 10}, median = 8, therefore the difference is 8 – 7 = 1.

Input:arr1[] = {1, 100, 250, 251}, arr2[] = {250, 1}

Output:-75 75.5

Explanation:

Initially median is 175.

After removing 250, array becomes arr1[] = {1, 100, 251}, median = 100, therefore the difference is 100 – 175 = -75.

After removing 1, array becomes arr1[] = {100, 251}, median = 175.5, therefore the difference is 175.5 – 100 = 75.5.

**Approach:** The idea is to traverse each element of the array **arr2[]** and remove each element from the array **arr1[]** and store the median of the array **arr1[]** after each removal of element in an array(say **temp[]**). Print the consecutive difference of the elements of the array to get change in median after removing elements from **arr2[]**.

Below is the implementation of the above approach:

`// C++ program for the above approach ` `#include <bits/stdc++.h> ` `using` `namespace` `std; ` ` ` `// Function to find the median change ` `// after removing elements from arr2[] ` `void` `medianChange(vector<` `int` `>& arr1, ` ` ` `vector<` `int` `>& arr2) ` `{ ` ` ` `int` `N = arr1.size(); ` ` ` ` ` `// To store the median ` ` ` `vector<` `float` `> median; ` ` ` ` ` `// Store the current median ` ` ` ` ` `// If N is odd ` ` ` `if` `(N & 1) { ` ` ` `median ` ` ` `.push_back(arr1[N / 2] * 1.0); ` ` ` `} ` ` ` ` ` `// If N is even ` ` ` `else` `{ ` ` ` `median ` ` ` `.push_back((arr1[N / 2] ` ` ` `+ arr1[(N - 1) / 2]) ` ` ` `/ 2.0); ` ` ` `} ` ` ` ` ` `for` `(` `auto` `& x : arr2) { ` ` ` ` ` `// Find the current element ` ` ` `// in arr1 ` ` ` `auto` `it = find(arr1.begin(), ` ` ` `arr1.end(), ` ` ` `x); ` ` ` ` ` `// Erase the element ` ` ` `arr1.erase(it); ` ` ` ` ` `// Decrement N ` ` ` `N--; ` ` ` ` ` `// Find the new median ` ` ` `// and append ` ` ` ` ` `// If N is odd ` ` ` `if` `(N & 1) { ` ` ` `median ` ` ` `.push_back(arr1[N / 2] * 1.0); ` ` ` `} ` ` ` ` ` `// If N is even ` ` ` `else` `{ ` ` ` `median ` ` ` `.push_back((arr1[N / 2] ` ` ` `+ arr1[(N - 1) / 2]) ` ` ` `/ 2.0); ` ` ` `} ` ` ` `} ` ` ` ` ` `// Print the corresponding ` ` ` `// difference of median ` ` ` `for` `(` `int` `i = 0; ` ` ` `i < median.size() - 1; ` ` ` `i++) { ` ` ` `cout << median[i + 1] - median[i] ` ` ` `<< ` `' '` `; ` ` ` `} ` `} ` ` ` `// Driven Code ` `int` `main() ` `{ ` ` ` `// Given arrays ` ` ` `vector<` `int` `> arr1 = { 2, 4, 6, 8, 10 }; ` ` ` `vector<` `int` `> arr2 = { 4, 6 }; ` ` ` ` ` `// Function Call ` ` ` `medianChange(arr1, arr2); ` ` ` ` ` `return` `0; ` `} ` |

*chevron_right*

*filter_none*

**Output:**

1 1

**Time Complexity:** O(M*N)

## Recommended Posts:

- Maximize the median of the given array after adding K elements to the same array
- Maximum possible middle element of the array after deleting exactly k elements
- Find the largest after deleting the given elements
- Find the smallest after deleting given elements
- Find the k largest numbers after deleting the given elements
- Find the k smallest numbers after deleting given elements
- Find last two remaining elements after removing median of any 3 consecutive elements repeatedly
- Reduce the array by deleting elements which are greater than all elements to its left
- Find last element after deleting every second element in array of n integers
- Count of Array elements greater than or equal to twice the Median of K trailing Array elements
- Maximize median after doing K addition operation on the Array
- Find maximum points which can be obtained by deleting elements from array
- Minimum elements to change so that for an index i all elements on the left are -ve and all elements on the right are +ve
- Maximize the size of array by deleting exactly k sub-arrays to make array prime
- Count distinct median possible for an Array using given ranges of elements
- Median after K additional integers
- Find array sum using Bitwise OR after splitting given array in two halves after K circular shifts
- Last element remaining by deleting two largest elements and replacing by their absolute difference if they are unequal
- Maximum score of deleting an element from an Array based on given condition
- Find K elements whose absolute difference with median of array is maximum

If you like GeeksforGeeks and would like to contribute, you can also write an article using contribute.geeksforgeeks.org or mail your article to contribute@geeksforgeeks.org. See your article appearing on the GeeksforGeeks main page and help other Geeks.

Please Improve this article if you find anything incorrect by clicking on the "Improve Article" button below.