
- 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
C++ program to search an element in a sorted rotated array
We are given a sorted array which is rotated about a point. We are also given a key to search in the array. The logic adopted to search for an element in this rotated array is −
First, we find the middle element of the array. If the key is present, then we return that the key is present in the array.
If the key is not present in the middle, we can see if the left part of the array(left to mid) is sorted or not. If sorted, we can search for the key in the left part otherwise, we can go for the search on the right part(mid+1, right)
If the key is not found in the middle and the left part is not sorted, then we would sort the right part and we can see if the key is present on the right part, or we will search on the left side of the array
Else we return not found.
Let’s see some input-ouput scenarios below −
Imagine there is an array consisting elements in it. Example, 2 ,5, 7, 9, 11 and after rotating it became 5, 9, 11, 2, 7. Let’s assume key of the array is 2.
Input: arr[] = {5, 9, 11, 2, 7}, Key=2 Output: Element "2" found at 3rd index
Let’s assume another scenario where the key is not in the specified array.
Input: arr[] = {10, 23, 45, 77, 84}, Key=90 Output: Element "90" not found.
Algorithm
The below following steps are the way of approach.
Find middle element of the array.
Divide the array into two parts. ( mid = left + right ) / 2
Check whether key is middle element or not.
Else if , check element in the left part of array and it is sorted
Else if, check element in right part (mid+1, right)
Else if, sort left part and check
Else, return element not found.
Example
For example, assume we have an array “2,3,4,5,6,7,8” and the rotated array is “5, 6, 7, 8, 2, 3, 4”. The key of this array is 2.
The C++ implementation of this operation is as follows −
#include <iostream> #include <vector> using namespace std; bool solve(vector<int> arr, int left, int right, int key) { if (left > right) { return false; } int mid = (left + right)/2; if (arr[mid] == key) { return true; } if (arr[left] <= arr[mid]) { if (key >= arr[left] && key <= arr[mid]) { return solve(arr, left, mid-1, key); } return solve(arr, mid+1, right, key); } if (key >= arr[mid] && key <= arr[right]) return solve(arr, mid+1, right, key); return solve(arr, left, mid-1, key); } int main() { vector<int> arr = {5, 6, 7, 8, 2, 3, 4}; int key = 2; if(solve(arr, 0, arr.size()-1, key)) cout << key << " is present"; else cout << key << " is not present"; return 0; }
Output
2 is present
Conclusion
Another approach for solving this problem would be to find out the pivot point or the index where the array is rotated about and then do a binary search for the side. Our approach takes only 1 binary search to solve the problem. Whenever we see search and sorted array we should search for binary search as one of the methods.
- Related Articles
- Search in Rotated Sorted Array in Python
- Search in Rotated Sorted Array II in C++
- Search in Rotated Sorted Array II in Python
- Write a Golang program to search an element in a sorted array
- Maximum element in a sorted and rotated array in C++
- Program to check whether an array Is sorted and rotated in Python
- Finding smallest element in a sorted array which is rotated in JavaScript
- Swift Program to Search an Element in an Array
- Write a Golang program to search an element in an array
- Check if an array is sorted and rotated in Python
- Check if an array is sorted and rotated in C++
- Find Minimum in Rotated Sorted Array in C++
- Java Program to Recursively Linearly Search an Element in an Array
- C program to search an array element using Pointers.
- Find Minimum in Rotated Sorted Array II in C++
