Find side of Square which makes minimal area to fit two identical rectangles inside it | C ++ program of the above approach ; if ' a ' and ' b ' same then double ' a ' or ' b ' and return ( 2 * a ) or ( 2 * b ) ; check if a != b ; if a > b ; double the smaller value that is ' b ' and store it to ' newB ' ; find the difference of ' newB ▁ and ▁ ' a ' ; if ' newB ' < a ; then add the difference of ' newB ' and ' a ' to the ' b ' to make ' b ' and ' a ' as same ; return side of the square a or b ; if ' newB ' > a then then add the difference of ' newB ' and ' a ' to the ' a ' to make ' a ' and ' newB ' as same ; return side of the square a or newB ; if a < b ; double the smaller value that is ' a ' and store it to ' newA ' ; find the difference of ' newA ▁ and ▁ ' b ' ; if ' newA ' < b ; then add the difference of ' newA ' and ' b ' to the ' a ' to make ' a ' and ' b ' as same ; return side of the square a or b ; if ' newA ' > b then then add the difference of ' newA ' and ' b ' to the ' b ' to make ' b ' and ' newA ' as same ; return side of the square b or newA ; Drive Code ; Size of rectangle
#include <bits/stdc++.h> NEW_LINE using namespace std ; int minimalSquareSide ( int a , int b ) { if ( a == b ) { return 2 * a ; } if ( a != b ) { if ( a > b ) { int newB = b + b ; int diff = abs ( newB - a ) ; if ( newB < a ) { b = newB + diff ; if ( a == b ) return a ; return 0 ; } else { a = a + diff ; if ( a == newB ) return a ; return 0 ; } } else { int newA = a + a ; int diff = abs ( newA - b ) ; if ( newA < b ) { a = diff + newA ; if ( a == b ) return a ; return 0 ; } else { b = b + diff ; if ( b == newA ) return b ; return 0 ; } } } } int main ( ) { int H , W ; H = 3 , W = 1 ; cout << minimalSquareSide ( H , W ) << endl ; return 0 ; }