Given an array arr of N positive elements, the task is to find whether it is possible to convert this array into Geometric Progression (GP) by removing at-most one element. If yes, then find index of the number removing which converts the array into a geometric progression.
Special Cases :
1) If whole array is already in GP, then return any index.
2) If it is not possible to convert array into GP, then print “Not possible”.
Input : arr = [2, 4, 8, 24, 16, 32] Output : 3 Number to remove is arr, i.e., 24 After removing 24 array will be [2, 4, 8, 16, 32] which is a GP with starting value 2 and common ratio 2. Input : arr = [2, 8, 30, 60] Output : Not Possible
We can solve this problem by handling some special cases and then finding the pivot element, removing which makes array a GP. First we will check that our pivot element is first or second element, if not then the multiplier between them will be common ration of our GP, if yes then we found our solution.
Once we get common ratio of GP, we can check array element with this ratio, if this ratio violates at some index, then we skip this element and check from next index whether it is a continuation of previous GP or not.
In below code a method isGP is implemented which checks array to be GP after removing element at index ‘index’. This method is written for special case handling of first, second and last element.
Please see below code for better understanding.
Remove 30 to get geometric progression
This article is contributed by Utkarsh Trivedi. If you like GeeksforGeeks and would like to contribute, you can also write an article using contribute.geeksforgeeks.org or mail your article to firstname.lastname@example.org. See your article appearing on the GeeksforGeeks main page and help other Geeks.
Please write comments if you find anything incorrect, or you want to share more information about the topic discussed above.
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.
- Number of GP (Geometric Progression) subsequences of size 3
- Minimum number of operations to convert a given sequence into a Geometric Progression
- Program for N-th term of Geometric Progression series
- Program to print GP (Geometric Progression)
- Geometric Progression
- Check whether nodes of Binary Tree form Arithmetic, Geometric or Harmonic Progression
- Sum of N-terms of geometric progression for larger values of N | Set 2 (Using recursion)
- Sum of elements of a Geometric Progression (GP) in a given range
- Change one element in the given array to make it an Arithmetic Progression
- Number of terms in Geometric Series with given conditions
- Minimum De-arrangements present in array of AP (Arithmetic Progression)
- Find the missing number in Arithmetic Progression
- Find the missing number in unordered Arithmetic Progression
- Program for sum of geometric series
- Geometric mean (Two Methods)
- Find Harmonic mean using Arithmetic mean and Geometric mean
- Sum of Arithmetic Geometric Sequence
- Find N Geometric Means between A and B
- Integer part of the geometric mean of the divisors of N
- Product of N terms of a given Geometric series
Improved By : sanjeev2552