Median after K additional integers | CPP program to find median of an array when we are allowed to add additional K integers to it . ; Find median of array after adding k elements ; sorting the array in increasing order . ; printing the median of array . Since n + K is always odd and K < n , so median of array always lies in the range of n . ; driver function
#include <bits/stdc++.h> NEW_LINE using namespace std ; void printMedian ( int arr [ ] , int n , int K ) { sort ( arr , arr + n ) ; cout << arr [ ( n + K ) / 2 ] ; } int main ( ) { int arr [ ] = { 5 , 3 , 2 , 8 } ; int k = 3 ; int n = sizeof ( arr ) / sizeof ( arr [ 0 ] ) ; printMedian ( arr , n , k ) ; return 0 ; }