Find two numbers such that difference of their squares equal to N | C ++ Program to find two numbers with difference of their squares equal to N ; Function to check and print the required two positive integers ; Iterate till sqrt ( n ) to find factors of N ; Check if x is one of the factors of N ; Store the factor ; Compute the other factor ; Check if the two factors are of the same parity ; Compute a and b ; If no pair exists ; Driver Code
#include <bits/stdc++.h> NEW_LINE 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 ; }