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
def bound ( w , h , N , x ) : NEW_LINE INDENT val = ( x // w ) * ( x // h ) NEW_LINE if ( val >= N ) : NEW_LINE INDENT return True NEW_LINE DEDENT else : NEW_LINE INDENT return False NEW_LINE DEDENT DEDENT def FindSquare ( N , W , H ) : NEW_LINE INDENT i = 1 NEW_LINE j = W * H * N NEW_LINE while ( i < j ) : NEW_LINE INDENT mid = i + ( j - i ) // 2 NEW_LINE if ( bound ( W , H , N , mid ) ) : NEW_LINE INDENT j = mid NEW_LINE DEDENT else : NEW_LINE INDENT i = mid + 1 NEW_LINE DEDENT DEDENT return j NEW_LINE DEDENT W = 2 NEW_LINE H = 3 NEW_LINE N = 10 NEW_LINE print ( FindSquare ( N , W , H ) ) NEW_LINE