Given an array **arr[]**, the task is to count all subarrays whose sum can be split as the difference of the squares of two integers.

**Examples:**

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

Output:6

Explanation:

There are six subarrays which can be formed from the array whose sum can be split as the difference of squares of two integers. They are:

Sum of the subarray {1} = 1 = 1^{2}– 0^{2}

Sum of the subarray {3} = 3 = 2^{2}– 1^{2}

Sum of the subarray {5} = 5 = 3^{2}– 2^{2}

Sum of the subarray {1, 3} = 4 = 2^{2}– 0^{2}

Sum of the subarray {3, 5} = 8 = 3^{2}– 1^{2}

Sum of the subarray {1, 3, 5} = 9 = 5^{2}– 4^{2}

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

Output:11

**Approach:** In order to solve this problem, an observation needs to be made. Any number can be represented as the difference of two squares except those which can be represented in the form **((4 * N) + 2)** where N is an integer. Therefore, the following steps can be followed to compute the answer:

- Iterate over the array to find all the possible subarrays of the given array.
- If a number K can be expressed as
**((4 * N) + 2)**where N is some integer, then**K + 2**is always a multiple of 4. - Therefore, for every sum of the subarray K, check if
**(K + 2)**is a multiple of 4 or not. - If it is, then that particular value cannot be expressed as the difference of the squares.

Below is the implementation of the above approach:

## C++

`// C++ program to count all the non-contiguous ` `// subarrays whose sum can be split ` `// as the difference of the squares ` `#include <bits/stdc++.h> ` `using` `namespace` `std; ` ` ` `// Function to count all the non-contiguous ` `// subarrays whose sum can be split ` `// as the difference of the squares ` `int` `Solve(` `int` `arr[], ` `int` `n) ` `{ ` ` ` `int` `temp = 0, count = 0; ` ` ` ` ` `// Loop to iterate over all the possible ` ` ` `// subsequences of the array ` ` ` `for` `(` `int` `i = 0; i < n; i++) { ` ` ` `temp = 0; ` ` ` `for` `(` `int` `j = i; j < n; j++) { ` ` ` ` ` `// Finding the sum of all the ` ` ` `// possible subsequences ` ` ` `temp += arr[j]; ` ` ` ` ` `// Condition to check whether ` ` ` `// the number can be split ` ` ` `// as difference of squares ` ` ` `if` `((temp + 2) % 4 != 0) ` ` ` `count++; ` ` ` `} ` ` ` `} ` ` ` ` ` `return` `count; ` `} ` ` ` `// Driver code ` `int` `main() ` `{ ` ` ` ` ` `int` `arr[] = { 1, 2, 3, 4, 5, ` ` ` `6, 7, 8, 9, 10 }; ` ` ` `int` `N = ` `sizeof` `(arr) / ` `sizeof` `(` `int` `); ` ` ` ` ` `cout << Solve(arr, N); ` ` ` `return` `0; ` `} ` |

*chevron_right*

*filter_none*

## Java

`// Java program to count all the ` `// non-contiguous subarrays whose ` `// sum can be split as the ` `// difference of the squares ` `import` `java.io.*; ` `import` `java.lang.*; ` `import` `java.util.*; ` ` ` `class` `GFG{ ` ` ` `// Function to count all the non-contiguous ` `// subarrays whose sum can be split ` `// as the difference of the squares ` `static` `int` `Solve(` `int` `arr[], ` `int` `n) ` `{ ` ` ` `int` `temp = ` `0` `, count = ` `0` `; ` ` ` ` ` `// Loop to iterate over all the possible ` ` ` `// subsequences of the array ` ` ` `for` `(` `int` `i = ` `0` `; i < n; i++) ` ` ` `{ ` ` ` `temp = ` `0` `; ` ` ` `for` `(` `int` `j = i; j < n; j++) ` ` ` `{ ` ` ` ` ` `// Finding the sum of all the ` ` ` `// possible subsequences ` ` ` `temp += arr[j]; ` ` ` ` ` `// Condition to check whether ` ` ` `// the number can be split ` ` ` `// as difference of squares ` ` ` `if` `((temp + ` `2` `) % ` `4` `!= ` `0` `) ` ` ` `count++; ` ` ` `} ` ` ` `} ` ` ` ` ` `return` `count; ` `} ` ` ` `// Driver Code ` `public` `static` `void` `main(String [] args) ` `{ ` ` ` `int` `arr[] = { ` `1` `, ` `2` `, ` `3` `, ` `4` `, ` `5` `, ` ` ` `6` `, ` `7` `, ` `8` `, ` `9` `, ` `10` `}; ` ` ` `int` `N = arr.length; ` ` ` ` ` `System.out.println(Solve(arr, N)); ` `} ` `} ` ` ` `// This code is contributed by shivanisinghss2110 ` |

