# Count of subsequences whose product is a difference of square of two integers

Given an array arr[] containing N elements which contain both positive and negative elements, the task is to find the total number of contiguous subsequences whose product can be expressed as the difference of the square of two integers.

Examples:

Input: arr[] = {1, 0, 2, 4, 5}
Output: 14
Explanation:
There are 14 subsequences whose product can be expressed as the difference of the square of two integers.
They are: {1}, {0}, {4}, {5}, {1, 0}, {1, 0, 2}, {1, 0, 2, 4}, {1, 0, 2, 4, 5}, {0, 2}, {0, 2, 4}, {0, 2, 4, 5}, {2, 4}, {2, 4, 5}, {4, 5}
The product of all the subsequences can be expressed as the difference of two squares. For example:
1 -> 1^2 – 0^2
0 -> 1^2 – 1^2
4 -> 2^2 – 0^2
5 -> 3^2 – 2^2
8 -> 3^2 – 1^2 …… and so on.

Input: arr[] = {-2, -7, 8, 9}
Output: 8
Explanation:
There are 8 subsequences whose product can be expressed as the difference of the square of two integers.
They are: {-7}, {8}, {9}, {-2, -7, 8}, {-2, -7, 8, 9}, {-7, 8}, {-7, 8, 9}, {8, 9}
The product of all the subsequences can be expressed as the difference of two squares. For example:
-7 -> 3^2 – 4^2
8 -> 3^2 – 1^2
9 -> 3^2 – 0^2
112 -> 11^2 – 3^2 …… and so on.

## Recommended: Please try your approach on {IDE} first, before moving on to the solution.

Naive approach: The naive approach for this problem is to generate all the contiguous subsequences and compute its product and simply check if that number can be expressed as the difference of two squares or not.

Below is the implementation of the above approach:

## C++

 `// C++ implementation to count the ` `// number of contiguous subsequences ` `// whose product can be expressed as ` `// the square of difference of two integers ` ` `  `#include ` `using` `namespace` `std; ` ` `  `// Function to count the number ` `// of contiguous subsequences ` `// whose product can be expressed ` `// as square of difference of two integers ` `int` `CntcontSubs(``int` `a[], ``int` `n) ` `{ ` `    ``int` `c = 0, d = 0, i, sum = 1, j; ` ` `  `    ``// Iterating through the array ` `    ``for` `(i = 0; i < n; i++) { ` ` `  `        ``// Check if that number can be ` `        ``// expressed as the square of ` `        ``// difference of two numbers ` `        ``if` `(a[i] % 2 != 0 || a[i] % 4 == 0) ` `            ``d++; ` ` `  `        ``// Variable to compute the product ` `        ``sum = a[i]; ` ` `  `        ``// Finding the remaining subsequences ` `        ``for` `(j = i + 1; j < n; j++) { ` `            ``sum = sum * a[j]; ` ` `  `            ``// Check if that number can be ` `            ``// expressed as the square of ` `            ``// difference of two numbers ` `            ``if` `(sum % 2 != 0 || sum % 4 == 0) ` `                ``c++; ` `        ``} ` `        ``sum = 1; ` `    ``} ` ` `  `    ``// Return the number of subsequences ` `    ``return` `c + d; ` `} ` ` `  `// Driver code ` `int` `main() ` `{ ` `    ``int` `arr[] = { 5, 4, 2, 9, 8 }; ` `    ``int` `n = ``sizeof``(arr) / ``sizeof``(arr); ` ` `  `    ``cout << CntcontSubs(arr, n); ` ` `  `    ``return` `0; ` `} `

## Java

 `// Java implementation to count the ` `// number of contiguous subsequences ` `// whose product can be expressed as ` `// the square of difference of two integers ` ` `  `class` `GFG{ ` `  `  `// Function to count the number ` `// of contiguous subsequences ` `// whose product can be expressed ` `// as square of difference of two integers ` `static` `int` `CntcontSubs(``int` `a[], ``int` `n) ` `{ ` `    ``int` `c = ``0``, d = ``0``, i, sum = ``1``, j; ` `  `  `    ``// Iterating through the array ` `    ``for` `(i = ``0``; i < n; i++) { ` `  `  `        ``// Check if that number can be ` `        ``// expressed as the square of ` `        ``// difference of two numbers ` `        ``if` `(a[i] % ``2` `!= ``0` `|| a[i] % ``4` `== ``0``) ` `            ``d++; ` `  `  `        ``// Variable to compute the product ` `        ``sum = a[i]; ` `  `  `        ``// Finding the remaining subsequences ` `        ``for` `(j = i + ``1``; j < n; j++) { ` `            ``sum = sum * a[j]; ` `  `  `            ``// Check if that number can be ` `            ``// expressed as the square of ` `            ``// difference of two numbers ` `            ``if` `(sum % ``2` `!= ``0` `|| sum % ``4` `== ``0``) ` `                ``c++; ` `        ``} ` `        ``sum = ``1``; ` `    ``} ` `  `  `    ``// Return the number of subsequences ` `    ``return` `c + d; ` `} ` `  `  `// Driver code ` `public` `static` `void` `main(String[] args) ` `{ ` `    ``int` `arr[] = { ``5``, ``4``, ``2``, ``9``, ``8` `}; ` `    ``int` `n = arr.length; ` `  `  `    ``System.out.print(CntcontSubs(arr, n)); ` `  `  `} ` `} ` ` `  `// This code contributed by PrinciRaj1992 `

