Count subarrays having product equal to the power of a given Prime Number | Function to check if y is a power of m or not ; Calculate log y base m and store it in a variable with integer datatype ; Calculate log y base m and store it in a variable with double datatype ; If res1 and res2 are equal , return True . Otherwise , return false ; Function to count the number of subarrays having product of elements equal to a power of m , where m is a prime number ; Stores the count of subarrays required ; Stores current sequence of consecutive array elements which are a multiple of m ; Traverse the array ; If arr [ i ] is a power of M ; Increment cnt ; Update ans ; Update cnt ; Return the count of subarrays ; Input
function isPower ( m , y ) { let res1 = parseInt ( Math . log ( y ) / Math . log ( m ) ) ; let res2 = Math . log ( y ) / Math . log ( m ) ; return ( res1 == res2 ) ; } function numSub ( arr , n , m ) { let ans = 0 ; let cnt = 0 ; for ( let i = 0 ; i < n ; i ++ ) { if ( isPower ( m , arr [ i ] ) ) { cnt ++ ; ans += parseInt ( ( cnt * ( cnt - 1 ) ) / 2 ) ; } else { cnt = 0 ; } } return ans ; } let arr = [ 1 , 1 , 1 , 3 ] ; let m = 3 ; let n = arr . length ; document . write ( numSub ( arr , n , m ) ) ;