Given a **non-negative array a**, the task is to find the count of subarrays whose product of elements can be represented as the difference of two different numbers. **Examples:**

Input:arr = {2, 5, 6}Output:2Explanation:

Product of elements of subarray {5} can be represented as 3^{2}– 2^{2}is equal to 5

Product of elements of subarray {2, 5, 6} can be represented as 8^{2}– 2^{2}is equal to 60

Hence, there are two subarrays which can be represented.

Input:arr = {1, 2, 3}Output:2

**Naive Approach:**

The naive solution to the above-mentioned question is to compute all the possible subarray from the given array. Then we have to compute the product of each subarray. But this method is not so efficient and is time-consuming.

**Efficient approach:**

A common observation of the efficient approach to the above problem is that a number which is divisible by 2 and not by 4 gives remainder 2 when divided by 4. Hence, all the numbers can be represented as a product of two different numbers except the numbers which give the remainder 2 when done modulo with 4. Now to solve the problem we take a pair of vector and store elements along with the position of the next element which is divisible by 2. After that traverse the array and look for the necessary conditions given below:

- If an odd number is encountered then this number forms all subarrays unless a number occurs which is divisible by 2. Now, this number also can form subarrays when another number occurs which is divisible by 2. Both of these are stored in pair type vector.
- If a number is encountered which is divisible by 4 then this number can form all subarrays.
- If a number occurs which is only divisible by 2 then this number cannot form subarray unless another number occurs which is a multiple of 2.

Below is the implementation of the above approach:

## C++

`// C++ program to Find count of ` `// Subarrays whose product can be ` `// represented as the difference between ` `// two different numbers ` ` ` `#include <bits/stdc++.h> ` `using` `namespace` `std; ` ` ` `// Function to print number of subarrays ` `void` `numberOfSubarrays(` `int` `arr[], ` `int` `n) ` `{ ` ` ` ` ` `vector<pair<` `int` `, ` `int` `> > next(n); ` ` ` `vector<pair<` `int` `, ` `int` `> > next_to_next(n); ` ` ` ` ` `int` `f = -1; ` ` ` `int` `s = -1; ` ` ` ` ` `for` `(` `int` `i = n - 1; i >= 0; i--) { ` ` ` `next[i].first = arr[i]; ` ` ` ` ` `next_to_next[i].first = arr[i]; ` ` ` ` ` `// check if number is divisible by 2 ` ` ` `if` `(arr[i] % 2 == 0) { ` ` ` `s = f; ` ` ` `f = i; ` ` ` `} ` ` ` ` ` `// Store the position ` ` ` `// of the next element ` ` ` `next[i].second = f; ` ` ` ` ` `// Store the position of ` ` ` `// next to next element ` ` ` `// which is multiple of 2 ` ` ` `next_to_next[i].second = s; ` ` ` `} ` ` ` ` ` `int` `total = 0; ` ` ` ` ` `for` `(` `int` `i = 0; i < n; i++) { ` ` ` `int` `calculate; ` ` ` ` ` `// Check if the element is divisible ` ` ` `// is divisible by 4 ` ` ` `if` `(next[i].first % 4 == 0) { ` ` ` `calculate = n - i; ` ` ` ` ` `total += calculate; ` ` ` `} ` ` ` ` ` `// Check if current element ` ` ` `// is an odd number ` ` ` `else` `if` `(next[i].first & 1 == 1) { ` ` ` ` ` `if` `(next[i].second == -1) { ` ` ` `calculate = n - i; ` ` ` ` ` `total += calculate; ` ` ` `} ` ` ` ` ` `else` `{ ` ` ` ` ` `// check if after the current element ` ` ` `// only 1 element exist which is a ` ` ` `// multiple of only 2 but not 4 ` ` ` `if` `(next_to_next[i].second == -1 ` ` ` `&& next[next[i].second].first % 4 != 0) ` ` ` ` ` `{ ` ` ` `calculate = next[i].second - i; ` ` ` `total += calculate; ` ` ` `} ` ` ` ` ` `// Check if after the current element an element exist ` ` ` `// which is multiple of only 2 and not 4 and after that ` ` ` `// an element also exist which is multiple of 2 ` ` ` `else` `if` `(next_to_next[i].second != -1 ` ` ` `&& next[next[i].second].first % 4 != 0) { ` ` ` `calculate = n - i; ` ` ` `total += calculate; ` ` ` `total -= next_to_next[i].second - next[i].second; ` ` ` `} ` ` ` ` ` `// All subarrays can be formed by current element ` ` ` `else` `{ ` ` ` `calculate = n - i; ` ` ` `total = total + calculate; ` ` ` `} ` ` ` `} ` ` ` `} ` ` ` ` ` `// Condition for an even number ` ` ` `else` `{ ` ` ` ` ` `// Check if next element does not ` ` ` `// exist which is multiple of 2 ` ` ` `if` `(next_to_next[i].second == -1) ` ` ` `total = total; ` ` ` ` ` `// Check if next element exist ` ` ` `// which is multiple of 2 ` ` ` `else` `{ ` ` ` `calculate = n - i; ` ` ` `total += calculate; ` ` ` `total = total - next_to_next[i].second + i; ` ` ` `} ` ` ` `} ` ` ` `} ` ` ` ` ` `// Print the output ` ` ` `cout << total << ` `"\n"` `; ` `} ` ` ` `// Driver Code ` `int` `main() ` `{ ` ` ` `// array initialisation ` ` ` `int` `arr[] = { 2, 5, 6 }; ` ` ` ` ` `int` `size = ` `sizeof` `(arr) / ` `sizeof` `(arr[0]); ` ` ` ` ` `numberOfSubarrays(arr, size); ` ` ` ` ` `return` `0; ` `} ` |

