
- C++ Basics
- C++ Home
- C++ Overview
- C++ Environment Setup
- C++ Basic Syntax
- C++ Comments
- C++ Data Types
- C++ Variable Types
- C++ Variable Scope
- C++ Constants/Literals
- C++ Modifier Types
- C++ Storage Classes
- C++ Operators
- C++ Loop Types
- C++ Decision Making
- C++ Functions
- C++ Numbers
- C++ Arrays
- C++ Strings
- C++ Pointers
- C++ References
- C++ Date & Time
- C++ Basic Input/Output
- C++ Data Structures
- C++ Object Oriented
- C++ Classes & Objects
- C++ Inheritance
- C++ Overloading
- C++ Polymorphism
- C++ Abstraction
- C++ Encapsulation
- C++ Interfaces
Find the missing number in Geometric Progression in C++
Suppose we have an array that represents elements of geometric progression in order. One element is missing. We have to find the missing element. So if arr = [1, 3, 27, 81], output is 9, as 9 is missing.
Using binary search, we can solve this problem. We will go to the middle element, then check whether the ratio between middle and next to the middle is same as common ratio or not. If not, then missing element is present between indices mid and mid + 1. If the middle element is the n/2th element in the GP, then missing element lies in the right half, otherwise to the left half.
Example
#include <iostream> #include <cmath> using namespace std; class Progression { public: int missingUtil(int arr[], int left, int right, int ratio) { if (right <= left) return INT_MAX; int mid = left + (right - left) / 2; if (arr[mid + 1] - arr[mid] != ratio) return (arr[mid] * ratio); if (mid > 0 && arr[mid] / arr[mid - 1] != ratio) return (arr[mid - 1] * ratio); if (arr[mid] == arr[0] * pow(ratio, mid)) return missingUtil(arr, mid + 1, right, ratio); return missingUtil(arr, left, mid - 1, ratio); } int missingElement(int arr[], int n) { int ratio = pow(arr[n-1]/arr[0], 1.0/n); return missingUtil(arr, 0, n - 1, ratio); } }; int main() { Progression pg; int arr[] = {1, 3, 27, 81}; int n = sizeof(arr) / sizeof(arr[0]); cout << "The missing element is: " << pg.missingElement(arr, n); }
Output
The missing element is: 9
- Related Articles
- Find the missing number in Arithmetic Progression in C++
- Missing Number In Arithmetic Progression using C++
- C program to compute geometric progression
- Find all triplets in a sorted array that forms Geometric Progression in C++
- Finding the missing number in an arithmetic progression sequence in JavaScript
- Removing a Number from Array to make It Geometric Progression using C++
- How to create geometric progression series in R?
- C Program for N-th term of Geometric Progression series
- Return numbers spaced evenly on a geometric progression in Numpy
- Find missing number in a sequence in C#
- Find the one missing number in range using C++
- Return evenly spaced numbers on a geometric progression and set the number of samples to generate in Numpy
- Find the only missing number in a sorted array using C++
- Find the repeating and the missing number using two equations in C++
- Return numbers spaced evenly on a geometric progression but with negative inputs in Numpy

Advertisements