Find minimum area of rectangle with given set of coordinates | C ++ Implementation of above approach ; function to find minimum area of Rectangle ; creating empty columns ; fill columns with coordinates ; check if rectangle can be formed ; Driver code
#include <bits/stdc++.h> NEW_LINE using namespace std ; int minAreaRect ( vector < vector < int > > A ) { map < int , vector < int > > columns ; for ( auto i : A ) columns [ i [ 0 ] ] . push_back ( i [ 1 ] ) ; map < pair < int , int > , int > lastx ; int ans = INT_MAX ; for ( auto x : columns ) { vector < int > column = x . second ; sort ( column . begin ( ) , column . end ( ) ) ; for ( int j = 0 ; j < column . size ( ) ; j ++ ) { for ( int i = 0 ; i < j ; i ++ ) { int y1 = column [ i ] ; if ( lastx . find ( { y1 , column [ j ] } ) != lastx . end ( ) ) { ans = min ( ans , ( x . first - lastx [ { y1 , column [ j ] } ] ) * ( column [ j ] - column [ i ] ) ) ; } lastx [ { y1 , column [ j ] } ] = x . first ; } } } if ( ans < INT_MAX ) return ans ; else return 0 ; } int main ( ) { vector < vector < int > > A = { { 1 , 1 } , { 1 , 3 } , { 3 , 1 } , { 3 , 3 } , { 2 , 2 } } ; cout << ( minAreaRect ( A ) ) ; return 0 ; }