Given a number N, the task is to check if this number can be represented as the difference of two perfect squares or not.
Examples:
Input: N = 3
Output: Yes
Explanation:
22 – 11 = 3Input: N = 10
Output: No
Approach: The idea is that all the numbers can be represented as the difference of two squares except the numbers which yield the remainder of 2 when divided by 4.
Let’s visualize this by taking a few examples:
N = 4 => 22 - 02
N = 6 => Can't be expressed as 6 % 4 = 2
N = 8 => 32 - 12
N = 10 => Can't be expressed as 10 % 4 = 2
N = 11 => 62 - 52
N = 12 => 42 - 22
and so on...
Therefore, the idea is to simply check the remainder for 2 when the given number is divided by 4.
Below is the implementation of the above approach:
C++
// C++ program to check whether a number// can be represented by the difference// of two squares#include <bits/stdc++.h>using namespace std;// Function to check whether a number// can be represented by the difference// of two squaresbool difSquare(int n){ // Checking if n % 4 = 2 or not if (n % 4 != 2) { return true; } return false;}// Driver codeint main(){ int n = 45; if (difSquare(n)) { cout << "Yes\n"; } else { cout << "No\n"; } return 0;} |
Java
// Java program to check whether a number// can be represented by the difference// of two squaresimport java.util.*;class GFG{// Function to check whether a number// can be represented by the difference// of two squaresstatic boolean difSquare(int n){ // Checking if n % 4 = 2 or not if (n % 4 != 2) { return true; } return false;}// Driver codepublic static void main(String[] args){ int n = 45; if (difSquare(n)) { System.out.print("Yes\n"); } else { System.out.print("No\n"); }}}// This code is contributed by shivanisinghss2110 |
Python3
# Python3 program to check whether a number# can be represented by the difference# of two squares# Function to check whether a number# can be represented by the difference# of two squaresdef difSquare(n): # Checking if n % 4 = 2 or not if (n % 4 != 2): return True return False# Driver codeif __name__ == '__main__': n = 45 if (difSquare(n)): print("Yes") else: print("No")# This code is contributed by mohit kumar 29 |
C#
// C# program to check whether a number// can be represented by the difference// of two squaresusing System;class GFG{// Function to check whether a number// can be represented by the difference// of two squaresstatic bool difSquare(int n){ // Checking if n % 4 = 2 or not if (n % 4 != 2) { return true; } return false;}// Driver codepublic static void Main(){ int n = 45; if (difSquare(n)) { Console.Write("Yes\n"); } else { Console.Write("No\n"); }}}// This code is contributed by Nidhi_biet |
Javascript
<script>// Javascript program to check whether a number// can be represented by the difference// of two squares// Function to check whether a number// can be represented by the difference// of two squaresfunction difSquare(n){ // Checking if n % 4 = 2 or not if (n % 4 != 2) { return true; } return false;}// Driver codevar n = 45;if (difSquare(n)) { document.write("Yes\n");} else{ document.write("No\n");}// This code is contributed by Rajput-Ji </script> |
Yes
Time complexity: O(1) because constant operations have been done
Auxiliary space: O(1)
Approach 2: Iterative Approach:
Here’s another approach to check whether a number can be represented by the difference of two squares:
- Iterate through all possible values of x from 0 to sqrt(n).
- For each value of x, compute y^2 = n + x^2.
- Check if y is an integer. If it is, then n can be represented as the difference of two squares, which are x^2 and y^2 – n.
- If no value of x satisfies the condition, then n cannot be represented as the difference of two squares.
Here’s the implementation of this approach:
C++
#include <bits/stdc++.h>using namespace std;bool difSquare(int n) { for (int x = 0; x <= sqrt(n); x++) { int y_squared = n + x * x; int y = sqrt(y_squared); if (y * y == y_squared) { return true; } } return false;}int main() { int n = 45; if (difSquare(n)) { cout << "Yes\n"; } else { cout << "No\n"; } return 0;} |
Java
import java.util.*;class Main { static boolean difSquare(int n) { for (int x = 0; x <= Math.sqrt(n); x++) { int y_squared = n + x * x; int y = (int) Math.sqrt(y_squared); if (y * y == y_squared) { return true; } } return false; } public static void main(String[] args) { int n = 45; if (difSquare(n)) { System.out.println("Yes"); } else { System.out.println("No"); } }} |
Python3
import mathdef difSquare(n): for x in range(int(math.sqrt(n))+1): y_squared = n + x * x y = int(math.sqrt(y_squared)) if y * y == y_squared: return True return Falsen = 45if difSquare(n): print("Yes")else: print("No") |
C#
using System;public class Program{ public static bool DifSquare(int n) { for (int x = 0; x <= Math.Sqrt(n); x++) { int ySquared = n + x * x; int y = (int)Math.Sqrt(ySquared); if (y * y == ySquared) { return true; } } return false; } public static void Main() { int n = 45; if (DifSquare(n)) { Console.WriteLine("Yes"); } else { Console.WriteLine("No"); } }} |
Javascript
// Function to check if a number can be represented as // the difference of two squaresfunction difSquare(n) { // Loop through all possible values of x from 0 up to the // square root of n for (let x = 0; x <= Math.sqrt(n); x++) { // Calculate y^2 as n + x^2 const y_squared = n + x * x; // Calculate the square root of y_squared const y = Math.sqrt(y_squared); // Check if y^2 is equal to y_squared, if so, we found // the difference of two squares if (y * y === y_squared) { return true; } } // If no difference of squares is found, return false return false;}// Driver Code const n = 45; // Call the difSquare function and check the result if (difSquare(n)) { console.log("Yes"); } else { console.log("No"); } |
Output:
Yes
Time complexity: O(1) because constant operations have been done
Auxiliary space: O(1)
Ready to dive in? Explore our Free Demo Content and join our DSA course, trusted by over 100,000 neveropen!
