Count subarrays of atleast size 3 forming a Geometric Progression ( GP ) | Function to count all the subarrays of size at least 3 forming GP ; If array size is less than 3 ; Stores the count of subarray ; Stores the count of subarray for each iteration ; Traverse the array ; Check if L [ i ] forms GP ; Otherwise , update count to 0 ; Update the final count ; Return the final count ; Given array arr [ ] ; Function Call
function numberOfGP ( L , N ) { if ( N <= 2 ) return 0 ; let count = 0 ; let res = 0 ; for ( let i = 2 ; i < N ; ++ i ) { if ( L [ i - 1 ] * L [ i - 1 ] == L [ i ] * L [ i - 2 ] ) { ++ count ; } else { count = 0 ; } res += count ; } return res ; } let arr = [ 1 , 2 , 4 , 8 , 16 , 24 ] ; let N = arr . length ; document . write ( numberOfGP ( arr , N ) ) ;