Check if a given number is a Perfect square using Binary Search | Function to check for perfect square number ; Find the mid value from start and last ; Check if we got the number which is square root of the perfect square number N ; If the square ( mid ) is greater than N it means only lower values then mid will be possibly the square root of N ; If the square ( mid ) is less than N it means only higher values then mid will be possibly the square root of N ; Driver code
function checkPerfectSquare ( N , start , last ) { let mid = parseInt ( ( start + last ) / 2 ) ; if ( start > last ) { return - 1 ; } if ( mid * mid == N ) { return mid ; } else if ( mid * mid > N ) { return checkPerfectSquare ( N , start , mid - 1 ) ; } else { return checkPerfectSquare ( N , mid + 1 , last ) ; } } let N = 65 ; document . write ( checkPerfectSquare ( N , 1 , N ) ) ;