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 = 3
Input: 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++
#include <bits/stdc++.h>
using namespace std;
bool difSquare(int n)
{
if (n % 4 != 2) {
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 GFG{
static boolean difSquare(int n)
{
if (n % 4 != 2)
{
return true;
}
return false;
}
public static void main(String[] args)
{
int n = 45;
if (difSquare(n))
{
System.out.print("Yes\n");
}
else
{
System.out.print("No\n");
}
}
}
|
Python3
def difSquare(n):
if (n % 4 != 2):
return True
return False
if __name__ == '__main__':
n = 45
if (difSquare(n)):
print("Yes")
else:
print("No")
|
C#
using System;
class GFG{
static bool difSquare(int n)
{
if (n % 4 != 2)
{
return true;
}
return false;
}
public static void Main()
{
int n = 45;
if (difSquare(n))
{
Console.Write("Yes\n");
}
else
{
Console.Write("No\n");
}
}
}
|
Javascript
<script>
function difSquare(n)
{
if (n % 4 != 2)
{
return true;
}
return false;
}
var n = 45;
if (difSquare(n))
{
document.write("Yes\n");
}
else
{
document.write("No\n");
}
</script>
|
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 math
def 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 False
n = 45
if 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 difSquare(n) {
for (let x = 0; x <= Math.sqrt(n); x++) {
const y_squared = n + x * x;
const y = Math.sqrt(y_squared);
if (y * y === y_squared) {
return true;
}
}
return false;
}
const n = 45;
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)