Given two arrays x[] and y[] where x[i] represents the x coordinate and y[i] represent the corresponding y coordinate of a 2-D point, the task is to print the coordinate points in ascending order followed by their frequencies.
Examples:
Input: x[] = {1, 2, 1, 1, 1}, y[] = {1, 1, 3, 1, 3}
Output:
1 1 2
1 3 2
2 1 1
Input: x[] = {-1, 2, 1, -1, 2}, y[] = {-1, 1, -3, -1, 3}
Output:
-1 -1 2
1 -3 1
2 1 1
2 3 1
Approach: The idea is to use a map having key as pair (x[i], y[i]), and mapped value as integer frequency of the same point. Key-value will store the pair of coordinates and the mapped value will store their respective frequencies.
Below is the implementation of the above approach:
C++
// C++ implementation of the approach #include <bits/stdc++.h> using namespace std; // Function to print the coordinates along with // their frequency in ascending order void Print( int x[], int y[], int n) { // map to store the pairs // and their frequencies map<pair< int , int >, int > m; // Store the coordinates along // with their frequencies for ( int i = 0; i < n; i++) m[make_pair(x[i], y[i])]++; map<pair< int , int >, int >::iterator i; for (i = m.begin(); i != m.end(); i++) { cout << (i->first).first << " " << (i->first).second << " " << i->second << "\n" ; } } // Driver code int main() { int x[] = { 1, 2, 1, 1, 1 }; int y[] = { 1, 1, 3, 1, 3 }; int n = sizeof (x) / sizeof ( int ); Print(x, y, n); return 0; } |
Java
/*package whatever //do not write package name here */ import java.io.*; import java.util.*; class Pair { int x; int y; Pair( int x, int y) { this .x = x; this .y = y; } @Override public boolean equals(Object o) { if (o == this ) return true ; if (!(o instanceof Pair)) return false ; Pair p = (Pair) o; return x == p.x && y == p.y; } @Override public int hashCode() { return Objects.hash(x, y); } } class GFG { // Function to print the coordinates along with // their frequency in ascending order public static void print( int [] x, int [] y, int n) { // map to store the pairs // and their frequencies Map<Pair, Integer> m = new HashMap<>(); // Store the coordinates along // with their frequencies for ( int i = 0 ; i < n; i++) { Pair p = new Pair(x[i], y[i]); m.put(p, m.getOrDefault(p, 0 ) + 1 ); } // Sort the map by key and print the values Map<Pair, Integer> sortedMap = new TreeMap<>( new Comparator<Pair>() { @Override public int compare(Pair o1, Pair o2) { if (o1.x != o2.x) return o1.x - o2.x; return o1.y - o2.y; } }); sortedMap.putAll(m); // Traversing the whole map for (Map.Entry<Pair, Integer> entry : sortedMap.entrySet()) { Pair p = entry.getKey(); Integer count = entry.getValue(); System.out.println(p.x + " " + p.y + " " + count); } } // Driver code public static void main(String[] args) { int [] x = { 1 , 2 , 1 , 1 , 1 }; int [] y = { 1 , 1 , 3 , 1 , 3 }; int n = x.length; print(x, y, n); } } |
Python3
# Python3 implementation of the approach # Function to print the coordinates along with # their frequency in ascending order def Print (x, y, n): # map to store the pairs # and their frequencies m = dict () # Store the coordinates along # with their frequencies for i in range (n): m[(x[i], y[i])] = m.get((x[i], y[i]), 0 ) + 1 e = sorted (m) for i in e: print (i[ 0 ], i[ 1 ], m[i]) # Driver code x = [ 1 , 2 , 1 , 1 , 1 ] y = [ 1 , 1 , 3 , 1 , 3 ] n = len (x) Print (x, y, n) # This code is contributed # by mohit kumar |
C#
// C# implementation of // the above approach using System; using System.Collections.Generic; class GFG{ public class store : IComparer<KeyValuePair< int , int >> { public int Compare(KeyValuePair< int , int > x, KeyValuePair< int , int > y) { if (x.Key != y.Key) { return x.Key.CompareTo(y.Key); } else { return x.Value.CompareTo(y.Value); } } } // Function to print the // coordinates along with // their frequency in // ascending order static void Print( int []x, int []y, int n) { // Map to store the pairs // and their frequencies SortedDictionary<KeyValuePair< int , int >, int > m = new SortedDictionary<KeyValuePair< int , int >, int >( new store()); // Store the coordinates along // with their frequencies for ( int i = 0; i < n; i++) { KeyValuePair< int , int > tmp = new KeyValuePair< int , int >(x[i], y[i]); if (m.ContainsKey(tmp)) { m[tmp]++; } else { m[tmp] = 1; } } foreach (KeyValuePair<KeyValuePair< int , int >, int > i in m) { Console.Write(i.Key.Key + " " + i.Key.Value + " " + i.Value + "\n" ); } } // Driver code public static void Main( string [] args) { int []x = {1, 2, 1, 1, 1}; int []y = {1, 1, 3, 1, 3}; int n = x.Length; Print(x, y, n); } } // This code is contributed by rutvik_56 |
Javascript
// Function to print the coordinates along with their frequency in ascending order function printCoordinates(x, y, n) { // Map to store the pairs and their frequencies const m = new Map(); // Store the coordinates along with their frequencies for (let i = 0; i < n; i++) { const key = x[i] + ',' + y[i]; m.set(key, (m.get(key) || 0) + 1); } // Sort the coordinates in ascending order const sortedKeys = Array.from(m.keys()).sort(); // Print the coordinates along with their frequencies for (let i = 0; i < sortedKeys.length; i++) { const [x, y] = sortedKeys[i].split( ',' ); console.log(x, y, m.get(sortedKeys[i])); } } // Driver code const x = [1, 2, 1, 1, 1]; const y = [1, 1, 3, 1, 3]; const n = x.length; printCoordinates(x, y, n); |
1 1 2 1 3 2 2 1 1
Time Complexity: O(N * logN)
Auxiliary Space: O(N)
Ready to dive in? Explore our Free Demo Content and join our DSA course, trusted by over 100,000 neveropen!