Count of pair of integers (x , y) such that difference between square of x and y is a perfect square
Last Updated :
16 Mar, 2023
Given an integer N. The task is to find the number of pairs of integers (x, y) both less than N and greater than 1, such that x2 – y is a square number or 0.
Example:
Input: N = 3
Output: 2
Explanation:
The only possible valid pairs are (1, 1), (2, 3). Therefore, the count of such pairs is 2.
Input: N = 2
Output: 1
Naive Approach: The simplest approach to solve the given problem is to generate all possible pairs of integers (x, y) over the range [1, N] and then check that if the value of (x2 – y) is a perfect square or not. If found to be true, then count this pair. After checking for all the possible, print the total count obtained.
C++
#include <bits/stdc++.h>
using namespace std;
bool checkperfectsquare( int n)
{
if ( ceil (( double ) sqrt (n)) == floor (( double ) sqrt (n))) {
return true ;
}
else {
return false ;
}
}
int countPairs( int N)
{
int count=0;
for ( int x = 1; x <= N; x++) {
for ( int y = 1; y <= N; y++) {
int diff = x * x - y;
if (checkperfectsquare(diff))
{
count++;
}
}
}
cout << count << endl;
}
int main() {
int N =3;
countPairs(N);
return 0;
}
|
Java
import java.util.*;
public class Main {
public static boolean checkperfectsquare( int n)
{
if (Math.ceil(Math.sqrt(n))
== Math.floor(Math.sqrt(n))) {
return true ;
}
else {
return false ;
}
}
public static int countPairs( int N)
{
int count = 0 ;
for ( int x = 1 ; x <= N; x++) {
for ( int y = 1 ; y <= N; y++) {
int diff = x * x - y;
if (checkperfectsquare(diff)) {
count++;
}
}
}
System.out.println(count);
return count;
}
public static void main(String[] args)
{
int N = 3 ;
countPairs(N);
}
}
|
Python3
import math
def checkperfectsquare(n):
if n < 0 :
return False
if math.ceil(math.sqrt(n)) = = math.floor(math.sqrt(n)):
return True
else :
return False
def countPairs(N):
count = 0
for x in range ( 1 ,N + 1 ):
for y in range ( 1 ,N + 1 ):
diff = x * x - y
if checkperfectsquare(diff):
count + = 1
print (count)
if __name__ = = '__main__' :
N = 3
countPairs(N)
|
C#
using System;
using System.Collections.Generic;
class Gfg{
static bool checkperfectsquare( int n)
{
if (Math.Ceiling(( double )Math.Sqrt(n)) == Math.Floor(( double )Math.Sqrt(n))) {
return true ;
}
else {
return false ;
}
}
static void countPairs( int N)
{
int count=0;
for ( int x = 1; x <= N; x++) {
for ( int y = 1; y <= N; y++) {
int diff = x * x - y;
if (checkperfectsquare(diff))
{
count++;
}
}
}
Console.WriteLine(count);
}
public static void Main(String[] args)
{
int N =3;
countPairs(N);
}
}
|
Javascript
function checkperfectsquare(n)
{
if (Math.ceil(Math.sqrt(n)) == Math.floor(Math.sqrt(n))) {
return true ;
} else {
return false ;
}
}
function countPairs(N)
{
let count = 0;
for (let x = 1; x <= N; x++) {
for (let y = 1; y <= N; y++) {
let diff = x * x - y;
if (checkperfectsquare(diff)) {
count++;
}
}
}
console.log(count);
}
const N = 3;
countPairs(N);
|
Time Complexity: O(N2 *LogN)
Auxiliary Space: O(1)
Efficient Approach: The above approach can also be optimized by the following observation:
x2-y is a square of a number, let’s say square of z.
x2 – y = z2
x2 – z2 = y
( x + z ) * ( x – z ) = y
Now, let x + z = p and x – z = q
p * q = y
So, the problem gets reduced to count the pairs of p, q instead of x, y.
Now, as y can only be in the range of 1 to N
So, p*q will also be in the range from 1 to N. And as p>=q ( because x+z >= x-z ), q will be in the range from 1 to ?N and p will be in the range of 1 to N/q.
Also p+q = 2*x, so x = (p+q)/2.
Now, as x can have a max value of N, therefore
(p+q)/2 <= N
p <= 2*N-q
So, the max value of p = min ( 2*N – q, N/q).
Now, after knowing the ranges of p & q, try all possible values of p & q. And after fixing, all possible pairs possible are (l, q) where l is in the range from q to maximum value of p.
So, the total number of pairs formed are (p – q + 1), say cnt.
Now, as we know that p=x+z and q=x-z, so both p & q are either even or odd. And based on this conclusion, if q is even the total valid pairs are cnt/2 (after removing all pairs with an even value of p) and if it is odd then the total number of valid pairs are (cnt/2 + 1).
Below is the implementation of the above approach:
C++
#include <bits/stdc++.h>
using namespace std;
int countPairs( int N)
{
int res = 0;
for ( int q = 1; q * q <= N; q++) {
int maxP = min(2 * N - q, N / q);
if (maxP < q)
continue ;
int cnt = maxP - q + 1;
res += (cnt / 2 + (cnt & 1));
}
return res;
}
int main()
{
int N = 3;
cout << countPairs(N);
return 0;
}
|
Java
import java.util.*;
class GFG{
static int countPairs( int N)
{
int res = 0 ;
for ( int q = 1 ; q * q <= N; q++) {
int maxP = Math.min( 2 * N - q, N / q);
if (maxP < q)
continue ;
int cnt = maxP - q + 1 ;
res += (cnt / 2 + (cnt & 1 ));
}
return res;
}
public static void main(String[] args)
{
int N = 3 ;
System.out.print(countPairs(N));
}
}
|
Python3
import math
def countPairs(N):
res = 0
for q in range ( 1 , int (math.sqrt(N)) + 1 ):
maxP = min ( 2 * N - q, N / / q)
if (maxP < q):
continue
cnt = maxP - q + 1
res + = (cnt / / 2 + (cnt & 1 ))
return res
if __name__ = = "__main__" :
N = 3
print (countPairs(N))
|
C#
using System;
class GFG
{
static int countPairs( int N)
{
int res = 0;
for ( int q = 1; q * q <= N; q++) {
int maxP = Math.Min(2 * N - q, N / q);
if (maxP < q)
continue ;
int cnt = maxP - q + 1;
res += (cnt / 2 + (cnt & 1));
}
return res;
}
public static void Main()
{
int N = 3;
Console.WriteLine(countPairs(N));
}
}
|
Javascript
<script>
function countPairs(N) {
let res = 0;
for (let q = 1; q * q <= N; q++) {
let maxP = Math.min(2 * N - q, N / q);
if (maxP < q)
continue ;
let cnt = maxP - q + 1;
res += Math.floor(cnt / 2 + (cnt & 1));
}
return res;
}
let N = 3;
document.write(countPairs(N));
</script>
|
Time Complexity: O(N1/2)
Auxiliary Space: O(1)
Like Article
Suggest improvement
Share your thoughts in the comments
Please Login to comment...