Given N numbers in an array. Your task is to keep at-most K numbers at the top (According to their frequency). We basically need to print top k numbers when input stream has included k distinct elements, else need to print all distinct elements sorted by frequency. - Codeprg

Breaking

programing News Travel Computer Engineering Science Blogging Earning

Tuesday, 14 July 2020

Given N numbers in an array. Your task is to keep at-most K numbers at the top (According to their frequency). We basically need to print top k numbers when input stream has included k distinct elements, else need to print all distinct elements sorted by frequency.

Given N numbers in an array. Your task is to keep at-most K numbers at the top (According to their frequency).
Given N numbers in an array. Your task is to keep at-most K numbers at the top (According to their frequency)

Given 
N numbers in an array. Your task is to keep at-most K numbers at the top (According to their frequency).  We basically need to print top k numbers when the input stream has included k distinct elements, else need to print all distinct elements sorted by frequency.

Single line output, print the atmost top K elements of the array.

Constraints:
1 <= T <= 103
1 <= N, K <= 106

Example:
Input:

2
5 4
5 2 1 3 2
5 4
5 2 1 3 4
Output:
5 2 5 1 2 5 1 2 3 5 2 1 3 5 
5 2 5 1 2 5 1 2 3 5 1 2 3 4 

Explanation:
For 1st test case:
arr[] = 5 2 1 3 2
Firstly their was 5 whose frequency is max till now. so print 5.
Then 2, which is smaller than 5 but their frequency is the same so print 2 5.
Then 1, Which is the smallest among all the numbers arrived, so print 1 2 5.
Then 3, so print 1 2 3 5
Then again 2, which has the highest frequency among all numbers so 2 1 3 5.

For the 2nd test case:
arr[] = 5 2 1 3 4
Firstly there was 5 whose frequency is max till now. so print 5.
Then 2, which is smaller than 5 but their frequency is the same so print 2 5.
Then 1, Which is the smallest among all the numbers arrived, so print 1 2 5.
Then 3, so print 1 2 3 5.


#include<bits/stdc++.h>

using namespace std;

bool comp(const pair < int, int > & a,

    const pair < int, int > & b) {

//sorting on basis of frequency decending order

//if frequency is same for both then 

    if (a.second == b.second)

        return (a.first < b.first);

    else

        return (a.second > b.second);

}

int main() {

    //code

    int t;

    cin >> t;

    while (t--) {

        int n, m, l, temp;

        cin >> n >> m;

        vector < pair < int, int > > vp;

        vector < pair < int, int > > ::iterator it;

        int j;

        for (int i = 0; i < n; ++i) {

            cin >> temp;

            l = 1;

            for (it = vp.begin(); it != vp.end(); ++it) {

                if (it - > first == temp) {

                    it - > second++,l = 0;

                    break;

                }

            }


            if (l) {

                vp.push_back(make_pair(temp, 1));

            }

            sort(vp.begin(), vp.end(), comp);

            it = vp.begin(),j = 0;

            while (it < vp.end() && j < m) {

                cout << it - > first << " ";

                ++j, ++it;

            }

        }

        cout << endl;

    }

    return 0;

}