
- 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 position of an element in a sorted array of infinite numbers in C++
In this problem we are given an array consisting of infinite sorted numbers. Our task is to Find position of an element in a sorted array of infinite numbers.
Let’s take an example to understand the problem,
Input
arr[] = {2, 4, 6, 8, 9, 12, 14,17, ….}, ele = 9
Output
4
Explanation
Solution Approach
For searching elements from a sorted array efficiently, we will be using the binary searching method. Here, single the end point is not known, we will modify the algorithm a bit.
We will fix the start pointer to first position, then take the end pointer to second position. After this, we will check for the value at the end pointer and increment it by doubling it if the value is less than key and update the start pointer with the last position of the end pointer.
When the last position value is greater than the element to be found, we will search in this subarray using binary search.
Program to illustrate the working of our solution,
Example
#include<iostream> using namespace std; int binarySearch(int arr[], int start, int end, int ele) { if (end >= start) { int mid = start + (end - start)/2; if (arr[mid] == ele) return mid; if (arr[mid] > ele) return binarySearch(arr, start, mid-1, ele); return binarySearch(arr, mid+1, end, ele); } return -1; } int findPos(int arr[], int value) { int start = 0, end = 1; while (arr[end] < value) { start = end; end = 2*end; } return binarySearch(arr, start, end, value); } int main(){ int arr[] = {1, 2, 4, 6, 8, 9, 12, 14, 17, 21, 45}; int index = findPos(arr, 9); if (index == -1) cout<<"Element not found!"; else cout<<"Element found! index = "<<index; return 0; }
Output
Element found! index = 5
- Related Articles
- Find First and Last Position of Element in Sorted Array in Python
- Find missing element in a sorted array of consecutive numbers in Python
- Find missing element in a sorted array of consecutive numbers in C++
- Find the index of first 1 in an infinite sorted array of 0s and 1s in C++
- Find index of an extra element present in one sorted array in C++
- Adding an element at a given position of the array in Javascript
- Removing an element from a given position of the array in Javascript
- Find the only repeating element in a sorted array of size n using C++
- Finding missing element in an array of numbers in JavaScript
- C++ program to search an element in a sorted rotated array
- How to move an element of an array to a specific position (swap)?
- Write a Golang program to search an element in a sorted array
- Single Element in a Sorted Array in C++
- Count of only repeated element in a sorted array of consecutive elements in C++
- Find the element that appears once in sorted array - JavaScript
