Problem : Nearest Perfect Square
You are given a number N. You need to find the perfect square that is nearest to it. If two perfect squares are at the same distance to N, then print the greater perfect square.
Nearest Perfect Square Contest
Hint
Input:
The first line of input contains T denoting the number of testcases. T testcases follow. Each testcase contains one line of input containing N.
Output:
For each testcase, in a new line, print the perfect square closest to N.
Constraints:
1 <= T <= 100
1 <= N <= 1014
Examples:
Input:
3
1
56
100
Output:
0
49
81
Explanation:
Testcase1: 0 and 4 are near to 1. 0 is nearest.
Testcase2: 49 and 64 are near to 56. 49 is nearest.
Testcase3: 81 and 121 are near to 100. 81 is nearest.
Solution:
import java.io.*;
import java.util.*;
class GFG {
public static void main (String[] args) {
Scanner sc=new Scanner(System.in);
int T=sc.nextInt();
while(T-->0)
{
long N=sc.nextLong();
long Above;
long Below;
if(Geeks.isPerfectSquare(N)==true)
{
Below=(long)Math.sqrt(N)-1;
Above=(long)Math.sqrt(N)+1;
}
else
{
Below=(long)Math.floor(Math.sqrt(N));
Above=(long)Math.ceil(Math.sqrt(N));
}
if((long)Math.abs(Below*Below-N)<(long)Math.abs(Above*Above-N))
System.out.println(Below*Below);
else
System.out.println(Above*Above);
}
}
}
class Geeks
{
public static boolean isPerfectSquare(long number)
{
long root=(long)Math.sqrt(number);
return root*root==number;
}
}
import java.util.*;
class GFG {
public static void main (String[] args) {
Scanner sc=new Scanner(System.in);
int T=sc.nextInt();
while(T-->0)
{
long N=sc.nextLong();
long Above;
long Below;
if(Geeks.isPerfectSquare(N)==true)
{
Below=(long)Math.sqrt(N)-1;
Above=(long)Math.sqrt(N)+1;
}
else
{
Below=(long)Math.floor(Math.sqrt(N));
Above=(long)Math.ceil(Math.sqrt(N));
}
if((long)Math.abs(Below*Below-N)<(long)Math.abs(Above*Above-N))
System.out.println(Below*Below);
else
System.out.println(Above*Above);
}
}
}
class Geeks
{
public static boolean isPerfectSquare(long number)
{
long root=(long)Math.sqrt(number);
return root*root==number;
}
}