Given two arrays arr[] of N integers and W[] of N weights where W[i] is the weight for the element arr[i]. The task is to find the weighted median of the given array.
Note: The sum of the weight of all elements will always be 1.
Let the array arr[] be arranged in increasing order with their corresponding weights.
If N is odd, then there is only one weighted median say arr[k] which satisfies the below property:
If N is even, then there are two weighted medians, i.e., lower and upper weighted median.
The lower weighted median for element arr[k] which satisfies the following:
The upper weighted median for element arr[k] which satisfies the following:
Examples:
Input: arr={5, 1, 3, 2, 4}, W=[0.25, 0.15, 0.2, 0.1, 0.3]
Output: The weighted median is element 4
Explanation:
Here the number of element is odd, so there is only one weighted median because at K = 3 the above condition is satisfied.
The cumulative weights on each side of element 4 is 0.45 and 0.25.Input: arr=[4, 1, 3, 2], W=[0.25, 0.49, 0.25, 0.01]
Output:
The lower weighted median is element 2
The upper weighted median is element 3
Explanation:
Here there are an even number of elements, so there are two weighted medians.
Lower weighted median is at K = 2 because at K = 2 the above condition is satisfied with cumulative weight on each side of element 2 is 0.49 and 0.5.
Upper weighted median is at K = 3 because at K = 3 the above condition is satisfied with cumulative weight on each side of element 3 is 0.5 and 0.25.
Approach: Follow the steps below to solve the given problem:
- Now to find the median of the array arr[] in increasing order with their respective order of weight shouldn’t be changed.
- So, create a set of pairs where the first element of the pair will be arr[i] and the second element of the pair will be its corresponding weights W[i].
- Then sort the set of Pairs according to the arr[] values.
- If the number of pairs is odd, then find the weighted median as:
- Traverse over the set of pairs and compute sum by adding weights.
- When the sum becomes greater than 0.5 print the arr[i] value of that Pair.
- But, if the number of pairs is even, then find both lower and upper weighted medians:
- For the lower median traverse over the set pairs from the left and compute sum by adding weights.
- When the sum becomes greater than or equal to 0.5 print the arr[i] value of that Pair.
- For the upper median traverse over the set pairs from the right and compute sum by adding weights.
- When the sum becomes greater than or equal to 0.5 print the arr[i] value of that Pair.
Below is the implementation of the above approach:
Python3
# Python program for the above approach # Function to calculate weighted median def weightedMedian(arr, W): # Store pairs of arr[i] and W[i] pairs = [] for index in range(len(arr)): pairs.append([arr[index], W[index]]) # Sort the list of pairs w.r.t. # to their arr[] values pairs.sort(key = lambda p: p[0]) # If N is odd if len(arr) % 2 != 0: # Traverse the set pairs # from left to right sums = 0 for element, weight in pairs: # Update sums sums += weight # If sum becomes > 0.5 if sums > 0.5: print("The Weighted Median", end = ' ') print("is element {}".format(element)) # If N is even else: # For lower median traverse # the set pairs from left sums = 0 for element, weight in pairs: # Update sums sums += weight # When sum >= 0.5 if sums >= 0.5: print("Lower Weighted Median", end = ' ') print("is element {}".format(element)) break # For upper median traverse # the set pairs from right sums = 0 for index in range(len(pairs)-1, -1, -1): element = pairs[index][0] weight = pairs[index][1] # Update sums sums += weight # When sum >= 0.5 if sums >= 0.5: print("Upper Weighted Median", end = ' ') print("is element {}".format(element)) break # Driver Code if __name__ == "__main__": # Given array arr[] arr = [4, 1, 3, 2] # Given weights W[] W = [0.25, 0.49, 0.25, 0.01] # Function Call weightedMedian(arr, W) |
Lower Weighted Median is element 2 Upper Weighted Median is element 3
Time Complexity: O(N log N)
Auxiliary Space: O(N)
Attention reader! Don’t stop learning now. Get hold of all the important DSA concepts with the DSA Self Paced Course at a student-friendly price and become industry ready.
Recommended Posts:
- Program for weighted mean of natural numbers.
- Change in Median of given array after deleting given elements
- Maximize the median of the given array after adding K elements to the same array
- Count the nodes of the given tree whose weighted string is a palindrome
- Minimum difference between any two weighted nodes in Sum Tree of the given Tree
- Minimum cost to connect weighted nodes represented as array
- Program for Mean and median of an unsorted array
- Find the length of the median of a Triangle if length of sides are given
- Find the median array for Binary tree
- Find K elements whose absolute difference with median of array is maximum
- Count distinct median possible for an Array using given ranges of elements
- Count of Array elements greater than or equal to twice the Median of K trailing Array elements
- Probability of a random pair being the maximum weighted pair
- Determining the inconsistently weighted object
- Count the nodes of a tree whose weighted string does not contain any duplicate characters
- Weighted Sum Method - Multi Criteria Decision Making
- Minimum Cost of Simple Path between two nodes in a Directed and Weighted Graph
- Find median of BST in O(n) time and O(1) space
- Find the number of sub arrays in the permutation of first N natural numbers such that their median is M
- Find last two remaining elements after removing median of any 3 consecutive elements repeatedly
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.

