- Related Questions & Answers
- Print numbers in descending order along with their frequencies
- Print characters and their frequencies in order of occurrence in C++
- Print the corner elements and their sum in a 2-D matrix in C Program.
- Print a 2 D Array or Matrix in C#
- Print characters having odd frequencies in order of occurrence in C++
- Print an array with numbers having 1, 2 and 3 as a digit in ascending order
- Print a 2 D Array or Matrix in Java Programming
- Sort array of points by ascending distance from a given point JavaScript
- MySQL Order by a specific column x and display remaining values in ascending order
- Print level order traversal line by line in C++ Programming.
- Print n smallest elements from given array in their original order
- MySQL command to order timestamp values in ascending order?
- How to ORDER BY last 2 character string in MySQL?
- Order MySQL records randomly and display name in Ascending order
- Program to find line passing through 2 Points in C++

- Selected Reading
- UPSC IAS Exams Notes
- Developer's Best Practices
- Questions and Answers
- Effective Resume Writing
- HR Interview Questions
- Computer Glossary
- Who is Who

In this problem, we are given 2 arrays x[] , y[] such that (x,y) gives a cordinate of a point in 2D plane. Our task is to print all points along with their frequencies of occurence.

Let’s take an example to understand the problem

Input: x[]={0, 1, 1, 0, 0} ; y[]={1, 2, 2, 2, 1} Output (0, 1) = 2 (1, 2) = 2 (0, 2) = 1

To solve this problem, we need to store frequency of occurence of each point. So this we need to use map data-structure. The key of the map is (x[i], y[i]), mapped value is integer frequency of occurence.

The program will show the implementation of our solution,

#include <bits/stdc++.h> using namespace std; void printFrequencyofPoint(int x[], int y[], int n){ map<pair<int, int>, int> pFreq; for (int i = 0; i < n; i++) pFreq[make_pair(x[i], y[i])]++; map<pair<int, int>, int>::iterator i; for (i = pFreq.begin(); i != pFreq.end(); i++) { cout<<"("<<(i->first).first <<", "<< (i->first).second <<") -> "; cout<<i->second << "\n"; } } int main() { int x[]={0, 1, 1, 0, 0}; int y[]={1, 2, 2, 2, 1}; int n=5; cout<<"The points and their frequency of occurance is :\n"; printFrequencyofPoint(x, y, n); return 0; }

The points and their frequency of occurance is : (0, 1) -> 2 (0, 2) -> 1 (1, 2) -> 2

Advertisements