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 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.

How to delete Snapchat Account?

What is Snapchat? Snapchat is a social networking app, which is developed by Snap Inc. It is created by Even Spiegel, Bobby Murphy, and Reggie Brown. Snapchat was launched in September 2011. It is...

3 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 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 change Computer Name?

How to change computer name Today computers are all around us and have become an indispensable part of our lives. Every computer generally has a name attached to it. There might...

6 minutes read.

How to add contacts to Gmail?

How to add contacts to Gmail? Managing your inbox is a tiresome task, but one way to make it effortless and easier is to gain an add on the benefit of...

5 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 snooze an email in Gmail

How to snooze an email in Gmail With the popularity of Gmail, today every deal is likely to be discussed in emails. And with so many things rushing in the Inbox,...

3 minutes read.

How to delete Instagram account?

What is Instagram? Instagram is used for sharing the images and videos on social networking websites through various platforms such as iOS, Android, Windows Phones, and Desktop. In 2010, Instagram was...

3 minutes read.

How to build a Computer?

How to build a computer Computing technology has evolved a lot in the last decade and has come a long way since then. For many years building computers was as tough...

17 minutes read.

How to create a Gmail account?

How to create a Gmail account? Gmail is a free email service provider widely used worldwide to communicate and exchange personal and professional emails. With almost a billion active users, Gmail...

7 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 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 cancel amazon prime?

How to cancel amazon prime? Amazon Prime is a subscription that offers variety of services with it. The advantage of amazon prime is that you can expect hassle-free shipping within two days, access to...

3 minutes read.

How to write a cheque?

How to write a cheque? In today's time, digital payments are consistently gaining more market share, but it's also important to know how to write a cheque. Many companies, employers, workers, and landlords still...

2 minutes read.

How to write a cover letter

How to write a cover letter The letter that you include with your job application is known as a cover letter. One is suggested to always include a cover letter with your job application...

5 minutes read.

How to take a Screenshot on computer?

How to take a Screenshot on computer The ScreenShot feature is widely used to procure the content of the screen as an image. This feature was introduced in the late ’90s,...

8 minutes read.

How to delete a page in Microsoft Word?

What is Microsoft Word? Microsoft word is a word processing program, which is developed by Microsoft. It was initially launched on 25 October 1983 and has since been modified many times. Generally, Microsoft Word...

2 minutes read.

How to delete Gmail Account?

What is Gmail? Gmail is a free mobile and web-based email service introduced by Google on April 1, 2004. Gmail is used for personal to business purposes. It allows users to...

2 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.