Given an array of n integers. We are allowed to add k additional integer in the array and then find the median of the resultant array. We can choose any k values to be added. 
Constraints: 
 
k < n n + k is always odd.
Examples : 
 
Input : arr[] = { 4, 7 }
         k = 1 
Output : 7
Explanation : One of the possible solutions 
is to add 8 making the array [4, 7, 8], whose
median is 7
Input : arr[] = { 6, 1, 1, 1, 1 }
         k = 2
Output : 1
Explanation : No matter what elements we add 
to this array, the median will always be 1
We first sort the array in increasing order. Since value of k is less than n and n+k is always odd, we can always choose to add k elements that are greater than the largest element of an array, and (n+k)/2th element is always a median of the array. 
 
C++
| // CPP program to find median of an array when // we are allowed to add additional K integers // to it. #include <bits/stdc++.h> usingnamespacestd;  // Find median of array after adding k elements voidprintMedian(intarr[], intn, intK) {     // sorting  the array in increasing order.     sort(arr, arr + n);      // 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.     cout << arr[(n + K) / 2]; }  // driver function intmain() {     intarr[] = { 5, 3, 2, 8 };     intk = 3;     intn = sizeof(arr) / sizeof(arr[0]);     printMedian(arr, n, k);     return0; }  | 
Java
| // Java program to find median of an array when // we are allowed to add additional K integers // to it. importjava.util.Arrays;  classGFG {          // Find median of array after adding k elements     staticvoidprintMedian(intarr[], intn, intK)     {                  // sorting the array in increasing order.         Arrays.sort(arr);              // 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.         System.out.print(arr[(n + K) / 2]);     }          //Driver code     publicstaticvoidmain (String[] args)     {                  intarr[] = { 5, 3, 2, 8};         intk = 3;         intn = arr.length;                  printMedian(arr, n, k);     } }  // This code is contributed by Anant Agarwal.  | 
Python3
| # Python3 code 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 defprintMedian (arr, n, K):          # sorting the array in      # increasing order.     arr.sort()          # 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.     print( arr[int((n +K) /2)])  # driver function arr =[ 5, 3, 2, 8] k =3n =len(arr) printMedian(arr, n, k)  # This code is contributed by "Sharad_Bhardwaj".  | 
C#
| // C# program to find median of an array when // we are allowed to add additional K integers // to it. usingSystem;  classGFG {     // Find median of array after adding k elements     staticvoidprintMedian(int[]arr, intn, intK)     {         // sorting  the array in increasing order.         Array.Sort(arr);               // 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.         Console.Write(arr[(n + K) / 2]);     }          //Driver code     publicstaticvoidMain ()     {     int[]arr = { 5, 3, 2, 8 };         intk = 3;         intn = arr.Length;         printMedian(arr, n, k);     } }  // This code is contributed by  anant321.  | 
PHP
| <?php // PHP 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 functionprintMedian($arr, $n, $K) {     // sorting the array      // in increasing order.     sort($arr);      // 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.     echo$arr[($n+ $K) / 2]; }  // Driver Code $arr= array( 5, 3, 2, 8 ); $k= 3; $n= count($arr); printMedian($arr, $n, $k);  // This code is contributed by Sam007 ?>  | 
Javascript
| <script>  // Javascript 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     functionprintMedian(arr, n, K)     {                    // sorting the array in increasing order.         arr.sort();                // 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.         document.write(arr[(Math.floor((n + K) / 2))]);     }  // driver program           let arr = [ 5, 3, 2, 8 ];         let k = 3;         let n = arr.length;                    printMedian(arr, n, k);              </script>  | 
Output :
8
Time complexity: O(nlog(n))
Auxiliary Space: O(1)
 If you like neveropen and would like to contribute, you can also write an article using write.geeksforgeeks.org or mail your article to review-team@geeksforgeeks.org. See your article appearing on the neveropen main page and help other Geeks.
Please write comments if you find anything incorrect, or you want to share more information about the topic discussed above.
 
Ready to dive in? Explore our Free Demo Content and join our DSA course, trusted by over 100,000 neveropen!


 
                                    







