Open In App

Find the missing number in Geometric Progression

Last Updated : 31 Oct, 2022
Improve
Improve
Like Article
Like
Save
Share
Report

Given an array that represents elements of geometric progression in order. One element is missing in the progression, find the missing number. It may be assumed that one term is always missing and the missing term is not first or last of series.

Examples: 

Input : arr[] = {1, 3 , 27, 81}
Output : 9

Input : arr[] = {4, 16, 64, 1024};
Output : 256

A Simple Solution is to linearly traverse the array and find the missing number. Time complexity of this solution is O(n).

An efficient solution to solve this problem in O(Log n) time using Binary Search. The idea is to go to the middle element. Check if the ratio of middle and next to middle is equal to common ratio or not, if not then the missing element lies between mid and mid+1. If the middle element is equal to n/2th term in Geometric Series (Let n be the number of elements in input array), then missing element lies in right half. Else element lies in left half.

Implementation:

C++




// C++ program to find missing number in
// geometric progression
#include <bits/stdc++.h>
using namespace std;
 
// It returns INT_MAX in case of error
int findMissingRec(int arr[], int low,
                   int high, int ratio)
{
    if (low >= high)
        return INT_MAX;
    int mid = low + (high - low)/2;
 
    // If element next to mid is missing
    if (arr[mid+1]/arr[mid] != ratio)
        return (arr[mid] * ratio);
 
    // If element previous to mid is missing
    if ((mid > 0) && (arr[mid]/arr[mid-1]) != ratio)
        return (arr[mid-1] * ratio);
 
    // If missing element is in right half
    if (arr[mid] == arr[0] * (pow(ratio, mid)) )
        return findMissingRec(arr, mid+1, high, ratio);
 
    return findMissingRec(arr, low, mid-1, ratio);
}
 
// Find ration and calls findMissingRec
int findMissing(int arr[], int n)
{
    // Finding ration assuming that the missing term is
    // not first or last term of series.
    int ratio = (float) pow(arr[n-1]/arr[0], 1.0/n);
 
    return findMissingRec(arr, 0, n-1, ratio);
}
 
// Driver code
int main(void)
{
    int arr[] = {2, 4, 8, 32};
    int n = sizeof(arr)/sizeof(arr[0]);
    cout << findMissing(arr, n);
    return 0;
}


Java




// JAVA Code for Find the missing number
// in Geometric Progression
class GFG {
      
    // It returns INT_MAX in case of error
    public static int findMissingRec(int arr[], int low,
                       int high, int ratio)
    {
        if (low >= high)
            return Integer.MAX_VALUE;
        int mid = low + (high - low)/2;
      
        // If element next to mid is missing
        if (arr[mid+1]/arr[mid] != ratio)
            return (arr[mid] * ratio);
      
        // If element previous to mid is missing
        if ((mid > 0) && (arr[mid]/arr[mid-1]) != ratio)
            return (arr[mid-1] * ratio);
      
        // If missing element is in right half
        if (arr[mid] == arr[0] * (Math.pow(ratio, mid)) )
            return findMissingRec(arr, mid+1, high, ratio);
      
        return findMissingRec(arr, low, mid-1, ratio);
    }
      
    // Find ration and calls findMissingRec
    public static int findMissing(int arr[], int n)
    {
        // Finding ration assuming that the missing
        // term is not first or last term of series.
        int ratio =(int) Math.pow(arr[n-1]/arr[0], 1.0/n);
      
        return findMissingRec(arr, 0, n-1, ratio);
    }   
     
    /* Driver program to test above function */
    public static void main(String[] args)
    {
        int arr[] = {2, 4, 8, 32};
        int n = arr.length;
         
        System.out.print(findMissing(arr, n));
    }
  }
// This code is contributed by Arnav Kr. Mandal.


Python3




# Python3 program to find missing
# number in geometric progression
 
