Maximize count of unique Squares that can be formed with N arbitrary points in coordinate plane | Function to find the maximum number of unique squares that can be formed from the given N points ; Stores the resultant count of squares formed ; Base Case ; Subtract the maximum possible grid size as sqrt ( N ) ; Find the total squares till now for the maximum grid ; A i * i grid contains ( i - 1 ) * ( i - 1 ) + ( i - 2 ) * ( i - 2 ) + ... + 1 * 1 squares ; When N >= len then more squares will be counted ; Return total count of squares ; Driver Code
function maximumUniqueSquares ( N ) { var ans = 0 ; var i ; if ( N < 4 ) { return 0 ; } var len = Math . sqrt ( N ) ; N -= len * len ; for ( i = 1 ; i < len ; i ++ ) { ans += i * i ; } if ( N >= len ) { N -= len ; for ( i = 1 ; i < len ; i ++ ) { ans += i ; } } for ( i = 1 ; i < N ; i ++ ) { ans += i ; } return ans ; } var N = 9 ; document . write ( maximumUniqueSquares ( N ) ) ;