The Wayback Machine - https://web.archive.org/web/20220412113057/https://www.geeksforgeeks.org/find-median-of-the-array-formed-from-given-frequencies-of-elements/
Skip to content
Related Articles

Related Articles

Improve Article
Save Article
Like Article

Find Median of the Array formed from given frequencies of elements

  • Last Updated : 12 Apr, 2022

Given an array A[] containing N elements of an array and their frequency in that array (say arr[]), the task is to find the median of the array whose elements and frequency are given.

Note: Median of an array is the element at the middle of the sorted array

Examples:

Input: A[] = { {1, 2}, {4, 2}, {5, 1} }
Output: 4 
Explanation: The array whose elements are given is {1, 1, 4, 4, 5}. 
Therefore, the median of the array will be 4.

Input: A[] = { {3, 4}, {2, 3}, {9, 2} }
Output: 3
Explanation: The newly created array will be {2, 2, 2, 3, 3, 3, 3, 9, 9}. 
Therefore the median of the array will be 3.

 

Naive Approach: The basic approach is to create the array and then sort the array and find the middle element of that array.

Time Complexity: O(M * log M)  where M is the sum of the frequencies of all elements given in A[].
Auxiliary Space: O(M)

Efficient approach: As the sum of frequencies of the elements present in A[] can be very large it is not feasible to build an array. This can be solved efficiently based on the following idea: 

Sort the array A[] based on the value of elements. Now calculate the total number of elements that will be in the array formed from these elements (say M). The elemnt at M/2 th position is the median.
So iterate from the minimum elements and with the help of their frequencies find out the element and M/2 th position.

Follow the below steps to implement the above idea:

  • Insert all the elements in a map with the elements as the key and their frequency as value (a map is sorted based on the value of the key. Therefore it satisfies the need of sorting).
  • Count total elements that will be in the array. 
  • Iterate from the minimum elements and check if the total elements till the current value is the same as M/2:
    • If it is same, then the current element is the required median.
    • Otherwise, increase the total number of elements till now.
  • Return the median.

Below is the implementation of the above approach.

C++




// C++ code to implement the approach
  
#include <bits/stdc++.h>
using namespace std;
  
// Find median of the newly created array
int findMedian(vector<vector<int> > a, int n)
{
    map<int, int> m;
  
    // Size of the newly created array
    int totalsize = 0;
  
    // Put all elements in the map
    for (int i = 0; i < n; i++) {
        int val = a[i][0];
        int time = a[i][1];
        m[val] += time;
        totalsize += time;
    }
  
    // Find the element present at the middle
    // of the newly created array
    int meidanpos = totalsize / 2;
    long long pos = 0;
    for (auto it : m) {
  
        // If the pos + current element times
        // is greater than medianpos
        // then return current element
        if (pos + it.second > meidanpos) {
            return it.first;
        }
        else {
            pos += it.second;
        }
    }
}
  
// Driver Code
int main()
{
    vector<vector<int> > A;
    A = { { 1, 2 }, { 4, 2 }, { 5, 1 } };
    int N = A.size();
  
    // Function call
    cout << findMedian(A, N);
    return 0;
}
Output
4

Time Complexity: O(N * logN)
Auxiliary Space: O(N)


My Personal Notes arrow_drop_up
Recommended Articles
Page :

Start Your Coding Journey Now!