# It returns INT_MAX in case of error
def findMissingRec(arr, low, high, ratio):
 
    if (low >= high):
        return 2147483647
    mid = low + (high - low) // 2
 
    # If element next to mid is missing
    if (arr[mid + 1] // arr[mid] != ratio):
        return (arr[mid] * ratio)
 
    # If element previous to mid is missing
    if ((mid > 0) and (arr[mid] / arr[mid-1]) != ratio):
        return (arr[mid - 1] * ratio)
 
    # If missing element is in right half
    if (arr[mid] == arr[0] * (pow(ratio, mid)) ):
        return findMissingRec(arr, mid+1, high, ratio)
 
    return findMissingRec(arr, low, mid-1, ratio)
 
 
# Find ration and calls findMissingRec
def findMissing(arr, n):
  
    # Finding ration assuming that
    # the missing term is not first
    # or last term of series.
    ratio = int(pow(arr[n-1] / arr[0], 1.0 / n))
 
    return findMissingRec(arr, 0, n-1, ratio)
 
# Driver code
arr = [2, 4, 8, 32]
n = len(arr)
print(findMissing(arr, n))
 
# This code is contributed by Anant Agarwal.


C#




// C# Code for Find the missing number
// in Geometric Progression
using System;
 
class GFG {
     
    // It returns INT_MAX in case of error
    public static int findMissingRec(int []arr, int low,
                                    int high, int ratio)
    {
        if (low >= high)
            return int.MaxValue;
             
        int mid = low + (high - low)/2;
     
        // If element next to mid is missing
        if (arr[mid+1]/arr[mid] != ratio)
            return (arr[mid] * ratio);
     
        // If element previous to mid is missing
        if ((mid > 0) && (arr[mid]/arr[mid-1]) != ratio)
            return (arr[mid-1] * ratio);
     
        // If missing element is in right half
        if (arr[mid] == arr[0] * (Math.Pow(ratio, mid)) )
            return findMissingRec(arr, mid+1, high, ratio);
     
        return findMissingRec(arr, low, mid-1, ratio);
    }
     
    // Find ration and calls findMissingRec
    public static int findMissing(int []arr, int n)
    {
         
        // Finding ration assuming that the missing
        // term is not first or last term of series.
        int ratio =(int) Math.Pow(arr[n-1]/arr[0], 1.0/n);
     
        return findMissingRec(arr, 0, n-1, ratio);
    }
     
    /* Driver program to test above function */
    public static void Main()
    {
        int []arr = {2, 4, 8, 32};
        int n = arr.Length;
         
        Console.Write(findMissing(arr, n));
    }
}
 
// This code is contributed by nitin mittal.


PHP




<?php
// PHP program to find missing number
// in geometric progression
 
// It returns INT_MAX in case of error
function findMissingRec(&$arr, $low,
                         $high, $ratio)
{
    if ($low >= $high)
        return PHP_INT_MAX;
    $mid = $low + intval(($high - $low) / 2);
 
    // If element next to mid is missing
    if ($arr[$mid+1]/$arr[$mid] != $ratio)
        return ($arr[$mid] * $ratio);
 
    // If element previous to mid is missing
    if (($mid > 0) && ($arr[$mid] /
                       $arr[$mid - 1]) != $ratio)
        return ($arr[$mid - 1] * $ratio);
 
    // If missing element is in right half
    if ($arr[$mid] == $arr[0] * (pow($ratio, $mid)))
        return findMissingRec($arr, $mid + 1,
                              $high, $ratio);
 
    return findMissingRec($arr, $low,
                          $mid - 1, $ratio);
}
 
// Find ration and calls findMissingRec
function findMissing(&$arr, $n)
{
    // Finding ration assuming that the missing
    // term is not first or last term of series.
    $ratio = (float) pow($arr[$n - 1] /
                         $arr[0], 1.0 / $n);
 
    return findMissingRec($arr, 0, $n - 1, $ratio);
}
 
// Driver code
$arr = array(2, 4, 8, 32);
$n = sizeof($arr);
echo findMissing($arr, $n);
 
// This code is contributed by ita_c
?>


Javascript




<script>
 
// Javascript Code for Find the missing number
// in Geometric Progression
     
    // It returns INT_MAX in case of error
    function findMissingRec(arr,low,high,ratio)
    {
        if (low >= high)
            return Integer.MAX_VALUE;
        let mid = Math.floor(low + (high - low)/2);
        
        // If element next to mid is missing
        if (arr[mid+1]/arr[mid] != ratio)
            return (arr[mid] * ratio);
        
        // If element previous to mid is missing
        if ((mid > 0) && (arr[mid]/arr[mid-1]) != ratio)
            return (arr[mid-1] * ratio);
        
        // If missing element is in right half
        if (arr[mid] == arr[0] * (Math.pow(ratio, mid)) )
            return findMissingRec(arr, mid+1, high, ratio);
        
        return findMissingRec(arr, low, mid-1, ratio);
    }
     
     // Find ration and calls findMissingRec
    function findMissing(arr,n)
    {
        // Finding ration assuming that the missing
        // term is not first or last term of series.
        let ratio =Math.floor( Math.pow(arr[n-1]/arr[0], 1.0/n));
        
        return findMissingRec(arr, 0, n-1, ratio);
    }
     
    /* Driver program to test above function */
    let arr=[2, 4, 8, 32];
    let n = arr.length;
    document.write(findMissing(arr, n));
     
    // This code is contributed by rag2127
     
</script>


Output

16

Time Complexity: O(logn)

Auxiliary Space: O(logn)

Note : Drawback with this solution are : For larger values or for bigger array, it may cause overflow and/or may take more time to computer powers.

 



Similar Reads

Program to find Nth term of given Geometric Progression (GP) series
Given first term (a), common ratio (r), and an integer N of the Geometric Progression series, the task is to find the Nth term of the series. Examples: Input: a = 2 r = 2, N = 4Output: The 4th term of the series is : 16 Input: a = 2 r = 3, N = 5Output: The 5th term of the series is : 162 Approach: To solve the problem follow the below idea: We know
7 min read
Find all triplets in a sorted array that forms Geometric Progression
Given a sorted array of distinct positive integers, print all triplets that forms Geometric Progression with integral common ratio.A geometric progression is a sequence of numbers where each term after the first is found by multiplying the previous one by a fixed, non-zero number called the common ratio. For example, the sequence 2, 6, 18, 54,... i
10 min read
Number of GP (Geometric Progression) subsequences of size 3
Given n elements and a ratio r, find the number of G.P. subsequences with length 3. A subsequence is considered GP with length 3 with ration r. Examples: Input : arr[] = {1, 1, 2, 2, 4} r = 2 Output : 4 Explanation: Any of the two 1s can be chosen as the first element, the second element can be any of the two 2s, and the third element of the subseq
18 min read
Minimum number of operations to convert a given sequence into a Geometric Progression
Given a sequence of N elements, only three operations can be performed on any element at most one time. The operations are: Add one to the element.Subtract one from the element.Leave the element unchanged. Perform any one of the operations on all elements in the array. The task is to find the minimum number of operations(addition and subtraction) t
14 min read
Minimum number of operations to convert a given sequence into a Geometric Progression | Set 2
Given an array arr[] consisting of N integers, the following three operations can be performed on any element one at a time: Add one to the element.Subtract one from the element.Leave the element unchanged.The task is to find the minimum cost required to convert it into a Geometric Progression and also find the common ratio. Note: Each addition and
13 min read
Removing a number from array to make it Geometric Progression
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
11 min read
Find the missing number in unordered Arithmetic Progression
Given an unsorted array arr[] of N integers that are in Arithmetic Progression, the task is to print the missing element from the given series. Examples: Input: arr[] = {12, 3, 6, 15, 18} Output: 9 Explanation: The elements given in order are: 3, 6, 12, 15, 18. Hence missing element is 9. Input: arr[] = {2, 8, 6, 10} Output: 4 Explanation: The elem
13 min read
Find the missing number in Arithmetic Progression
Given an array that represents elements of arithmetic progression in order. One element is missing in the progression, find the missing number. Examples: Input: arr[] = {2, 4, 8, 10, 12, 14} Output: 6 Input: arr[] = {1, 6, 11, 16, 21, 31}; Output: 26Recommended PracticeMissing element of APTry It! A Simple Solution is to linearly traverse the array
31 min read
Geometric Progression
A sequence of numbers is called a Geometric progression if the ratio of any two consecutive terms is always the same. In simple terms, A geometric series is a list of numbers where each number, or term, is found by multiplying the previous term by a common ratio r. The general form of Geometric Progression is: [caption width="800"]GP-series[/captio
6 min read
Check whether nodes of Binary Tree form Arithmetic, Geometric or Harmonic Progression
Given a binary tree, the task is to check whether the nodes in this tree form an arithmetic progression, geometric progression or harmonic progression.Examples: Input: 4 / \ 2 16 / \ / \ 1 8 64 32 Output: Geometric Progression Explanation: The nodes of the binary tree can be used to form a Geometric Progression as follows - {1, 2, 4, 8, 16, 32, 64}
17 min read
Article Tags :
Practice Tags :