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, 2, 4}
- {2, 4, 8}
- {1, 2, 4, 8}
Input: arr[] = {1, 2, 4, 8, 16, 24}
Output: 6
Explanation: The required subarrays forming geometric progression are:
- {1, 2, 4}
- {2, 4, 8}
- {4, 8, 16}
- {1, 2, 4, 8}
- {2, 4, 8, 16}
- {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++
#include <bits/stdc++.h>
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;
}
|
Java
import java.util.*;
class GFG{
static 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;
}
public static void main(String[] args)
{
int arr[] = { 1 , 2 , 4 ,
8 , 16 , 24 };
int N = arr.length;
System.out.print(numberOfGP(arr, N));
}
}
|
Python3
def numberOfGP(L, N):
if (N < = 2 ):
return 0
count = 0
res = 0
for i in range ( 2 , N):
if (L[i - 1 ] * L[i - 1 ] = =
L[i] * L[i - 2 ]):
count + = 1
else :
count = 0
res + = count
return res
if __name__ = = '__main__' :
arr = [ 1 , 2 , 4 , 8 , 16 , 24 ]
N = len (arr)
print (numberOfGP(arr, N))
|
C#
using System;
class GFG {
static 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;
}
public static void Main(String[] args)
{
int [] arr = {1, 2, 4, 8, 16, 24};
int N = arr.Length;
Console.Write(numberOfGP(arr, N));
}
}
|
Javascript
<script>
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));
</script>
|
Time Complexity: O(N)
Auxiliary Space: O(1) as it is using constant variables