Count subarrays of atleast size 3 forming a Geometric Progression (GP)

Given an array arr[] of N integers, the task is to find the count of all subarrays from the given array of at least size 3 forming a Geometric Progression.

Examples:  

Input: arr[] = {1, 2, 4, 8}
Output: 3
Explanation: The required subarrays forming geometric progression are: 

  1. {1, 2, 4}
  2. {2, 4, 8}
  3. {1, 2, 4, 8}

Input: arr[] = {1, 2, 4, 8, 16, 24}
Output: 6
Explanation: The required subarrays forming geometric progression are: 

  1. {1, 2, 4}
  2. {2, 4, 8}
  3. {4, 8, 16}
  4. {1, 2, 4, 8}
  5. {2, 4, 8, 16}
  6. {1, 2, 4, 8, 16}

Naive Approach: The simplest approach is to generate all the subarrays of size at least 3 and count all those subarrays forming a Geometric Progression. Print the count after checking all the subarrays.



Time Complexity: O(N3)
Auxiliary Space: O(N)

Efficient Approach: The idea is to use a property of Geometric Progression i.e., {a, b, c} is GP if and only if a*c = b. Follow the below steps to solve the problem:

  • Initialize a variable, res, and count with 0 to store the total subarrays forming geometric progression and length of the current subarray.
  • Traverse the given array over the range [2, N – 1] and increment the value of count if the current element forming a geometric progression i.e., arr[i]*arr[i – 2] = arr[i – 1]*arr[i – 1] Otherwise, set count as zero.
  • Add count to res for each iteration in the above steps.
  • After the above steps, print the value of res as the resultant count.

Below is the implementation of the above approach:

C++

filter_none

edit
close

play_arrow

link
brightness_4
code

// C++ program for the above approach
 
#include <bits/stdc++.h>
using namespace std;
 
// Function to count all the subarrays
// of size at least 3 forming GP
int numberOfGP(int L[], int N)
{
    // If array size is less than 3
    if (N <= 2)
        return 0;
 
    // Stores the count of subarray
    int count = 0;
 
    // Stores the count of subarray
    // for each iteration
    int res = 0;
 
    // Traverse the array
    for (int i = 2; i < N; ++i) {
 
        // Check if L[i] forms GP
        if (L[i - 1] * L[i - 1]
            == L[i] * L[i - 2]) {
            ++count;
        }
 
        // Otherwise, update count to 0
        else {
            count = 0;
        }
 
        // Update the final count
        res += count;
    }
 
    // Return the final count
    return res;
}
 
// Driver Code
int main()
{
    // Given array arr[]
    int arr[] = { 1, 2, 4, 8, 16, 24 };
 
    int N = sizeof(arr) / sizeof(arr[0]);
 
    // Function Call
    cout << numberOfGP(arr, N);
 
    return 0;
}

chevron_right


Java

filter_none

edit
close

play_arrow

link
brightness_4
code

// Java program for the
// above approach
import java.util.*;
class GFG{
 
// Function to count all
// the subarrays of size
// at least 3 forming GP
static int numberOfGP(int L[],
                      int N)
{
  // If array size
  // is less than 3
  if (N <= 2)
    return 0;
 
  // Stores the count
  // of subarray
  int count = 0;
 
  // Stores the count
  // of subarray for
  // each iteration
  int res = 0;
 
  // Traverse the array
  for (int i = 2; i < N; ++i)
  {
    // Check if L[i] forms GP
    if (L[i - 1] * L[i - 1] ==
        L[i] * L[i - 2])
    {
      ++count;
    }
 
    // Otherwise, update
    // count to 0
    else
    {
      count = 0;
    }
 
    // Update the
    // final count
    res += count;
  }
 
  // Return the final count
  return res;
}
 
// Driver Code
public static void main(String[] args)
{
  // Given array arr[]
  int arr[] = {1, 2, 4,
               8, 16, 24};
 
  int N = arr.length;
 
  // Function Call
  System.out.print(numberOfGP(arr, N));
}
}
 
// This code is contributed by gauravrajput1

chevron_right


Python3

filter_none

edit
close

play_arrow

link
brightness_4
code

# Python3 program for the above approach
 
# Function to count all the subarrays
# of size at least 3 forming GP
def numberOfGP(L, N):
     
    # If array size is less than 3
    if (N <= 2):
        return 0
 
    # Stores the count of subarray
    count = 0
 
    # Stores the count of subarray
    # for each iteration
    res = 0
 
    # Traverse the array
    for i in range(2, N):
 
        # Check if L[i] forms GP
        if (L[i - 1] * L[i - 1] ==
                L[i] * L[i - 2]):
            count += 1
 
        # Otherwise, update count to 0
        else:
            count = 0
 
        # Update the final count
        res += count
 
    # Return the final count
    return res
 
# Driver Code
if __name__ == '__main__':
     
    # Given array arr[]
    arr = [ 1, 2, 4, 8, 16, 24 ]
 
    N = len(arr)
 
    # Function Call
    print(numberOfGP(arr, N))
 
# This code is contributed by mohit kumar 29

chevron_right


C#

filter_none

edit
close

play_arrow

link
brightness_4
code

// C# program for the
// above approach
using System;
class GFG {
 
// Function to count all
// the subarrays of size
// at least 3 forming GP
static int numberOfGP(int[] L,
                      int N)
{
  // If array size
  // is less than 3
  if (N <= 2)
    return 0;
 
  // Stores the count
  // of subarray
  int count = 0;
 
  // Stores the count
  // of subarray for
  // each iteration
  int res = 0;
 
  // Traverse the array
  for (int i = 2; i < N; ++i)
  {
    // Check if L[i] forms GP
    if (L[i - 1] * L[i - 1] ==
        L[i] * L[i - 2])
    {
      ++count;
    }
 
    // Otherwise, update
    // count to 0
    else
    {
      count = 0;
    }
 
    // Update the
    // final count
    res += count;
  }
 
  // Return the final
  // count
  return res;
}
 
// Driver Code
public static void Main(String[] args)
{
  // Given array arr[]
  int[] arr = {1, 2, 4, 8, 16, 24};
 
  int N = arr.Length;
 
  // Function Call
  Console.Write(numberOfGP(arr, N));
}
}
 
// This code is contributed by Chitranayal

chevron_right


Output

6

Time Complexity: O(N)
Auxiliary Space: O(N)

Attention reader! Don’t stop learning now. Get hold of all the important DSA concepts with the DSA Self Paced Course at a student-friendly price and become industry ready.




My Personal Notes arrow_drop_up

Check out this Author's contributed articles.

If you like GeeksforGeeks and would like to contribute, you can also write an article using contribute.geeksforgeeks.org or mail your article to contribute@geeksforgeeks.org. See your article appearing on the GeeksforGeeks main page and help other Geeks.

Please Improve this article if you find anything incorrect by clicking on the "Improve Article" button below.