Size of smallest square that contains N non | Function to check if side of square X can pack all the N rectangles or not ; Find the number of rectangle it can pack ; If val is atleast N , then return true ; Otherwise , return false ; Function to find the size of the smallest square that can contain N rectangles of dimensions W * H ; Stores the lower bound ; Stores the upper bound ; Iterate until i is less than j ; Calculate the mid value ; If the current size of square cam contain N rectangles ; Otherwise , update i ; Return the minimum size of the square required ; Driver code ; Function Call
function bound ( w , h , N , x ) { let val = parseInt ( x / w ) * parseInt ( x / h ) ; if ( val >= N ) return true ; else return false ; } function FindSquare ( N , W , H ) { let i = 1 ; let j = W * H * N ; while ( i < j ) { let mid = i + parseInt ( ( j - i ) / 2 ) ; if ( bound ( W , H , N , mid ) ) j = mid ; else i = mid + 1 ; } return j ; } let W = 2 ; let H = 3 ; let N = 10 ; document . write ( FindSquare ( N , W , H ) ) ;