## Python3

 `# Python3 implementation to count the ` `# number of contiguous subsequences ` `# whose product can be expressed as ` `# the square of difference of two integers ` ` `  `# Function to count the number ` `# of contiguous subsequences ` `# whose product can be expressed ` `# as square of difference of two integers ` `def` `CntcontSubs(a, n): ` `    ``c ``=` `0` `    ``d ``=` `0` `    ``sum` `=` `1` ` `  `    ``# Iterating through the array ` `    ``for` `i ``in` `range``(n): ` ` `  `        ``# Check if that number can be ` `        ``# expressed as the square of ` `        ``# difference of two numbers ` `        ``if` `(a[i] ``%` `2` `!``=` `0` `or` `a[i] ``%` `4` `=``=` `0``): ` `            ``d ``+``=` `1` ` `  `        ``# Variable to compute the product ` `        ``sum` `=` `a[i] ` ` `  `        ``# Finding the remaining subsequences ` `        ``for` `j ``in` `range``(i ``+` `1``, n): ` `            ``sum` `=` `sum` `*` `a[j] ` ` `  `            ``# Check if that number can be ` `            ``# expressed as the square of ` `            ``# difference of two numbers ` `            ``if` `(``sum` `%` `2` `!``=` `0` `or` `sum` `%` `4` `=``=` `0``): ` `                ``c ``+``=` `1` `        ``sum` `=` `1` ` `  `    ``# Return the number of subsequences ` `    ``return` `c ``+` `d ` ` `  `# Driver code ` `if` `__name__ ``=``=` `'__main__'``: ` `    ``arr``=``[``5``, ``4``, ``2``, ``9``, ``8``] ` `    ``n ``=` `len``(arr) ` ` `  `    ``print``(CntcontSubs(arr, n)) ` ` `  `# This code is contributed by mohit kumar 29 `

## C#

 `// C# implementation to count the  ` `// number of contiguous subsequences  ` `// whose product can be expressed as  ` `// the square of difference of two integers  ` `using` `System; ` ` `  `class` `GFG{  ` ` `  `// Function to count the number  ` `// of contiguous subsequences  ` `// whose product can be expressed  ` `// as square of difference of two integers  ` `static` `int` `CntcontSubs(``int` `[]a, ``int` `n)  ` `{  ` `    ``int` `c = 0, d = 0, i, sum = 1, j;  ` ` `  `    ``// Iterating through the array  ` `    ``for``(i = 0; i < n; i++)  ` `    ``{  ` `         `  `       ``// Check if that number can be  ` `       ``// expressed as the square of  ` `       ``// difference of two numbers  ` `       ``if` `(a[i] % 2 != 0 || a[i] % 4 == 0)  ` `           ``d++;  ` `            `  `       ``// Variable to compute the product  ` `       ``sum = a[i];  ` `        `  `       ``// Finding the remaining subsequences  ` `       ``for``(j = i + 1; j < n; j++) ` `       ``{  ` `          ``sum = sum * a[j];  ` `           `  `          ``// Check if that number can be  ` `          ``// expressed as the square of  ` `          ``// difference of two numbers  ` `          ``if` `(sum % 2 != 0 || sum % 4 == 0)  ` `              ``c++;  ` `       ``}  ` `       ``sum = 1;  ` `    ``}  ` ` `  `    ``// Return the number of subsequences  ` `    ``return` `c + d;  ` `}  ` ` `  `// Driver code  ` `static` `void` `Main()  ` `{  ` `    ``int` `[]arr = { 5, 4, 2, 9, 8 };  ` `    ``int` `n = arr.Length; ` `     `  `    ``Console.Write(CntcontSubs(arr, n));  ` `}  ` `}  ` ` `  `// This code is contributed by grand_master  `

Output:

```13
```

Time Complexity: O(N2) where N is the length of the array.

Efficient Approach: The idea lies behind the identity that a number cannot be expressed as the difference of two squares if it gives a remainder 2 when divided by 4. Therefore, the idea is to find all the subsequences which yield a product of 2 and subtract these from the total possible subsequences for the array. The total number of contiguous subsequences can be obtained by the formula: (N * (N + 1)) / 2

