Change in Median of given array after deleting given elements
Last Updated :
24 Mar, 2023
Given two arrays arr1[] and arr2[]. The array arr1[] is sorted. The task is to print the change in the 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++
#include <bits/stdc++.h>
using namespace std;
void medianChange(vector< int >& arr1,
vector< int >& arr2)
{
int N = arr1.size();
vector< float > median;
if (N & 1) {
median
.push_back(arr1[N / 2] * 1.0);
}
else {
median
.push_back((arr1[N / 2]
+ arr1[(N - 1) / 2])
/ 2.0);
}
for ( auto & x : arr2) {
auto it = find(arr1.begin(),
arr1.end(),
x);
arr1.erase(it);
N--;
if (N & 1) {
median
.push_back(arr1[N / 2] * 1.0);
}
else {
median
.push_back((arr1[N / 2]
+ arr1[(N - 1) / 2])
/ 2.0);
}
}
for ( int i = 0;
i < median.size() - 1;
i++) {
cout << median[i + 1] - median[i]
<< ' ' ;
}
}
int main()
{
vector< int > arr1 = { 2, 4, 6, 8, 10 };
vector< int > arr2 = { 4, 6 };
medianChange(arr1, arr2);
return 0;
}
|
Java
import java.util.*;
class GFG{
public static void medianChange(List<Integer> arr1,
List<Integer> arr2)
{
int N = arr1.size();
List<Integer> median = new ArrayList<>();
if ((N & 1 ) != 0 )
median.add(arr1.get(N / 2 ) * 1 );
else
median.add((arr1.get(N / 2 ) +
arr1.get((N - 1 ) / 2 )) / 2 );
for ( int x = 0 ; x < arr2.size(); x++)
{
int it = arr1.indexOf(arr2.get(x));
arr1.remove(it);
N--;
if ((N & 1 ) != 0 )
{
median.add(arr1.get(N / 2 ) * 1 );
}
else
{
median.add((arr1.get(N / 2 ) +
arr1.get((N - 1 ) / 2 )) / 2 );
}
}
for ( int i = 0 ; i < median.size() - 1 ; i++)
{
System.out.print(median.get(i + 1 ) -
median.get(i) + " " );
}
}
public static void main(String[] args)
{
List<Integer> arr1 = new ArrayList<Integer>(){
{ add( 2 ); add( 4 ); add( 6 ); add( 8 ); add( 10 ); } };
List<Integer> arr2 = new ArrayList<Integer>(){
{ add( 4 ); add( 6 ); } };
medianChange(arr1, arr2);
}
}
|
Python3
def medianChange(arr1, arr2):
N = len (arr1)
median = []
if (N & 1 ):
median.append(arr1[N / / 2 ] * 1 )
else :
median.append((arr1[N / / 2 ] +
arr1[(N - 1 ) / / 2 ]) / / 2 )
for x in arr2:
it = arr1.index(x)
arr1.pop(it)
N - = 1
if (N & 1 ):
median.append(arr1[N / / 2 ] * 1 )
else :
median.append((arr1[N / / 2 ] +
arr1[(N - 1 ) / / 2 ]) / / 2 )
for i in range ( len (median) - 1 ):
print (median[i + 1 ] - median[i],
end = ' ' )
if __name__ = = "__main__" :
arr1 = [ 2 , 4 , 6 ,
8 , 10 ]
arr2 = [ 4 , 6 ]
medianChange(arr1, arr2)
|
C#
using System;
using System.Collections.Generic;
class GFG{
static void medianChange(List< int > arr1,
List< int > arr2)
{
int N = arr1.Count;
List< double > median = new List< double >();
if ((N & 1) != 0)
{
median.Add(arr1[N / 2] * 1.0);
}
else
{
median.Add((arr1[N / 2] +
arr1[(N - 1) / 2]) / 2.0);
}
foreach ( int x in arr2)
{
int it = arr1.IndexOf(x);
arr1.RemoveAt(it);
N--;
if ((N & 1) != 0)
{
median.Add(arr1[N / 2] * 1.0);
}
else
{
median.Add((arr1[N / 2] +
arr1[(N - 1) / 2]) / 2.0);
}
}
for ( int i = 0; i < median.Count - 1; i++)
{
Console.Write(median[i + 1] -
median[i] + " " );
}
}
static void Main()
{
List< int > arr1 = new List< int >(
new int []{ 2, 4, 6, 8, 10 });
List< int > arr2 = new List< int >(
new int []{ 4, 6 });
medianChange(arr1, arr2);
}
}
|
Javascript
<script>
function medianChange(arr1,arr2)
{
let N = arr1.length;
let median = [];
if ((N & 1))
median.push((arr1[(Math.floor(N / 2))] * 1));
else
median.push(Math.floor((arr1[(Math.floor(N / 2))] +
arr1[(Math.floor((N - 1) / 2))]) / 2));
for (let x = 0; x < arr2.length; x++)
{
let it = arr1.indexOf(arr2[x]);
arr1.splice(it,1);
N--;
if ((N & 1))
{
median.push(arr1[(Math.floor(N / 2))] * 1);
}
else
{
median.push(Math.floor((arr1[(Math.floor(N / 2))] +
arr1[(Math.floor((N - 1) / 2))]) / 2));
}
}
for (let i = 0; i < median.length - 1; i++)
{
document.write((median[i + 1] -
median[i]) + " " );
}
}
let arr1 = [2, 4, 6,
8, 10];
let arr2 = [4, 6];
medianChange(arr1, arr2)
</script>
|
Time Complexity: O(M*N)
Auxiliary Space: O(M * N)
Like Article
Suggest improvement
Share your thoughts in the comments
Please Login to comment...