Minimize increments or decrements by 2 to convert given value to a perfect square | Function to find the minimum number of operations required to make N a perfect square ; Stores count of operations by performing decrements ; Stores value of N ; Decrement the value of temp ; Stores square root of temp ; If temp is a perfect square ; Update temp ; Store count of operations by performing increments ; Increment the value of N ; Stores sqrt of N ; If N is a perfect square ; Update temp ; Return the minimum count ; Driver Code
function MinimumOperationReq ( N ) { let cntDecr = 0 ; let temp = N ; while ( temp > 0 ) { let X = Math . floor ( Math . sqrt ( temp ) ) ; if ( X * X == temp ) { break ; } temp = temp - 2 ; cntDecr += 1 ; } let cntIncr = 0 ; while ( true ) { let X = Math . floor ( Math . sqrt ( N ) ) ; if ( X * X == N ) { break ; } N = N + 2 ; cntIncr += 1 ; } return Math . min ( cntIncr , cntDecr ) ; } let N = 15 ; document . write ( MinimumOperationReq ( N ) ) ;