• If the array contains the element 0, then the product of all the subsequences containing this element becomes 0. Therefore, all those subsequences can be expressed as the difference of two squares.
• If any element gives the remainder 2 when the number is divided by 4, then all the subsequences up to the nearest 2 or 0 have to be avoided because the remainder becomes 4 when a 2 is encountered and it becomes 0 when a 0 is encountered.

Example:

• Let arr[] = {6, 5, 13, 10, 4, 8, 14, 17}.
• We compute the remainders when the elements are divided by 4. So, {2, 1, 1, 2, 0, 0, 2, 1} are the remainders of the given array.
• Here we get the remainder 2 at the index 1, 4, 7. Let’s observe the 2 at the first index.
• The following are the subsequences of the remainder array {2}, {2, 1}, {2, 1, 1}, {2, 1, 1, 2}, {2, 1, 1, 2, 0} … {2, 1, 1, 2, 0, 0, 2, 1}.
• The product of the subsequences is {2, 2, 2, 4, 0 …. 0}.
• So we get the product as 2 from index 1 to index 3. Hence, the total contiguous subsequences from index 1 to index 3 whose product is 2 are 2 (i.e.) [index-3 – index-1].
• So, clearly, we find the nearest index of the element 2 or 0 and ignore all the subsequences until this index.

Below is the implementation of the above approach:

 `// C++ implementation to count all the ` `// contiguous subsequences whose ` `// product is expressed as the square ` `// of the difference of two integers ` ` `  `#include ` `using` `namespace` `std; ` ` `  `// Function to count all the ` `// contiguous subsequences whose ` `// product is expressed as the square ` `// of the difference of two integers ` `int` `CntcontSubs(``int` `a[], ``int` `n) ` `{ ` `    ``int` `prod = 1; ` ` `  `    ``// Creating vectors to store ` `    ``// the remainders and the ` `    ``// subsequences ` `    ``vector > vect; ` ` `  `    ``vect.push_back(make_pair(0, 2)); ` ` `  `    ``vector<``int``> two, zero; ` ` `  `    ``// Iterating through the array ` `    ``for` `(``int` `i = 0; i < n; i++) { ` ` `  `        ``// Finding the remainder when the ` `        ``// element is divided by 4 ` `        ``a[i] = a[i] % 4; ` ` `  `        ``// Bringing all the elements in ` `        ``// the range [0, 3] ` `        ``if` `(a[i] < 0) ` `            ``a[i] = a[i] + 4; ` ` `  `        ``// If the remainder is 2, store ` `        ``// the index of the ` `        ``if` `(a[i] == 2) ` `            ``two.push_back(i + 1); ` ` `  `        ``// If the remainder is 2, store ` `        ``// the index of the ` `        ``if` `(a[i] == 0) ` `            ``zero.push_back(i + 1); ` ` `  `        ``if` `(a[i] == 0 || a[i] == 2) ` `            ``vect.push_back(make_pair(i + 1, a[i])); ` `    ``} ` `    ``vect.push_back(make_pair(n + 1, 2)); ` ` `  `    ``// Finding the total number of subsequences ` `    ``int` `total = (n * (n + 1)) / 2; ` ` `  `    ``// If there are no numbers which ` `    ``// yield the remainder 2 ` `    ``if` `(two.empty()) ` `        ``return` `total; ` `    ``else` `{ ` `        ``int` `sum = 0; ` ` `  `        ``int` `pos1 = -1, pos2 = -1, pos3 = -1; ` ` `  `        ``int` `sz = vect.size(); ` ` `  `        ``// Iterating through the vector ` `        ``for` `(``int` `i = 1; i + 1 < sz; i++) { ` ` `  `            ``// If the element is 2, find the nearest ` `            ``// 2 or 0 and find the number of ` `            ``// elements between them ` `            ``if` `(vect[i].second == 2) { ` `                ``sum += (vect[i].first ` `                        ``- vect[i - 1].first) ` `                           ``* (vect[i + 1].first ` `                              ``- vect[i].first) ` `                       ``- 1; ` `            ``} ` `        ``} ` ` `  `        ``// Returning the count ` `        ``return` `total - sum - two.size(); ` `    ``} ` `} ` ` `  `// Driver code ` `int` `main() ` `{ ` `    ``int` `a[] = { 5, 4, 2, 9, 8 }; ` `    ``int` `n = ``sizeof``(a) / ``sizeof``(a); ` ` `  `    ``cout << CntcontSubs(a, n); ` ` `  `    ``return` `0; ` `} `

Output:

```13
```

Time Complexity: O(N) where N is the length of the array.

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.

My Personal Notes arrow_drop_up Check out this Author's contributed articles.

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.

Article Tags :
Practice Tags :

Be the First to upvote.

Please write to us at contribute@geeksforgeeks.org to report any issue with the above content.