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 n, vector<pair<int, int>> &arr)
{
    map<pair<int, int>, int> mp;
    for (int i = 0; i < n; i++)
    {
        int x = arr[i].first, y = arr[i].second;

        mp[{x, y}]++;
    }
    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<int, int>> arr = {{1, 1},
                                  {2, 1},
                                  {1, 3},
                                  {1, 1},
                                  {1, 3}};

    nCoordinates(n, arr);

    return 0;
}


No comments:

Post a Comment