Maximize count of unique Squares that can be formed with N arbitrary points in coordinate plane
Last Updated :
15 Sep, 2021
Given a positive integer N, the task is to find the maximum number of unique squares that can be formed with N arbitrary points in the coordinate plane.
Note: Any two squares that are not overlapping are considered unique.
Examples:
Input: N = 9
Output: 5
Explanation:
Consider the below square consisting of N points:
The squares ABEF, BCFE, DEHG, EFIH is one of the possible squares of size 1 which are non-overlapping with each other.
The square ACIG is also one of the possible squares of size 2.
Input: N = 6
Output: 2
Approach: This problem can be solved based on the following observations:
- Observe that if N is a perfect square then the maximum number of squares will be formed when sqrt(N)*sqrt(N) points form a grid of sqrt(N)*sqrt(N) and all of them are equally spaces.
- But when N is not a perfect square, then it still forms a grid but with the greatest number which is a perfect square having a value less than N.
- The remaining coordinates can be placed around the edges of the grid which will lead to maximum possible squares.
Follow the below steps to solve the problem:
- Initialize a variable, say ans that stores the resultant count of squares formed.
- Find the maximum possible grid size as sqrt(N) and the count of all possible squares formed up to length len to the variable ans which can be calculated by .
- Decrement the value of N by len*len.
- If the value of N is at least len, then all other squares can be formed by placing them in another cluster of points. Find the count of squares as calculated in Step 2 for the value of len.
- After completing the above steps, print the value of ans as the result.
Below is the implementation of the above approach:
C++
#include <bits/stdc++.h>
using namespace std;
int maximumUniqueSquares( int N)
{
int ans = 0;
if (N < 4) {
return 0;
}
int len = ( sqrt ( double (N)));
N -= len * len;
for ( int i = 1; i < len; i++) {
ans += i * i;
}
if (N >= len) {
N -= len;
for ( int i = 1; i < len; i++) {
ans += i;
}
}
for ( int i = 1; i < N; i++) {
ans += i;
}
return ans;
}
int main()
{
int N = 9;
cout << maximumUniqueSquares(N);
return 0;
}
|
Java
import java.io.*;
class GFG {
static int maximumUniqueSquares( int N)
{
int ans = 0 ;
if (N < 4 ) {
return 0 ;
}
int len = ( int )(Math.sqrt(N));
N -= len * len;
for ( int i = 1 ; i < len; i++) {
ans += i * i;
}
if (N >= len) {
N -= len;
for ( int i = 1 ; i < len; i++) {
ans += i;
}
}
for ( int i = 1 ; i < N; i++) {
ans += i;
}
return ans;
}
public static void main (String[] args)
{
int N = 9 ;
System.out.println( maximumUniqueSquares(N));
}
}
|
Python3
import math
def maximumUniqueSquares(N):
ans = 0
if N < 4 :
return 0
len = int (math.sqrt(N))
N - = len * len
for i in range ( 1 , len ):
ans + = i * i
if (N > = len ):
N - = len
for i in range ( 1 , len ):
ans + = i
for i in range ( 1 , N):
ans + = i
return ans
if __name__ = = "__main__" :
N = 9
print (maximumUniqueSquares(N))
|
C#
using System;
public class GFG
{
static int maximumUniqueSquares( int N)
{
int ans = 0;
if (N < 4) {
return 0;
}
int len = ( int )(Math.Sqrt(N));
N -= len * len;
for ( int i = 1; i < len; i++) {
ans += i * i;
}
if (N >= len) {
N -= len;
for ( int i = 1; i < len; i++) {
ans += i;
}
}
for ( int i = 1; i < N; i++) {
ans += i;
}
return ans;
}
public static void Main ( string [] args)
{
int N = 9;
Console.WriteLine( maximumUniqueSquares(N));
}
}
|
Javascript
<script>
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));
</script>
|
Time Complexity: O(sqrt(N))
Auxiliary Space: O(1)
Share your thoughts in the comments
Please Login to comment...