Count of subsequences whose product is a difference of square of two integers | Function to count all the contiguous subsequences whose product is expressed as the square of the difference of two integers ; Creating vectors to store the remainders and the subsequences ; Iterating through the array ; Finding the remainder when the element is divided by 4 ; Bringing all the elements in the range [ 0 , 3 ] ; If the remainder is 2 , store the index of the ; If the remainder is 2 , store the index of the ; Finding the total number of subsequences ; If there are no numbers which yield the remainder 2 ; Iterating through the vector ; If the element is 2 , find the nearest 2 or 0 and find the number of elements between them ; Returning the count ; Driver code
function CntcontSubs ( a , n ) { let prod = 1 ; let vect = [ ] ; vect . push ( [ 0 , 2 ] ) ; let two = [ ] ; let zero = [ ] ; for ( let i = 0 ; i < n ; i ++ ) { a [ i ] = a [ i ] % 4 ; if ( a [ i ] < 0 ) a [ i ] = a [ i ] + 4 ; if ( a [ i ] == 2 ) two . push ( i + 1 ) ; if ( a [ i ] == 0 ) zero . push ( i + 1 ) ; if ( a [ i ] == 0 a [ i ] == 2 ) vect . push ( [ i + 1 , a [ i ] ] ) ; } vect . push ( [ n + 1 , 2 ] ) ; let total = Math . floor ( ( n * ( n + 1 ) ) / 2 ) ; if ( two . length == 0 ) return total ; else { let sum = 0 ; let pos1 = - 1 , pos2 = - 1 , pos3 = - 1 ; let sz = vect . length ; for ( let i = 1 ; i + 1 < sz ; i ++ ) { if ( vect [ i ] [ 1 ] == 2 ) { sum += ( vect [ i ] [ 0 ] - vect [ i - 1 ] [ 0 ] ) * ( vect [ i + 1 ] [ 0 ] - vect [ i ] [ 0 ] ) - 1 ; } } return total - sum - two . length ; } } let a = [ 5 , 4 , 2 , 9 , 8 ] ; let n = a . length ; document . write ( CntcontSubs ( a , n ) ) ;