Count subarrays of atleast size 3 forming a Geometric Progression ( GP ) | C ++ program for the above approach ; 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 ; Driver Code ; Given array arr [ ] ; Function Call
#include <bits/stdc++.h> NEW_LINE using namespace std ; int numberOfGP ( int L [ ] , int N ) { if ( N <= 2 ) return 0 ; int count = 0 ; int res = 0 ; for ( int 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 ; } int main ( ) { int arr [ ] = { 1 , 2 , 4 , 8 , 16 , 24 } ; int N = sizeof ( arr ) / sizeof ( arr [ 0 ] ) ; cout << numberOfGP ( arr , N ) ; return 0 ; }