How to Find Element in Rotated Sorted Array?

In this article, we are going to find the element in a rotated sorted array using the programs in different computer languages like Java and C++. We are going to use different approaches to find the element in rotated sorted array. We are also going to find the Time complexity and the Space complexity of the program.

Finding the pivot point, splitting the array into two smaller arrays, and running a binary search are the intended steps.

The key concept for locating a pivot is:

  • In an array that has been rotated and sorted (in increasing order), the pivot element is the only one for which the element after it is smaller.
  • Pivot can be located via binary search, which is based on the previous concept.
  • The element at l will obviously be larger than the element at mid if rotation has occurred in the left half of the search space for indices in the range [l, r] where the middle index is mid.
  • The left half will be sorted without it, but the element at mid will be larger than the element at r.
  • Subdivide the array into two when the pivot has been located.

Every sub-array has been sorted now so that binary search can be used to find an element.

Implementation of the Program

The following steps are used to implement the program. The goal is to locate the key within an N-dimensional, sorted, and rotated array called arr[].

  • Utilize binary search to determine the pivot point. The initial array index will be low, while the last array index will be high.
  • We will determine the midpoint based on the high and low values.
  • Return the value at mid-1 as the pivot if it exceeds the value at mid.
  • Otherwise, return the mid-value as the pivot if the value at the mid+1 is smaller than the mid.
  • Consider the left half instead if the value at the low position is higher than the value at the mid position. If not, think about the right half.
  • Using the discovered pivot as a guide, divide the array into two smaller arrays.
  • One of the two sub-arrays should now be called using binary search.
  • Search in the left array if the element is greater than the 0th element.
  • If not, look in the appropriate array.
  • Return the index if the element can be found in the chosen sub-array.
  • Return -1 if not.

Example:

#include <bits/stdc++.h>
using namespace std;
// Standard Binary Search function
int binarySearch(int arr[], int low, int high, int key)
{
    if (high < low)
        return -1;


    int mid = (low + high) / 2;
    if (key == arr[mid])
        return mid;


    if (key > arr[mid])
        return binarySearch(arr, (mid + 1), high, key);


    return binarySearch(arr, low, (mid - 1), key);
}
int findPivot(int arr[], int low, int high)
{
    // Base cases
    if (high < low)
        return -1;
    if (high == low)
        return low;


    int mid = (low + high) / 2;
    if (mid < high && arr[mid] > arr[mid + 1])
        return mid;


    if (mid > low && arr[mid] < arr[mid - 1])
        return (mid - 1);


    if (arr[low] >= arr[mid])
        return findPivot(arr, low, mid - 1);


    return findPivot(arr, mid + 1, high);
}


int pivotedBinarySearch(int arr[], int n, int key)
{
    int pivot = findPivot(arr, 0, n - 1);


    // If we didn't find a pivot,
    // then array is not rotated at all
    if (pivot == -1)
        return binarySearch(arr, 0, n - 1, key);


    // If we found a pivot, then first compare with pivot
    // and then search in two subarrays around pivot
    if (arr[pivot] == key)
        return pivot;


    if (arr[0] <= key)
        return binarySearch(arr, 0, pivot - 1, key);


    return binarySearch(arr, pivot + 1, n - 1, key);
}


// Driver program to check above functions
int main()
{


    int arr1[] = { 5, 6, 7, 8, 9, 10, 1, 2, 3 };
    int n = sizeof(arr1) / sizeof(arr1[0]);
    int key = 3;


    cout << "Index of the element is : "
<< pivotedBinarySearch(arr1, n, key);


    return 0;
}

Output:

Index of the element is : 8

Time complexity of the following program is - O(log N)

The Space complexity of the following program is - O(1)


Related Topics

How to select all in Gmail

How to select all in Gmail Undoubtedly Gmail is one of the leading email service providers across the world. Teenagers, adults, old ages all rely upon Gmail because of its free...

3 minutes read.

How to unarchive in Gmail?

How to unarchive in Gmail Earlier, we have learned how to archive an email on a computer or a phone device via the Google app. But archiving is not sufficient; you...

3 minutes read.

How to Delete Facebook Account?

How to Delete Facebook Account Facebook is a social networking platform that helps you to quickly communicate and share online with your family and friends. Initially developed for college students, Mark Zuckerberg founded Facebook...

3 minutes read.

Does my computer have Bluetooth?

Does my computer have Bluetooth? With the advancement of IOT (Internet of things) and high-tech electronic gadgets, Bluetooth has become perennial in our digital lives. Most of us are already familiar...

6 minutes read.

How to backup iPhone to Computer?

How to backup iPhone to computer iPhone or iPad is an electronic gadget useful to store all preferences, memories, personal or official data, unlike essential documents, emails, messages, photos, running apps,...

6 minutes read.

How to find Computer name on Windows?

How to find computer name on Windows The computer’s name is undoubtedly useful and needed at times to resolve technical issues or downloading any new software or pairing your system with...

4 minutes read.

How to Cancel Netflix

How to Cancel Netflix Netflix Netflix is a service that lets its members watch a variety of television shows, movies, documentaries, and web series who purchase its subscription. You can enjoy unlimited ad-free...

3 minutes read.

How to Find Divisors of a Number?

A number that equally divides a larger integer is referred to as a divisor or factor. By simply listing all the possible methods to multiply two numbers together to obtain...

3 minutes read.

How to Make Money Online?

How to make money online? Earning money from the traditional way of working is a way harder and requires a lot of manpower and capital. With the increase in the use of the Internet...

9 minutes read.

How to schedule an email in Gmail?

How to schedule an email in Gmail Imagine scenarios where you send an important message to one of your international clients, and you know the time difference is significant. Earlier, you...

7 minutes read.

How to Multiply Fractions?

What is a Fraction? A fraction is a number that divides a whole number into equal parts. A fraction tells us how many parts we have from the whole part. We...

2 minutes read.

How to accelerate the speed of loading images on any website?

No website can be successful without attractive and relevant images or pictures. In the current time, the users want speedy information about their searches on the internet browser. The image...

3 minutes read.

How to Find Element in Rotated Sorted Array?

In this article, we are going to find the element in a rotated sorted array using the programs in different computer languages like Java and C++. We are going to...

3 minutes read.

How to mark all emails as read in Gmail?

How to mark all emails as read in Gmail Gmail is extensively used to exchange official documents, expressions, proposals around the world. In the business industry leaving unread mails is considered...

3 minutes read.

How to defrag a computer?

How to defrag a computer Topics Covered What is Defragmentation?What is the need of defragmentation?Advantages of DefragmentationWhen to defrag your computerDefragmentation for your SSDDefrag your computer running on windows 10Defrag your computer...

8 minutes read.

How to Create a Group Email in Gmail

How to Create a Group Email in Gmail Every now and then, one needs to pass on or share a message to several people at the same time. To make this...

6 minutes read.

How to address a letter (formal and in-formal)

How to address a letter In the generation of (SMS) and emails, it is still necessary to know how to address a letter for the one. Throughout the modern world, formal letters are...

7 minutes read.

How to flip your Computer Screen

How to flip your computer screen The word flip refers to turn over or rotate something quickly. Computers provide the flip functionality, which facilitates the providers to rotate the screen in...

5 minutes read.

How to find your Computer’s specs?

How to find your computer’s specs The computer specs include the details for every single component in your computer, including your CPU, hard drive space, and even the little things like...

7 minutes read.

How to find archived emails in Gmail

How to find archived emails in Gmail Similar/Related Questions Where do Gmail archived emails go? How can Gmail users access archived emails? How to get to archived Gmail? In which folder does the archived Gmail...

4 minutes read.