Count pairs of similar rectangles possible from a given array | C ++ Program for the hashmap Approach ; Get the count of all pairs of similar rectangles ; Initialize the result value and map to store the ratio to the rectangles ; Calculate the rectangular ratio and save them ; Calculate pairs of similar rectangles from its common ratio ; Driver code
#include <iostream> NEW_LINE #include <unordered_map> NEW_LINE using namespace std ; int getCount ( int rows , int columns , int sides [ ] [ 2 ] ) { int ans = 0 ; unordered_map < double , int > umap ; for ( int i = 0 ; i < rows ; i ++ ) { double ratio = ( double ) sides [ i ] [ 0 ] / sides [ i ] [ 1 ] ; umap [ ratio ] ++ ; } for ( auto x : umap ) { int value = x . second ; if ( value > 1 ) { ans += ( value * ( value - 1 ) ) / 2 ; } } return ans ; } int main ( ) { int sides [ 4 ] [ 2 ] = { { 4 , 8 } , { 10 , 20 } , { 15 , 30 } , { 3 , 6 } } ; int rows = 4 ; int columns = 2 ; cout << getCount ( rows , columns , sides ) ; return 0 ; }