# Check whether a number can be represented as difference of two squares

• Difficulty Level : Hard
• Last Updated : 13 Jul, 2021

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++

 // C++ program to check whether a number// can be represented by the difference// of two squares #include 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



Output:

Yes

My Personal Notes arrow_drop_up