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”.
Examples:
Input : arr[] = [2, 4, 8, 24, 16, 32]
Output : 3
Number to remove is arr[3], 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.
C++
#include <bits/stdc++.h>
using namespace std;
#define EPS 1e-6
bool fEqual( double a, double b)
{
return ( abs (a - b) < EPS);
}
bool isGP( double arr[], int N, int ignore)
{
double last = -1;
double ratio = -1;
for ( int i = 0; i < N; i++)
{
if (i != ignore)
{
if (last != -1)
{
if (ratio == -1)
ratio = ( double )arr[i] / last;
else if (!fEqual(ratio, ( double )arr[i] / last))
return false ;
}
last = arr[i];
}
}
return true ;
}
int makeGPbyRemovingOneElement( double arr[], int N)
{
if (isGP(arr, N, 0))
return 0;
if (isGP(arr, N, 1))
return 1;
if (isGP(arr, N, N-1))
return (N-1);
double ratio = ( double )arr[1]/arr[0];
for ( int i = 2; i < N; i++)
{
if (!fEqual(ratio, ( double )arr[i]/arr[i-1]))
{
return (isGP(arr+i-2, N-i+2, 2))? i : -1;
}
}
return -1;
}
int main()
{
double arr[] = {2, 4, 8, 30, 16};
int N = sizeof (arr) / sizeof (arr[0]);
int index = makeGPbyRemovingOneElement(arr, N);
if (index == -1)
cout << "Not possible\n" ;
else
cout << "Remove " << arr[index]
<< " to get geometric progression\n" ;
return 0;
}
|
Java
import java.util.*;
class GFG {
final static double EPS = ( double ) 1e- 6 ;
static boolean fEqual( double a, double b) {
return (Math.abs(a - b) < EPS);
}
static boolean isGP( double [] arr, int N, int ignore) {
double last = - 1 ;
double ratio = - 1 ;
for ( int i = 0 ; i < N; i++) {
if (i != ignore) {
if (last != - 1 ) {
if (ratio == - 1 )
ratio = ( double ) arr[i] / last;
else if (!fEqual(ratio, ( double ) arr[i] / last))
return false ;
}
last = arr[i];
}
}
return true ;
}
static int makeGPbyRemovingOneElement( double [] arr, int N) {
if (isGP(arr, N, 0 ))
return 0 ;
if (isGP(arr, N, 1 ))
return 1 ;
if (isGP(arr, N, N - 1 ))
return (N - 1 );
double ratio = ( double ) arr[ 1 ] / arr[ 0 ];
for ( int i = 2 ; i < N; i++) {
if (!fEqual(ratio, ( double ) arr[i] / arr[i - 1 ])) {
double [] temp = new double [N - i + 2 ];
int k = 0 ;
for ( int j = i - 2 ; j < N; j++) {
temp[k++] = arr[j];
}
return (isGP(temp, N - i + 2 , 2 )) ? i : - 1 ;
}
}
return - 1 ;
}
public static void main(String[] args) {
double arr[] = { 2 , 4 , 8 , 30 , 16 };
int N = arr.length;
int index = makeGPbyRemovingOneElement(arr, N);
if (index == - 1 )
System.out.println( "Not possible" );
else
System.out.println( "Remove " + arr[index] +
" to get geometric progression" );
}
}
|
Python3
EPS = 1e - 7
def fEqual(a, b):
return abs (a - b) < EPS
def isGP(arr, N, ignore):
last = - 1
ratio = - 1
for i in range (N):
if i ! = ignore:
if last ! = - 1 :
if ratio = = - 1 :
ratio = arr[i] / last
else if not fEqual(ratio, arr[i] / last):
return False
last = arr[i]
return True
def makeGPbyRemovingOneElement(arr, N):
if isGP(arr, N, 0 ):
return 0
if isGP(arr, N, 1 ):
return 1
if isGP(arr, N, N - 1 ):
return N - 1
ratio = arr[ 1 ] / arr[ 0 ]
for i in range ( 2 , N):
if not fEqual(ratio, arr[i] / arr[i - 1 ]):
return i if isGP(arr[i - 2 :], N - i + 2 , 2 ) else - 1
return - 1
if __name__ = = "__main__" :
arr = [ 2 , 4 , 8 , 30 , 16 ]
N = len (arr)
index = makeGPbyRemovingOneElement(arr, N)
if index = = - 1 :
print ( "Not Possible" )
else :
print ( "Remove %d to get geometric progression" % arr[index])
|
C#
using System;
class GFG{
static double EPS = ( double )1e-6;
static bool fEqual( double a, double b)
{
return (Math.Abs(a - b) < EPS);
}
static bool isGP( double [] arr, int N,
int ignore)
{
double last = -1;
double ratio = -1;
for ( int i = 0; i < N; i++)
{
if (i != ignore)
{
if (last != -1)
{
if (ratio == -1)
ratio = ( double )arr[i] / last;
else if (!fEqual(ratio,
( double ) arr[i] / last))
return false ;
}
last = arr[i];
}
}
return true ;
}
static int makeGPbyRemovingOneElement( double [] arr,
int N)
{
if (isGP(arr, N, 0))
return 0;
if (isGP(arr, N, 1))
return 1;
if (isGP(arr, N, N - 1))
return (N - 1);
double ratio = ( double ) arr[1] / arr[0];
for ( int i = 2; i < N; i++)
{
if (!fEqual(ratio, ( double )arr[i] /
arr[i - 1]))
{
double [] temp = new double [N - i + 2];
int k = 0;
for ( int j = i - 2; j < N; j++)
{
temp[k++] = arr[j];
}
return (isGP(temp, N - i + 2, 2)) ? i : -1;
}
}
return -1;
}
public static void Main( string [] args)
{
double []arr = { 2, 4, 8, 30, 16 };
int N = arr.Length;
int index = makeGPbyRemovingOneElement(arr, N);
if (index == -1)
Console.Write( "Not possible" );
else
Console.Write( "Remove " + arr[index] +
" to get geometric progression" );
}
}
|
Javascript
let EPS = 1e-5;
function fEqual(a, b)
{
return Math.abs(Math.fround(a) - Math.fround(b)) < Math.fround(EPS);
}
function isGP(arr, N, ignore)
{
var last = -1, ratio = -1;
for ( var i = 0; i < N; i++)
{
if (i != ignore)
{
if (last != -1)
{
if (ratio == -1)
ratio = (arr[i] / last);
else if (!(fEqual(ratio, arr[i] / last)))
return false ;
}
last = arr[i];
}
}
return true ;
}
function makeGPbyRemovingOneElement(arr, N)
{
if (isGP(arr, N, 0))
return 0;
if (isGP(arr, N, 1))
return 1;
if (isGP(arr, N, N - 1))
return N - 1;
var ratio = parseFloat(arr[1] / arr[0]);
for ( var i = 2; i < N; i++)
{
if (!fEqual(ratio, arr[i] / arr[i - 1]))
{
if (isGP(arr.slice(i - 2), N - i + 2, 2))
return i
return -1;
}
}
return -1
}
let arr = [2, 4, 8, 30, 16];
let N = arr.length;
let index = makeGPbyRemovingOneElement(arr, N)
if (index == -1)
console.log( "Not Possible" )
else
console.log( "Remove" , arr[index], "to get geometric progression" );
|
Output:
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 write.geeksforgeeks.org or mail your article to review-team@geeksforgeeks.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.