Find two numbers such that difference of their squares equal to N
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 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++
// C++ Program to find two numbers // with difference of their // squares equal to N #include <bits/stdc++.h> using namespace std; // Function to check and print // the required two positive integers void solve(int n) { // Iterate till sqrt(n) to find // factors of N for (int x = 1; x <= sqrt(n); x++) { // Check if x is one // of the factors of N if (n % x == 0) { // Store the factor int small = x; // Compute the other factor int big = n / x; // Check if the two factors // are of the same parity if (small % 2 == big % 2) { // Compute a and b int a = (small + big) / 2; int b = (big - small) / 2; cout << a << " " << b << endl; return; } } } // If no pair exists cout << -1 << endl; } // Driver Code int main() { int n = 7; solve(n); return 0; } |
4 3
Time Complexity: O(sqrt(N))
Auxilary Space: O(1)
GeeksforGeeks has prepared a complete interview preparation course with premium videos, theory, practice problems, TA support and many more features. Please refer Placement 100 for details
Recommended Posts:
- Find K numbers with sum equal to N and sum of their squares maximized
- Find two numbers whose difference of fourth power is equal to N
- Subsets of size K with product equal to difference of two perfect squares
- Difference between sum of the squares of first n natural numbers and square of sum
- Count numbers whose difference with N is equal to XOR with N
- Find all factorial numbers less than or equal to n
- Find N distinct numbers whose bitwise Or is equal to K
- Find maximum product of digits among numbers less than or equal to N
- Find two Composite Numbers such that there difference is N
- Find two numbers with the given LCM and minimum possible difference
- Program to find the percentage of difference between two numbers
- Find the minimum difference between Shifted tables of two numbers
- Find two numbers made up of a given digit such that their difference is divisible by N
- Program to find the maximum difference between the index of any two different numbers
- Check whether a number can be represented as difference of two squares
If you like GeeksforGeeks and would like to contribute, you can also write an article using contribute.geeksforgeeks.org or mail your article to contribute@geeksforgeeks.org. See your article appearing on the GeeksforGeeks main page and help other Geeks.
Please Improve this article if you find anything incorrect by clicking on the "Improve Article" button below.

