N - Co Ordinates (Map Practice)

There are 'N' 2-D co-ordinate points given. Now, you have to print the co-ordinate point, followed by their frequencies, See sample I/O,

Print the coordinate points in the Lexicographical order.

Example:

Input:  n = 5, arr={{1, 1}, {2, 1}, {1, 3}, {1, 1}, {1, 3}}

Output:

1 1 2 1 3 2 2 1 1

Approach:

C++

#include <bits/stdc++.h>
using namespace std;

void nCoordinates(int nvector<pair<intint>> &arr)
{
    map<pair<intint>, intmp;
    for (int i = 0i < ni++)
    {
        int x = arr[i].firsty = arr[i].second;

        mp[{xy}]++;
    }
    for (auto it = mp.begin(); it != mp.end(); it++)
        cout << it->first.first << " " << 
it->first.second << " " << it->second << "\n";
}
int main()
{
    int n = 5;

    vector<pair<intint>> arr = {{11},
                                  {21},
                                  {13},
                                  {11},
                                  {13}};

    nCoordinates(narr);

    return 0;
}


No comments:

Post a Comment