Given an array **arr[]**, the task is to count all sub-array whose sum can be represented as the difference of squares of any two numbers.

**Examples:**

Input:arr[] = {1, 2, 3}

Output:4

Explanation:

Required sub-arrays are {1}, {3}, {1, 2} and {2, 3}

As 1^{2}– 0^{2}= 1, 2^{2}– 1^{2}= 3, 2+3=5=> 3^{2}– 2^{2}= 5

Input:arr[] = {2, 1, 3, 7}

Output:7

Required sub-arrays are –

{1}, {3}, {7}, {2, 1}, {2, 1, 3, 7}, {1, 3} and {1, 3, 7}

**Approach:** The idea is to use the fact that the number which is odd or divisible by 4 can only be represented as the difference of squares of 2 numbers. Below is the illustration of the steps:

- Run nested loops and check for every sub-array whether sum can be written as the difference of squares of 2 numbers or not.
- If the sum of any subarray can be represented as the difference of the square of two numbers then increment the count of such subarrays by 1

Below is the imlpementation of the above approach:

## C++

`// C++ implementation to count the ` `// subarray which can be represented ` `// as the difference of two square ` `// of two numbers ` ` ` `#include <bits/stdc++.h> ` ` ` `using` `namespace` `std; ` `#define ll long long ` ` ` `// Function to count sub-arrays whose ` `// sum can be represented as difference ` `// of squares of 2 numbers ` `int` `countSubarray(` `int` `* arr, ` `int` `n) ` `{ ` ` ` `int` `count = 0; ` ` ` `for` `(` `int` `i = 0; i < n; i++) { ` ` ` ` ` `// Count the elements that ` ` ` `// are odd and divisible by 4 ` ` ` `if` `(arr[i] % 2 != 0 || arr[i] % 4 == 0) ` ` ` `count++; ` ` ` ` ` `// Declare a variable to store sum ` ` ` `ll sum = arr[i]; ` ` ` `for` `(` `int` `j = i + 1; j < n; j++) { ` ` ` ` ` `// Calculate sum of ` ` ` `// current subarray ` ` ` `sum += arr[j]; ` ` ` `if` `(sum % 2 != 0 || sum % 4 == 0) ` ` ` `count++; ` ` ` `} ` ` ` `} ` ` ` `return` `count; ` `} ` ` ` `// Driver Code ` `int` `main() ` `{ ` ` ` `int` `arr[] = { 2, 1, 3, 7 }; ` ` ` `int` `n = ` `sizeof` `(arr) / ` `sizeof` `(arr[0]); ` ` ` `cout << countSubarray(arr, n) << endl; ` ` ` `return` `0; ` `} ` |

*chevron_right*

*filter_none*

## Java

`// Java implementation to count the ` `// subarray which can be represented ` `// as the difference of two square ` `// of two numbers ` ` ` `import` `java.util.*; ` ` ` `class` `GFG { ` ` ` ` ` `// Function to count sub-arrays whose ` ` ` `// sum can be represented as difference ` ` ` `// of squares of 2 numbers ` ` ` `static` `int` `countSubarray(` `int` `[] arr, ` `int` `n) ` ` ` `{ ` ` ` `int` `count = ` `0` `; ` ` ` `for` `(` `int` `i = ` `0` `; i < n; i++) { ` ` ` ` ` `// Count the elements that ` ` ` `// are odd and divisible by 4 ` ` ` `if` `(arr[i] % ` `2` `!= ` `0` `|| arr[i] % ` `4` `== ` `0` `) ` ` ` `count++; ` ` ` ` ` `// Declare a variable to store sum ` ` ` `long` `sum = arr[i]; ` ` ` `for` `(` `int` `j = i + ` `1` `; j < n; j++) { ` ` ` ` ` `// Calculate sum of ` ` ` `// current subarray ` ` ` `sum += arr[j]; ` ` ` `if` `(sum % ` `2` `!= ` `0` `|| sum % ` `4` `== ` `0` `) ` ` ` `count++; ` ` ` `} ` ` ` `} ` ` ` `return` `count; ` ` ` `} ` ` ` ` ` `// Driver code ` ` ` `public` `static` `void` `main(String[] args) ` ` ` `{ ` ` ` `int` `arr[] = { ` `2` `, ` `1` `, ` `3` `, ` `7` `}; ` ` ` `int` `n = arr.length; ` ` ` `System.out.println(countSubarray(arr, n)); ` ` ` `} ` `} ` |

*chevron_right*

*filter_none*

## Python 3

`# Python implementation to ` `# count the sub-arrays whose ` `# sum can be reperesented as ` `# difference of square of two ` `# numbers ` ` ` `# Function to count sub-arrays whose ` `# sum can be represented as ` `# difference of squares of 2 numbers ` `def` `countSubarray(arr, n): ` ` ` `count ` `=` `0` ` ` `for` `i ` `in` `range` `(n): ` ` ` ` ` `# Count the elements that ` ` ` `# are odd or divisible by 4 ` ` ` `if` `arr[i]` `%` `2` `!` `=` `0` `or` `arr[i]` `%` `4` `=` `=` `0` `: ` ` ` `count` `+` `=` `1` ` ` ` ` `# Declare a variable to store sum ` ` ` `tot ` `=` `arr[i] ` ` ` `for` `j ` `in` `range` `(i ` `+` `1` `, n): ` ` ` ` ` `# Calculate sum of ` ` ` `# current subarray ` ` ` `tot` `+` `=` `arr[j] ` ` ` `if` `tot ` `%` `2` `!` `=` `0` `or` `tot ` `%` `4` `=` `=` `0` `: ` ` ` `count` `+` `=` `1` ` ` `return` `count ` ` ` ` ` `# Driver Code ` `if` `__name__ ` `=` `=` `"__main__"` `: ` ` ` `arr ` `=` `[ ` `2` `, ` `1` `, ` `3` `, ` `7` `] ` ` ` `n ` `=` `len` `(arr) ` ` ` `print` `(countSubarray(arr, n)) ` |

