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 he 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++
// C++ Program to implement // the above approach #include <bits/stdc++.h> using namespace std; // Function to return the length of // the longest subarray forming a // GP in a sorted array int longestGP(int A[], int N) { // Base Case if (N < 2) return N; // Stores the length of GP // and the common ratio int length = 1, common_ratio = 1; // Stores the maximum // length of the GP int maxlength = 1; // Traverse the array for (int i = 0; i < N - 1; i++) { // Check if the common ratio // is valid for GP if (A[i + 1] % A[i] == 0) { // If the current common ratio // is equal to previous common ratio if (A[i + 1] / A[i] == common_ratio) { // Increment the length of the GP length = length + 1; // Store the max length of GP maxlength = max(maxlength, length); } // Otherwise else { // Update the common ratio common_ratio = A[i + 1] / A[i]; // Update the length of GP length = 2; } } else { // Store the max length of GP maxlength = max(maxlength, length); // Update the length of GP length = 1; } } // Store the max length of GP maxlength = max(maxlength, length); // Return the max length of GP return maxlength; } // Driver Code int main() { // Given array int arr[] = { 1, 2, 4, 7, 14, 28, 56, 89 }; // Length of the array int N = sizeof(arr) / sizeof(arr[0]); // Function Call cout << longestGP(arr, N); return 0; } |
Java
// Java program to implement // the above approach import java.io.*; class GFG{ // Function to return the length of // the longest subarray forming a // GP in a sorted array static int longestGP(int A[], int N) { // Base Case if (N < 2) return N; // Stores the length of GP // and the common ratio int length = 1, common_ratio = 1; // Stores the maximum // length of the GP int maxlength = 1; // Traverse the array for(int i = 0; i < N - 1; i++) { // Check if the common ratio // is valid for GP if (A[i + 1] % A[i] == 0) { // If the current common ratio // is equal to previous common ratio if (A[i + 1] / A[i] == common_ratio) { // Increment the length of the GP length = length + 1; // Store the max length of GP maxlength = Math.max(maxlength, length); } // Otherwise else { // Update the common ratio common_ratio = A[i + 1] / A[i]; // Update the length of GP length = 2; } } else { // Store the max length of GP maxlength = Math.max(maxlength, length); // Update the length of GP length = 1; } } // Store the max length of GP maxlength = Math.max(maxlength, length); // Return the max length of GP return maxlength; } // Driver code public static void main (String[] args) { // Given array int arr[] = { 1, 2, 4, 7, 14, 28, 56, 89 }; // Length of the array int N = arr.length; // Function call System.out.println(longestGP(arr, N)); } } // This code is contributed by jana_sayantan |
Python3
# Python3 program to implement # the above approach # Function to return the length of # the longest subarray forming a # GP in a sorted array def longestGP(A, N): # Base Case if (N < 2): return N # Stores the length of GP # and the common ratio length = 1 common_ratio = 1 # Stores the maximum # length of the GP maxlength = 1 # Traverse the array for i in range(N - 1): # Check if the common ratio # is valid for GP if (A[i + 1] % A[i] == 0): # If the current common ratio # is equal to previous common ratio if (A[i + 1] // A[i] == common_ratio): # Increment the length of the GP length = length + 1 # Store the max length of GP maxlength = max(maxlength, length) # Otherwise else: # Update the common ratio common_ratio = A[i + 1] // A[i] # Update the length of GP length = 2 else: # Store the max length of GP maxlength = max(maxlength, length) # Update the length of GP length = 1 # Store the max length of GP maxlength = max(maxlength, length) # Return the max length of GP return maxlength # Driver Code # Given array arr = [ 1, 2, 4, 7, 14, 28, 56, 89 ] # Length of the array N = len(arr) # Function call print(longestGP(arr, N)) # This code is contributed by sanjoy_62 |
C#
// C# program to implement // the above approach using System; class GFG{ // Function to return the length of // the longest subarray forming a // GP in a sorted array static int longestGP(int []A, int N) { // Base Case if (N < 2) return N; // Stores the length of GP // and the common ratio int length = 1, common_ratio = 1; // Stores the maximum // length of the GP int maxlength = 1; // Traverse the array for(int i = 0; i < N - 1; i++) { // Check if the common ratio // is valid for GP if (A[i + 1] % A[i] == 0) { // If the current common ratio // is equal to previous common ratio if (A[i + 1] / A[i] == common_ratio) { // Increment the length of the GP length = length + 1; // Store the max length of GP maxlength = Math.Max(maxlength, length); } // Otherwise else { // Update the common ratio common_ratio = A[i + 1] / A[i]; // Update the length of GP length = 2; } } else { // Store the max length of GP maxlength = Math.Max(maxlength, length); // Update the length of GP length = 1; } } // Store the max length of GP maxlength = Math.Max(maxlength, length); // Return the max length of GP return maxlength; } // Driver code public static void Main(String[] args) { // Given array int []arr = {1, 2, 4, 7, 14, 28, 56, 89}; // Length of the array int N = arr.Length; // Function call Console.WriteLine(longestGP(arr, N)); } } // This code is contributed by shikhasingrajput |
4
Time Complexity: O(N)
Space Complexity: O(1)
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.
Recommended Posts:
- Longest subarray forming an Arithmetic Progression (AP)
- Longest subsequence forming an Arithmetic Progression (AP)
- Count of subarrays forming an Arithmetic Progression (AP)
- Count subarrays of atleast size 3 forming a Geometric Progression (GP)
- Forming triangles using points on a square
- Forming smallest array with given constraints
- Minimum colors required such that edges forming cycle do not have same color
- Possible pairs forming a Pythagorean Triple with a given value
- Count of ways to split N into Triplets forming a Triangle
- Longest Arithmetic Progression | DP-35
- Longest arithmetic progression with the given common difference
- Longest Geometric Progression
- Find all triplets in a sorted array that forms Geometric Progression
- Removing a number from array to make it Geometric Progression
- Check whether Arithmetic Progression can be formed from the given array
- Find the missing number in Geometric Progression
- Minimum De-arrangements present in array of AP (Arithmetic Progression)
- Harmonic progression Sum
- Program for N-th term of Geometric Progression series
- Program for N-th term of Arithmetic Progression series
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.

