// C# program to find count of
// subarrays whose product can be
// represented as the difference
// between two different numbers
using System;
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
Console.WriteLine(total);
}
static void Main()
{
// Array initialisation
int[] arr = {2, 5, 6};
int size = arr.Length;
numberOfSubarrays(arr, size);
}
}
// This code is contributed by divyeshrabadiya07