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
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:
#include<bits/stdc++.h>
using namespace std;
long long int NearestPerfectSquare(long long int N){
long long int abovenumBER = ceil(sqrt(N+1)) * ceil(sqrt(N+1));
long long int belownumBER = floor(sqrt(N-1)) * floor(sqrt(N-1));
long long int difference1 = N - abovenumBER;
long long int difference2 = belownumBER - N;
if(difference1 == difference2){
return belownumBER;
}else if(difference1 > difference2){
return abovenumBER;
}else{
return belownumBER;
}
}
int main(){
int T;
cin>>T;
while(T--){
long long int N;
cin>>N;
cout<<NearestPerfectSquare(N)<<endl;
}
return 0;
}
#include <bits/stdc++.h>
using namespace std;
bool isPerfectSquare(long long number)
{
long long root=sqrt(number);
return root*root==number;
}
int main() {
int testcases;
cin>>testcases;
while(testcases--)
{
long long n;
cin>>n;
long long smaller;
long long greater;
if(isPerfectSquare(n)==true)
{
smaller=sqrt(n)-1;
greater=sqrt(n)+1;
}
else
{
smaller=floor(sqrt(n));
greater=ceil(sqrt(n));
}
if(abs(smaller*smaller-n)<abs(greater*greater-n))
cout<<smaller*smaller<<endl;
else
cout<<greater*greater<<endl;
}
return 0;
}
import java.io.*;
import java.util.*;
class GFG {
public static void main (String[] args) {
Scanner sc=new Scanner(System.in);
int testcases=sc.nextInt();
while(testcases-->0)
{
long n=sc.nextLong();
long smaller;
long greater;
if(Geeks.isPerfectSquare(n)==true)
{
smaller=(long)Math.sqrt(n)-1;
greater=(long)Math.sqrt(n)+1;
}
else
{
smaller=(long)Math.floor(Math.sqrt(n));
greater=(long)Math.ceil(Math.sqrt(n));
}
if((long)Math.abs(smaller*smaller-n)<(long)Math.abs(greater*greater-n))
System.out.println(smaller*smaller);
else
System.out.println(greater*greater);
}
}
}
class Geeks
{
public static boolean isPerfectSquare(long number)
{
long root=(long)Math.sqrt(number);
return root*root==number;
}
}
import math
def ps(n):
num=int(math.sqrt(n))
return num*num==n
testcases=int(input())
while(testcases>0):
n=int(input())
smaller=0
greater=0
if(ps(n) is True):
smaller=int(math.sqrt(n))-1
greater=int(math.sqrt(n))+1
else:
smaller=math.floor(math.sqrt(n))
greater=math.ceil(math.sqrt(n))
if(abs(smaller*smaller-n)<abs(greater*greater-n)):
print(int(smaller*smaller))
else:
print(int(greater*greater))
testcases-=1
Happy Learning – If you require any further information, feel free to contact me.