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

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:

2^{2}– 1^{1}= 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 => 4^{2}- 0^{2}N = 6 => Can't be expressed as 6 % 4 = 2 N = 8 => 3^{2}- 1^{2}N = 10 => Can't be expressed as 10 % 4 = 2 N = 11 => 6^{2}- 5^{2}N = 12 => 4^{2}- 2^{2}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 squares ` `bool` `difSquare(` `int` `n) ` `{ ` ` ` `// Checking if n % 4 = 2 or not ` ` ` `if` `(n % 4 != 2) { ` ` ` `return` `true` `; ` ` ` `} ` ` ` ` ` `return` `false` `; ` `} ` ` ` `// Driver code ` `int` `main() ` `{ ` ` ` ` ` `int` `n = 45; ` ` ` `if` `(difSquare(n)) { ` ` ` `cout << ` `"Yes\n"` `; ` ` ` `} ` ` ` `else` `{ ` ` ` `cout << ` `"No\n"` `; ` ` ` `} ` ` ` `return` `0; ` `} ` |

*chevron_right*

*filter_none*

## Java

`// Java program to check whether a number ` `// can be represented by the difference ` `// of two squares ` `import` `java.util.*; ` ` ` `class` `GFG{ ` ` ` `// Function to check whether a number ` `// can be represented by the difference ` `// of two squares ` `static` `boolean` `difSquare(` `int` `n) ` `{ ` ` ` ` ` `// Checking if n % 4 = 2 or not ` ` ` `if` `(n % ` `4` `!= ` `2` `) ` ` ` `{ ` ` ` `return` `true` `; ` ` ` `} ` ` ` `return` `false` `; ` `} ` ` ` `// Driver code ` `public` `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 ` |

*chevron_right*

*filter_none*

## 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 squares ` `def` `difSquare(n): ` ` ` ` ` `# Checking if n % 4 = 2 or not ` ` ` `if` `(n ` `%` `4` `!` `=` `2` `): ` ` ` `return` `True` ` ` `return` `False` ` ` `# Driver code ` `if` `__name__ ` `=` `=` `'__main__'` `: ` ` ` ` ` `n ` `=` `45` ` ` ` ` `if` `(difSquare(n)): ` ` ` `print` `(` `"Yes"` `) ` ` ` `else` `: ` ` ` `print` `(` `"No"` `) ` ` ` `# This code is contributed by mohit kumar 29 ` |

*chevron_right*

*filter_none*

## C#

`// C# program to check whether a number ` `// can be represented by the difference ` `// of two squares ` `using` `System; ` `class` `GFG{ ` ` ` `// Function to check whether a number ` `// can be represented by the difference ` `// of two squares ` `static` `bool` `difSquare(` `int` `n) ` `{ ` ` ` ` ` `// Checking if n % 4 = 2 or not ` ` ` `if` `(n % 4 != 2) ` ` ` `{ ` ` ` `return` `true` `; ` ` ` `} ` ` ` `return` `false` `; ` `} ` ` ` `// Driver code ` `public` `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 ` |

*chevron_right*

*filter_none*

**Output:**

Yes

Don’t stop now and take your learning to the next level. Learn all the important concepts of Data Structures and Algorithms with the help of the most trusted course: **DSA Self Paced**. Become industry ready at a student-friendly price.

## Recommended Posts:

- Check whether a number can be represented by sum of two squares
- Check whether a number can be represented by the product of two squares
- Check if N can be represented as sum of squares of two consecutive integers
- Count elements in an Array that can be represented as difference of two perfect squares
- Check if difference of areas of two squares is prime
- Check if a number can be represented as sum of non zero powers of 2
- Check if given number can be represented as sum of two great numbers
- Check if a number can be represented as a sum of 2 triangular numbers
- Check if a given number can be represented in given a no. of digits in any base
- Check whether a number can be represented as sum of K distinct positive integers
- Check if N can be represented as sum of integers chosen from set {A, B}
- Check if all nodes of the Binary Tree can be represented as sum of two primes
- Check if the sum of perfect squares in an array is divisible by x
- Check if factorial of N is divisible by the sum of squares of first N natural numbers
- Find two numbers such that difference of their squares equal to N
- Count of pairs of integers whose difference of squares is equal to N
- Count all subarrays whose sum can be split as difference of squares of two Integers
- Subsets of size K with product equal to difference of two perfect squares
- Count of possible permutations of a number represented as a sum of 2's, 4's and 6's only
- Divide large number represented as string

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.