Find two numbers such that difference of their squares equal to N
Last Updated :
29 Nov, 2023
Given an integer N, the task is to find two non-negative integers A and B, such that A2 – B2 = N. If no such integers exist, then print -1.
Examples:
Input: N = 7
Output: 4 3
Explanation:
The two integers 4 and 3 can be represented as 42 – 32 = 7.
Input: N = 6
Output: -1
Explanation:
No pair of (A, B) exists that satisfies the required condition.
Approach:
- A2 – B2 can be represented as (A – B) * (A + B).
A2 – B2 = (A – B) * (A + B)
- Thus, for A2 – B2 to be equal to N, both (A + B) and (A – B) should be divisors of N.
- Considering A + B and A – B to be equal to C and D respectively, then C and D must be divisors of N such that C ? D and C and D should be of the same parity.
- Hence, in order to solve this problem, we just need to find any pair C and D satisfying the above condition. If no such C & D exists, then the print -1.
Below is the implementation of the above approach:
C++
#include <bits/stdc++.h>
using namespace std;
void solve( int n)
{
for ( int x = 1; x <= sqrt (n); x++) {
if (n % x == 0) {
int small = x;
int big = n / x;
if (small % 2 == big % 2) {
int a = (small + big) / 2;
int b = (big - small) / 2;
cout << a << " "
<< b << endl;
return ;
}
}
}
cout << -1 << endl;
}
int main()
{
int n = 7;
solve(n);
return 0;
}
|
Java
import java.util.*;
class GFG{
static void solve( int n)
{
for ( int x = 1 ; x <= Math.sqrt(n); x++)
{
if (n % x == 0 )
{
int small = x;
int big = n / x;
if (small % 2 == big % 2 )
{
int a = (small + big) / 2 ;
int b = (big - small) / 2 ;
System.out.print(a + " " + b);
return ;
}
}
}
System.out.print(- 1 );
}
public static void main(String args[])
{
int n = 7 ;
solve(n);
}
}
|
Python3
from math import sqrt
def solve(n) :
for x in range ( 1 , int (sqrt(n)) + 1 ) :
if (n % x = = 0 ) :
small = x;
big = n / / x;
if (small % 2 = = big % 2 ) :
a = (small + big) / / 2 ;
b = (big - small) / / 2 ;
print (a, b) ;
return ;
print ( - 1 );
if __name__ = = "__main__" :
n = 7 ;
solve(n);
|
C#
using System;
class GFG{
static void solve( int n)
{
for ( int x = 1; x <= Math.Sqrt(n); x++)
{
if (n % x == 0)
{
int small = x;
int big = n / x;
if (small % 2 == big % 2)
{
int a = (small + big) / 2;
int b = (big - small) / 2;
Console.WriteLine(a + " " + b);
return ;
}
}
}
Console.WriteLine(-1);
}
public static void Main()
{
int n = 7;
solve(n);
}
}
|
Javascript
<script>
function solve(n) {
for ( var x = 1; x <= Math.sqrt(n); x++) {
if (n % x == 0) {
var small = x;
var big = n / x;
if (small % 2 == big % 2) {
var a = (small + big) / 2;
var b = (big - small) / 2;
document.write(a + " " + b);
return ;
}
}
}
document.write(-1);
}
var n = 7;
solve(n);
</script>
|
Time Complexity: O(sqrt(N))
Auxiliary Space: O(1)
Approach#2: Using brute force
This approach is to use two nested loops to generate all possible pairs of integers (i, j) such that i < j. We then check if the difference of their squares is equal to N. If we find a pair that satisfies this condition, we return the pair. If no such pair exists, we return -1.
Algorithm
1. Initialize a nested loop that iterates over all possible pairs of integers (i, j) such that i < j.
2. Compute the difference of their squares, (j^2 – i^2).
3. If (j^2 – i^2) is equal to N, return the pair (j, i) as a string.
4. If no pair is found that satisfies the condition, return -1.
C++
#include <iostream>
using namespace std;
string findNumbersWithSquareDifference( int N) {
for ( int i = 1; i < N; i++) {
for ( int j = i + 1; j <= N; j++) {
if (j * j - i * i == N) {
return to_string(j) + " " + to_string(i);
}
}
}
return "-1" ;
}
int main() {
int N = 7;
cout << findNumbersWithSquareDifference(N) << endl;
return 0;
}
|
Java
public class GFG {
public static String
findNumbersWithSquareDifference( int N)
{
for ( int i = 1 ; i < N; i++) {
for ( int j = i + 1 ; j <= N; j++) {
if (j * j - i * i == N) {
return j + " " + i;
}
}
}
return "-1" ;
}
public static void main(String[] args)
{
int N = 7 ;
System.out.println(
findNumbersWithSquareDifference(N));
}
}
|
Python3
def find_numbers_with_square_difference(N):
for i in range ( 1 , N):
for j in range (i + 1 , N + 1 ):
if j * * 2 - i * * 2 = = N:
return f "{j} {i}"
return "-1"
N = 7
print (find_numbers_with_square_difference(N))
|
C#
using System;
class GFG {
static string FindNumbersWithSquareDifference( int N) {
for ( int i = 1; i < N; i++) {
for ( int j = i + 1; j <= N; j++) {
if (j * j - i * i == N) {
return $ "{j} {i}" ;
}
}
}
return "-1" ;
}
public static void Main() {
int N = 7;
Console.WriteLine(
FindNumbersWithSquareDifference(N));
}
}
|
Javascript
function findNumbersWithSquareDifference(N) {
for (let i = 1; i < N; i++) {
for (let j = i + 1; j <= N; j++) {
if (j * j - i * i === N) {
return j + " " + i;
}
}
}
return "-1" ;
}
const N = 7;
console.log(findNumbersWithSquareDifference(N));
|
Time Complexity: O(N^2) because we use two nested loops to iterate over all possible pairs of integers from 1 to N.
Space Complexity: O(1) because we only store the variables i, j, and the return string, which do not depend on the input size N.
Like Article
Suggest improvement
Share your thoughts in the comments
Please Login to comment...