*chevron_right*

*filter_none*

## C#

`// C# implementation to count the ` `// subarray which can be represented ` `// as the difference of two square ` `// of two numbers ` `using` `System; ` ` ` `class` `GFG { ` ` ` ` ` `// Function to count sub-arrays whose ` ` ` `// sum can be represented as difference ` ` ` `// of squares of 2 numbers ` ` ` `static` `int` `countSubarray(` `int` `[] arr, ` `int` `n) ` ` ` `{ ` ` ` `int` `count = 0; ` ` ` `for` `(` `int` `i = 0; i < n; i++) { ` ` ` ` ` `// Count the elements that ` ` ` `// are odd and divisible by 4 ` ` ` `if` `(arr[i] % 2 != 0 || arr[i] % 4 == 0) ` ` ` `count++; ` ` ` ` ` `// Declare a variable to store sum ` ` ` `long` `sum = arr[i]; ` ` ` `for` `(` `int` `j = i + 1; j < n; j++) { ` ` ` ` ` `// Calculate sum of ` ` ` `// current subarray ` ` ` `sum += arr[j]; ` ` ` `if` `(sum % 2 != 0 || sum % 4 == 0) ` ` ` `count++; ` ` ` `} ` ` ` `} ` ` ` `return` `count; ` ` ` `} ` ` ` ` ` `// Driver Code ` ` ` `public` `static` `void` `Main() ` ` ` `{ ` ` ` `int` `[] arr = { 2, 1, 3, 7 }; ` ` ` `int` `n = arr.Length; ` ` ` `Console.WriteLine(countSubarray(arr, n)); ` ` ` `} ` `} ` |

*chevron_right*

*filter_none*

## PHP

`<?php ` `// PHP implementation to count the ` `// subarray which can be represented ` `// as the difference of two square ` `// of two numbers ` ` ` `// Function to count sub-arrays whose ` `// sum is divisible by K ` `function` `countSubarray(` `$arr` `, ` `$n` `) ` `{ ` ` ` `$count` `=0; ` ` ` `for` `(` `$i` `=0;` `$i` `<` `$n` `;` `$i` `++) ` ` ` `{ ` ` ` ` ` `// Count the elements that ` ` ` `// are odd and divisible by 4 ` ` ` `if` `(` `$arr` `[` `$i` `]%2!=0 || ` `$arr` `[` `$i` `]%4==0) ` ` ` `$count` `++; ` ` ` ` ` `// Declare a variable to store sum ` ` ` `$sum` `=` `$arr` `[` `$i` `]; ` ` ` `for` `(` `$j` `=` `$i` `+1;` `$j` `<` `$n` `;` `$j` `++) ` ` ` `{ ` ` ` ` ` `// Calculate sum of ` ` ` `// current subarray ` ` ` `$sum` `+=` `$arr` `[` `$j` `]; ` ` ` `if` `(` `$sum` `%2!=0 || ` `$sum` `%4==0) ` ` ` `$count` `++; ` ` ` `} ` ` ` `} ` ` ` `return` `$count` `; ` `} ` ` ` `// Driver code ` `$arr` `= ` `array` `( 2, 1, 3, 7 ); ` `$n` `= ` `count` `(` `$arr` `); ` `echo` `countSubarray(` `$arr` `, ` `$n` `); ` ` ` `?> ` |

*chevron_right*

*filter_none*

**Output:**

7

**Time Complexity:** O(N^{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 all subarrays whose sum can be split as difference of squares of two Integers
- Count of subarrays whose product is equal to difference of two different numbers
- Count elements in an Array that can be represented as difference of two perfect squares
- Minimize the sum of squares of sum of N/2 paired formed by N numbers
- Count of total subarrays whose sum is a Fibonacci Numbers
- Split array into two subarrays such that difference of their sum is minimum
- Count subarrays such that remainder after dividing sum of elements by K gives count of elements
- Differences between number of increasing subarrays and decreasing subarrays in k sized windows
- Subsets of size K with product equal to difference of two perfect squares
- Minimize the sum of the squares of the sum of elements of each group the array is divided into
- Count subarrays with equal number of occurrences of two given elements
- Count of common subarrays in two different permutations of 1 to N
- Count of Subarrays in an array containing numbers from 1 to the length of subarray
- Count non decreasing subarrays of size N from N Natural numbers
- Count of subarrays having exactly K prime numbers
- Count of subarrays consisting of only prime numbers
- Count of subarrays having exactly K perfect square numbers
- Count of subarrays of size K which is a permutation of numbers from 1 to K
- Minimum absolute difference of XOR values of two subarrays
- Partition into two subarrays of lengths k and (N - k) such that the difference of sums is maximum

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.