Longest subarray forming a Geometric Progression (GP)
Given a sorted array arr[] consisting of distinct numbers, the task is to find the length of the longest subarray that forms a Geometric Progression.
Examples:
Input: arr[]={1, 2, 4, 7, 14, 28, 56, 89}
Output: 4
Explanation:
The subarrays {1, 2, 4} and {7, 14, 28, 56} forms a GP.
Since {7, 14, 28, 56} is the longest, the required output is 4.
Input: arr[]={3, 6, 7, 12, 24, 28, 56}
Output: 2
Naive Approach: The simplest approach to solve the problem is to generate all possible subarrays and for each subarray, check if it forms a GP or not. Keep updating the maximum length of such subarrays found. Finally, print the maximum length obtained.
Time Complexity: O(N3)
Auxiliary Space: O(N)
Efficient Approach: The above approach can be optimized by the following steps:
- Traverse the array and select a pair of adjacent elements, i.e., arr[i] and arr[i+1], as the first two terms of the Geometric Progression.
- If arr[i+1] is not divisible by arr[i], then it cannot be considered for the common ratio. Otherwise, take arr[i+1] / arr[i] as the common ratio for the current Geometric Progression.
- Increase and store the length of the Geometric Progression if the subsequent elements have the same common ratio. Otherwise, update the common ratio equal to the ratio of the new pair of adjacent elements.
- Finally, return the length of the longest subarray that forms a Geometric Progression as the output.
Below is the implementation of the above approach:
C++
#include <bits/stdc++.h>
using namespace std;
int longestGP( int A[], int N)
{
if (N < 2)
return N;
int length = 1, common_ratio = 1;
int maxlength = 1;
for ( int i = 0; i < N - 1; i++) {
if (A[i + 1] % A[i] == 0) {
if (A[i + 1] / A[i] == common_ratio) {
length = length + 1;
maxlength
= max(maxlength, length);
}
else {
common_ratio = A[i + 1] / A[i];
length = 2;
}
}
else {
maxlength
= max(maxlength, length);
length = 1;
}
}
maxlength = max(maxlength, length);
return maxlength;
}
int main()
{
int arr[] = { 1, 2, 4, 7, 14, 28, 56, 89 };
int N = sizeof (arr) / sizeof (arr[0]);
cout << longestGP(arr, N);
return 0;
}
|
Java
import java.io.*;
class GFG{
static int longestGP( int A[], int N)
{
if (N < 2 )
return N;
int length = 1 , common_ratio = 1 ;
int maxlength = 1 ;
for ( int i = 0 ; i < N - 1 ; i++)
{
if (A[i + 1 ] % A[i] == 0 )
{
if (A[i + 1 ] / A[i] == common_ratio)
{
length = length + 1 ;
maxlength = Math.max(maxlength, length);
}
else
{
common_ratio = A[i + 1 ] / A[i];
length = 2 ;
}
}
else
{
maxlength = Math.max(maxlength, length);
length = 1 ;
}
}
maxlength = Math.max(maxlength, length);
return maxlength;
}
public static void main (String[] args)
{
int arr[] = { 1 , 2 , 4 , 7 , 14 , 28 , 56 , 89 };
int N = arr.length;
System.out.println(longestGP(arr, N));
}
}
|
Python3
def longestGP(A, N):
if (N < 2 ):
return N
length = 1
common_ratio = 1
maxlength = 1
for i in range (N - 1 ):
if (A[i + 1 ] % A[i] = = 0 ):
if (A[i + 1 ] / / A[i] = = common_ratio):
length = length + 1
maxlength = max (maxlength, length)
else :
common_ratio = A[i + 1 ] / / A[i]
length = 2
else :
maxlength = max (maxlength, length)
length = 1
maxlength = max (maxlength, length)
return maxlength
arr = [ 1 , 2 , 4 , 7 , 14 , 28 , 56 , 89 ]
N = len (arr)
print (longestGP(arr, N))
|
C#
using System;
class GFG{
static int longestGP( int []A, int N)
{
if (N < 2)
return N;
int length = 1, common_ratio = 1;
int maxlength = 1;
for ( int i = 0; i < N - 1; i++)
{
if (A[i + 1] % A[i] == 0)
{
if (A[i + 1] / A[i] == common_ratio)
{
length = length + 1;
maxlength = Math.Max(maxlength,
length);
}
else
{
common_ratio = A[i + 1] /
A[i];
length = 2;
}
}
else
{
maxlength = Math.Max(maxlength,
length);
length = 1;
}
}
maxlength = Math.Max(maxlength,
length);
return maxlength;
}
public static void Main(String[] args)
{
int []arr = {1, 2, 4, 7,
14, 28, 56, 89};
int N = arr.Length;
Console.WriteLine(longestGP(arr, N));
}
}
|
Javascript
<script>
function longestGP(A, N)
{
if (N < 2)
return N;
let length = 1, common_ratio = 1;
let maxlength = 1;
for (let i = 0; i < N - 1; i++)
{
if (A[i + 1] % A[i] == 0)
{
if (A[i + 1] / A[i] == common_ratio)
{
length = length + 1;
maxlength = Math.max(maxlength, length);
}
else
{
common_ratio = A[i + 1] / A[i];
length = 2;
}
}
else
{
maxlength = Math.max(maxlength, length);
length = 1;
}
}
maxlength = Math.max(maxlength, length);
return maxlength;
}
let arr = [ 1, 2, 4, 7, 14, 28, 56, 89 ];
let N = arr.length;
document.write(longestGP(arr, N));
</script>
|
Time Complexity: O(N)
Space Complexity: O(1)
Last Updated :
15 Jun, 2022
Like Article
Save Article
Share your thoughts in the comments
Please Login to comment...