Given an array arr[], the task is to count the number of elements in the array that can be represented as in the form of the difference of two perfect square numbers. ![]()
Examples:
Input: arr[] = {1, 2, 3}
Output: 2
Explanation:
There are two such elements that can be represented as
difference of square of two numbers –
Element 1 –
Element 3 –
Therefore, Count of such elements is 2.Input: arr[] = {2, 5, 6}
Output: 1
Explanation:
There is only one such element. That is –
Element 5 –
Therefore, Count of such elements is 1.
Approach: The key observation in the problem is numbers which can be represented as the difference of the squares of two numbers never yield 2 as the remainder when divided by 4.
For Example:
N = 4 =>
N = 6 => Can’t be represented as
N = 8 =>
N = 10 => Can’t be represented as![]()
Therefore, iterate over the array and count the number of such elements in the array.
Below is the implementation of the above approach:
C++
// C++ implementation to count the // number of elements which can be // represented as the difference // of the two square #include <bits/stdc++.h> using namespace std; // Function to count of such elements // in the array which can be represented // as the difference of the two squares int count_num(int arr[], int n) { // Initialize count int count = 0; // Loop to iterate // over the array for (int i = 0; i < n; i++) // Condition to check if the // number can be represented // as the difference of squares if ((arr[i] % 4) != 2) count++; cout << count; return 0; } // Driver code int main() { int arr[] = { 1, 2, 3 }; int n = sizeof(arr) / sizeof(arr[0]); count_num(arr, n); return 0; } |
Java
// Java implementation to count the // number of elements which can be // represented as the difference // of the two square class GFG{ // Function to count of such elements // in the array which can be represented // as the difference of the two squares static void count_num(int []arr, int n) { // Initialize count int count = 0; // Loop to iterate // over the array for(int i = 0; i < n; i++) { // Condition to check if the // number can be represented // as the difference of squares if ((arr[i] % 4) != 2) count++; } System.out.println(count); } // Driver code public static void main (String[] args) { int arr[] = { 1, 2, 3 }; int n = arr.length; count_num(arr, n); } } // This code is contributed by AnkitRai01 |
Python3
# Python3 implementation to count the # number of elements in the array # which can be represented as difference # of the two elements # Function to return the # Count of required count # of such elements def count_num(arr, n): # Intialize count count = 0 # Loop to iterate over the # array of elements for i in arr: # Condition to check if the # number can be represented # as the difference # of two squares if ((i % 4) != 2): count = count + 1 return count # Driver Code if __name__ == "__main__": arr = [1, 2, 3] n = len(arr) # Function Call print(count_num(arr, n)) |
C#
// C# implementation to count the // number of elements which can be // represented as the difference // of the two square using System; class GFG{ // Function to count of such elements // in the array which can be represented // as the difference of the two squares static void count_num(int []arr, int n) { // Initialize count int count = 0; // Loop to iterate // over the array for(int i = 0; i < n; i++) { // Condition to check if the // number can be represented // as the difference of squares if ((arr[i] % 4) != 2) count++; } Console.WriteLine(count); } // Driver code public static void Main(string[] args) { int []arr = { 1, 2, 3 }; int n = arr.Length; count_num(arr, n); } } // This code is contributed by shivanisinghss2110 |
2
Attention reader! Don’t stop learning now. Get hold of all the important DSA concepts with the DSA Self Paced Course at a student-friendly price and become industry ready.
Recommended Posts:
- Count array elements that can be represented as sum of at least two consecutive array elements
- Check whether a number can be represented as difference of two squares
- 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
- Check if a number can be represented as sum of two consecutive perfect cubes
- Count of primes in a given range that can be expressed as sum of perfect squares
- Subsets of size K with product equal to difference of two perfect squares
- Count all subarrays whose sum can be split as difference of squares of two Integers
- Largest sub-array whose all elements are perfect squares
- Check whether a number can be represented as difference of two consecutive cubes
- Number of perfect squares between two given numbers
- Sum of distances between the two nearest perfect squares to all the nodes of the given linked list
- Count number less than N which are product of perfect squares
- Count of perfect squares of given length
- Sort perfect squares in an array at their relative positions
- Check if the sum of perfect squares in an array is divisible by x
- Count numbers upto N which are both perfect square and perfect cube
- Check if given number can be represented as sum of two great numbers
- Number of ways in which N can be represented as the sum of two positive integers
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.

