A median in an array with the length of n is an element which occupies position number (n+1)/2 after we sort the elements in the non-decreasing order (the array elements are numbered starting with 1). A median of an array (2, 6, 1, 2, 3) is the number 2, and a median of array (0, 96, 17, 23) — the number 17.
Input : 3 10 10 20 30 Output : 1 In the first sample we can add number 9 to array (10, 20, 30). The resulting array (9, 10, 20, 30) will have a median in position (4+1)/2 = 2, that is, 10 Input : 3 4 1 2 3 Output : 4 In the second sample you should add numbers 4, 5, 5, 5. The resulting array has median equal to 4.
First Approach:- The approach is to add one more number x to the array until the median of the array equals to x. Below is the implementation of the above approach:-
# Python 3 program to find minimum number
# of elements needs to add to the
# array so that its median equals x.
# Returns count of elements to be added
# to make median x. This function
# assumes that a has enough extra space.
def minNumber(a, n, x):
# to sort the array in increasing order.
a.sort(reverse = False)
k = 0
while(a[int((n – 1) / 2)] != x):
a[n – 1] = x
n += 1
a.sort(reverse = False)
k += 1
# Driver code
if __name__ == ‘__main__’:
x = 10
a = [10, 20, 30]
n = 3
print(minNumber(a, n, x))
# This code is contributed by
Time complexity : O(knLogn)
Second Approach:- Better approach is to count all the elements equal to x(that is e), greater than x(that is h) and smaller than x(that is l). And then –
if l is greater than h then, the ans will be (l – h) + 1 – e;
And if h is greater than l then, ans will be (h – l – 1) + 1 – e;
We can use Hoare’s partition scheme to count smaller, equal and greater elements.
Below is the implementation of the above approach:
Time complexity : O(n)
This article is contributed by Sagar Shukla. If you like GeeksforGeeks and would like to contribute, you can also write an article using contribute.geeksforgeeks.org or mail your article to firstname.lastname@example.org. See your article appearing on the GeeksforGeeks 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.
- Minimum Increment / decrement to make array elements equal
- Minimum operations of given type to make all elements of a matrix equal
- Find whether it is possible to make array elements same using one external number
- Minimum number of subtract operation to make an array decreasing
- Minimum number of subsets with distinct elements
- Minimum number of distinct elements after removing m items
- Minimum number of increment/decrement operations such that array contains all elements from 1 to N
- Minimum swaps to make two arrays identical
- Find minimum difference between any two elements
- Minimum sum after subtracting multiples of k from the elements of the array
- Making elements of two arrays same with minimum increment/decrement
- Choose k array elements such that difference of maximum and minimum is minimized
- Maximum number of partitions that can be sorted individually to make sorted
- Mean and Median of a matrix