Find the missing number in Geometric Progression - GeeksforGeeks

19 Apr, 2021
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. 
C++
Java
Python3
C#
PHP
Javascript
// C++ program to find missing number in// geometric progression#include <bits/stdc++.h>using namespace std; // It returns INT_MAX in case of errorint 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 findMissingRecint 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 codeint main(void){    int arr[] = {2, 4, 8, 32};    int n = sizeof(arr)/sizeof(arr[0]);    cout << findMissing(arr, n);    return 0;}
// JAVA Code for Find the missing number// in Geometric Progressionclass 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 program to find missing# number in geometric progression # It returns INT_MAX in case of errordef 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 findMissingRecdef 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 codearr = [2, 4, 8, 32]n = len(arr)print(findMissing(arr, n)) # This code is contributed by Anant Agarwal.
// C# Code for Find the missing number// in Geometric Progressionusing 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 program to find missing number// in geometric progression // It returns INT_MAX in case of errorfunction 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 findMissingRecfunction 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?>
<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
Note : Drawback withs this solution are : For larger values or for bigger array, it may cause overflow and/or may take more time to computer powers.This article is contributed by Yasin Zafar. 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 write comments if you find anything incorrect, or you want to share more information about the topic discussed above. 
nitin mittal
ukasp
rag2127
Binary Search
Arrays
Arrays
Binary Search
Writing code in comment?
Please use ide.geeksforgeeks.org,
generate link and share the link here.
Program for Bridge and Torch problem
Window Sliding Technique
Binary Tree (Array implementation)
Segment Tree | Set 1 (Sum of given range)
MSD( Most Significant Digit ) Radix Sort
Building Heap from Array
Maximum Subarray Sum using Divide and Conquer algorithm
Product of Array except itself
Find duplicates in O(n) time and O(1) extra space | Set 1