Find side of Square which makes minimal area to fit two identical rectangles inside it | javascript program of the above approach ; If ' a ' and ' b ' same then var ' 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 ; Driver code ; Size of rectangle
function minimalSquareSide ( a , b ) { if ( a == b ) { return 2 * a ; } if ( a != b ) { if ( a > b ) { var newB = b + b ; var diff = Math . 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 { var newA = a + a ; var diff = Math . 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 ; } } } return 0 ; } var H , W ; H = 3 ; W = 1 ; document . write ( minimalSquareSide ( H , W ) ) ;