*chevron_right*

*filter_none*

## Java

`// Java program to find count of ` `// subarrays whose product can be ` `// represented as the difference ` `// between two different numbers ` `import` `java.io.*; ` `import` `java.util.*; ` ` ` `class` `GFG{ ` ` ` `// Function to print number of subarrays ` `static` `void` `numberOfSubarrays(` `int` `arr[], ` `int` `n) ` `{ ` ` ` `int` `[][] next = ` `new` `int` `[n][` `2` `]; ` ` ` `int` `[][] next_to_next = ` `new` `int` `[n][` `2` `]; ` ` ` ` ` `int` `f = -` `1` `; ` ` ` `int` `s = -` `1` `; ` ` ` ` ` `for` `(` `int` `i = n - ` `1` `; i >= ` `0` `; i--) ` ` ` `{ ` ` ` `next[i][` `0` `] = arr[i]; ` ` ` ` ` `next_to_next[i][` `0` `] = arr[i]; ` ` ` ` ` `// Check if number is divisible by 2 ` ` ` `if` `(arr[i] % ` `2` `== ` `0` `) ` ` ` `{ ` ` ` `s = f; ` ` ` `f = i; ` ` ` `} ` ` ` ` ` `// Store the position ` ` ` `// of the next element ` ` ` `next[i][` `1` `] = f; ` ` ` ` ` `// Store the position of ` ` ` `// next to next element ` ` ` `// which is multiple of 2 ` ` ` `next_to_next[i][` `1` `] = s; ` ` ` `} ` ` ` ` ` `int` `total = ` `0` `; ` ` ` ` ` `for` `(` `int` `i = ` `0` `; i < n; i++) ` ` ` `{ ` ` ` `int` `calculate; ` ` ` ` ` `// Check if the element is divisible ` ` ` `// is divisible by 4 ` ` ` `if` `(next[i][` `0` `] % ` `4` `== ` `0` `) ` ` ` `{ ` ` ` `calculate = n - i; ` ` ` `total += calculate; ` ` ` `} ` ` ` ` ` `// Check if current element ` ` ` `// is an odd number ` ` ` `else` `if` `((next[i][` `0` `] & ` `1` `) == ` `1` `) ` ` ` `{ ` ` ` `if` `(next[i][` `1` `] == -` `1` `) ` ` ` `{ ` ` ` `calculate = n - i; ` ` ` `total += calculate; ` ` ` `} ` ` ` ` ` `else` ` ` `{ ` ` ` ` ` `// Check if after the current element ` ` ` `// only 1 element exist which is a ` ` ` `// multiple of only 2 but not 4 ` ` ` `if` `(next_to_next[i][` `1` `] == -` `1` `&& ` ` ` `next[next[i][` `1` `]][` `0` `] % ` `4` `!= ` `0` `) ` ` ` `{ ` ` ` `calculate = next[i][` `1` `] - i; ` ` ` `total += calculate; ` ` ` `} ` ` ` ` ` `// Check if after the current element ` ` ` `// an element exist which is multiple ` ` ` `// of only 2 and not 4 and after that ` ` ` `// an element also exist which is ` ` ` `// multiple of 2 ` ` ` `else` `if` `(next_to_next[i][` `1` `] != -` `1` `&& ` ` ` `next[next[i][` `1` `]][` `0` `] % ` `4` `!= ` `0` `) ` ` ` `{ ` ` ` `calculate = n - i; ` ` ` `total += calculate; ` ` ` `total -= next_to_next[i][` `1` `] - ` ` ` `next[i][` `1` `]; ` ` ` `} ` ` ` ` ` `// All subarrays can be formed ` ` ` `// by current element ` ` ` `else` ` ` `{ ` ` ` `calculate = n - i; ` ` ` `total = total + calculate; ` ` ` `} ` ` ` `} ` ` ` `} ` ` ` ` ` `// Condition for an even number ` ` ` `else` ` ` `{ ` ` ` ` ` `// Check if next element does not ` ` ` `// exist which is multiple of 2 ` ` ` `if` `(next_to_next[i][` `1` `] == -` `1` `) ` ` ` `total = total; ` ` ` ` ` `// Check if next element exist ` ` ` `// which is multiple of 2 ` ` ` `else` ` ` `{ ` ` ` `calculate = n - i; ` ` ` `total += calculate; ` ` ` `total = total - next_to_next[i][` `1` `] + i; ` ` ` `} ` ` ` `} ` ` ` `} ` ` ` ` ` `// Print the output ` ` ` `System.out.println(total); ` `} ` ` ` `// Driver Code ` `public` `static` `void` `main(String args[]) ` `{ ` ` ` ` ` `// Array initialisation ` ` ` `int` `arr[] = { ` `2` `, ` `5` `, ` `6` `}; ` ` ` ` ` `int` `size = arr.length; ` ` ` ` ` `numberOfSubarrays(arr, size); ` `} ` `} ` ` ` `// This code is contributed by offbeat ` |

*chevron_right*

*filter_none*

**Output:**

2

**Time complexity:** O(N)

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
- Find an element which divides the array in two subarrays with equal product
- Count of common subarrays in two different permutations of 1 to N
- Count of subsequences whose product is a difference of square of two integers
- Count of total subarrays whose sum is a Fibonacci Numbers
- Count of pairs upto N such whose LCM is not equal to their product for Q queries
- Count subarrays with sum as difference of squares of two numbers
- Count subarrays with equal number of occurrences of two given elements
- Number of subarrays for which product and sum are equal
- Find all possible subarrays having product less than or equal to K
- Count of numbers whose difference with Fibonacci count upto them is atleast K
- Count pairs from two sorted arrays whose sum is equal to a given value x
- Count of subarrays whose maximum element is greater than k
- Count of subarrays whose products don't have any repeating prime factor
- Count of subarrays whose sum is a perfect square
- Maximum product of sum of two contiguous subarrays of an array
- Subsets of size K with product equal to difference of two perfect squares
- Maximum product from array such that frequency sum of all repeating elements in product is less than or equal to 2 * k
- Program to find the maximum difference between the index of any two different numbers
- Find if array can be divided into two subarrays of equal sum

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.