*chevron_right*

*filter_none*

## Python3

`# Python3 program to count all the non-contiguous ` `# subarrays whose sum can be split ` `# as the difference of the squares ` ` ` `# Function to count all the non-contiguous ` `# subarrays whose sum can be split ` `# as the difference of the squares ` `def` `Solve(arr, n): ` ` ` ` ` `temp ` `=` `0` `; count ` `=` `0` `; ` ` ` ` ` `# Loop to iterate over all the possible ` ` ` `# subsequences of the array ` ` ` `for` `i ` `in` `range` `(` `0` `, n): ` ` ` `temp ` `=` `0` `; ` ` ` `for` `j ` `in` `range` `(i, n): ` ` ` ` ` `# Finding the sum of all the ` ` ` `# possible subsequences ` ` ` `temp ` `=` `temp ` `+` `arr[j]; ` ` ` ` ` `# Condition to check whether ` ` ` `# the number can be split ` ` ` `# as difference of squares ` ` ` `if` `((temp ` `+` `2` `) ` `%` `4` `!` `=` `0` `): ` ` ` `count ` `+` `=` `1` `; ` ` ` ` ` `return` `count; ` ` ` `# Driver code ` `arr ` `=` `[ ` `1` `, ` `2` `, ` `3` `, ` `4` `, ` `5` `, ` ` ` `6` `, ` `7` `, ` `8` `, ` `9` `, ` `10` `]; ` `N ` `=` `len` `(arr); ` `print` `(Solve(arr, N)); ` ` ` `# This code is contributed by Code_Mech ` |

*chevron_right*

*filter_none*

## C#

`// C# program to count all the ` `// non-contiguous subarrays whose ` `// sum can be split as the ` `// difference of the squares ` `using` `System; ` ` ` `class` `GFG{ ` ` ` `// Function to count all the ` `// non-contiguous subarrays whose ` `// sum can be split as the ` `// difference of the squares ` `static` `int` `Solve(` `int` `[]arr, ` `int` `n) ` `{ ` ` ` `int` `temp = 0, count = 0; ` ` ` ` ` `// Loop to iterate over all ` ` ` `// the possible subsequences ` ` ` `// of the array ` ` ` `for` `(` `int` `i = 0; i < n; i++) ` ` ` `{ ` ` ` `temp = 0; ` ` ` `for` `(` `int` `j = i; j < n; j++) ` ` ` `{ ` ` ` ` ` `// Finding the sum of all the ` ` ` `// possible subsequences ` ` ` `temp += arr[j]; ` ` ` ` ` `// Condition to check whether ` ` ` `// the number can be split ` ` ` `// as difference of squares ` ` ` `if` `((temp + 2) % 4 != 0) ` ` ` `count++; ` ` ` `} ` ` ` `} ` ` ` `return` `count; ` `} ` ` ` `// Driver code ` `public` `static` `void` `Main(String[] args) ` `{ ` ` ` `int` `[]arr = { 1, 2, 3, 4, 5, ` ` ` `6, 7, 8, 9, 10 }; ` ` ` `int` `N = arr.Length; ` ` ` ` ` `Console.Write(Solve(arr, N)); ` `} ` `} ` ` ` `// This code is contributed by shivanisinghss2110 ` |

*chevron_right*

*filter_none*

**Output:**

40

**Time Complexity:** *O(N) ^{2}* where N is the size of the array

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 subarrays with sum as difference of squares of two numbers
- Count of pairs of integers whose difference of squares is equal to N
- Check if N can be represented as sum of squares of two consecutive integers
- Split N into two integers whose addition to A and B makes them equal
- Split array into two subarrays such that difference of their sum is minimum
- Count elements in an Array that can be represented as difference of two perfect squares
- Count of subsequences whose product is a difference of square of two integers
- Split array into two subarrays such that difference of their maximum is minimum
- Split an array into two equal Sum subarrays
- Count of ways to split an Array into three contiguous Subarrays having increasing Sum
- Check if an array can be split into subarrays with GCD exceeding K
- Count pairs (a, b) whose sum of squares is N (a^2 + b^2 = N)
- Check whether a number can be represented as difference of two squares
- Check whether a number can be represented by sum of two squares
- Count of primes in a given range that can be expressed as sum of perfect squares
- Count of total subarrays whose sum is a Fibonacci Numbers
- Count of subarrays whose sum is a perfect square
- Generate Array whose sum of all K-size subarrays divided by N leaves remainder X
- Split an Array to maximize subarrays having equal count of odd and even elements for a cost not exceeding K
- Split array into subarrays at minimum cost by minimizing count of repeating elements in each subarray

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.