
- 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
Search in a Sorted Array of Unknown Size in C++
Suppose we have an array and that is sorted in ascending order, we have to define a function to search target in nums. If the target is present, then return its index, otherwise, return -1.
The array size is not known. We can only access the array using an ArrayReader interface. There is a get function like ArrayReader.get(k) this will return the element of the array at index k.
So, if the input is like array = [-1,0,3,5,9,12], target = 9, then the output will be 4 as 9 exists in nums and its index is 4
To solve this, we will follow these steps −
high := 1, low := 0
while get(high) of reader < target, do −
low := high
high = high * 2
while low <= high, do −
mid := low + (high - low) / 2
x := get(mid) of reader
if x is same as target, then −
return mid
if x > target, then −
high := mid - 1
Otherwise
low := mid + 1
return -1
Example
Let us see the following implementation to get better understanding −
#include <bits/stdc++.h> using namespace std; class ArrayReader{ private: vector<int> v; public: ArrayReader(vector<int> &v){ this->v = v; } int get(int i){ return v[i]; } }; class Solution { public: int search(ArrayReader& reader, int target) { int high = 1; int low = 0; while (reader.get(high) < target) { low = high; high <<= 1; } while (low <= high) { int mid = low + (high - low) / 2; int x = reader.get(mid); if (x == target) return mid; if (x > target) { high = mid - 1; } else low = mid + 1; } return -1; } }; main(){ Solution ob; vector<int> v = {-1,0,3,5,9,12}; ArrayReader reader(v); cout<<(ob.search(reader, 9)); }
Input
{-1,0,3,5,9,12}, 9
Output
4
- Related Articles
- Search in Rotated Sorted Array in Python
- Search elements in a sorted object array in Java
- Search in Rotated Sorted Array II in C++
- Search in Rotated Sorted Array II in Python
- How to iterate efficiently through an array of integers of unknown size in C#
- C++ program to search an element in a sorted rotated array
- Convert Sorted Array to Binary Search Tree in Python
- Write a Golang program to search an element in a sorted array
- Find the only repeating element in a sorted array of size n using C++
- Binary search in sorted vector of pairs in C++
- Square of a sorted array in C++
- Find a sorted subsequence of size 3 in linear time in Python\n
- Floor in a Sorted Array in C++
- Search array of objects in a MongoDB collection?
- Absolute distinct count in a sorted array?
