Skip to main content

Program to search the element in Sorted Rotated Pivoted array

Sorted Rotated Pivoted array means the sorted array is rotated through the pivot element.The element in a sorted array can be find using binary search in O(logn) time.But if the sorted array is rotated through some pivot element then how to search for an element in that array.Suppose take sorted array 1 2 3 4 5 it might become 3 4 5 1 2 after rotating at Pivot element 5.Pivot element can be found by the idea that the only element whose next element is smaller then pivot element in an array.So here is the program to find the element in a sorted rotated pivoted array.

Our first task is to find a pivot element in an array.The above idea of  pivot element is used to find the pivot element in an array.

int findpivot(int a[],int low,int high)
{
int mid=(low+high)/2;

if(a[mid]>a[mid+1])        // if middle element is greater then its next element then it is pivot element as it is the          
                                         only element whose next element is smaller then itself.
return mid;

if(a[low]>a[mid])            // if the low element is greater then middle element then search for pivot from low to
                                         mid-1 by calling the same function recursively .

return findpivot(a,low,mid-1);

else                                 //if not then it is in right part call recursively from mid+1 to high

return findpivot(a,mid+1,high);
}

Now we can find the element in sorted rotated pivoted array using the pivot element and doing Binary search.

int pivotedBinarySearch(int a[],int size,int no)
{
int pivot=findpivot(a,0,size-1);

if(a[pivot]==no)              //If number is at pivot element index then return the index pivot

return pivot;

if(a[0]<=no)                   // Now search in subarrays the element to be find using binary search

return Binarysearch(a,0,pivot-1,no);

else'

return Binarysearch(a,pivot+1,size-1,no);
}

Program :


int pivotedBinarySearch(int a[],int size,int no)
{
int pivot=findpivot(a,0,size-1);
if(a[pivot]==no)            
return pivot;
if(a[0]<=no)                  
return Binarysearch(a,0,pivot-1,no);
else
return Binarysearch(a,pivot+1,size-1,no);
}

int findpivot(int a[],int low,int high)
{
int mid=(low+high)/2;
if(a[mid]>a[mid+1])      
return mid;
if(a[low]>a[mid])          
return findpivot(a,low,mid-1);
else                                 /
return findpivot(a,mid+1,high);
}
int Binarysearch(int a[],int low,int high,int no)
{
if(high>=low)
int mid=(low+high)/2;
if(no==a[mid])
return mid;
if(no>a[mid])
return Binarysearch(a,mid+1,high,no);
else
return Binarysearch(a,low,mid-1,no);
}

//This program may not work with Duplicate elements.

Time Complexity - O(logn)

Comments

Popular posts from this blog

Tricky Questions or Puzzles in C ( Updated for 2026)

Updated for 2026 This article was originally written when C/C++ puzzles were commonly asked in interviews. While such language-specific puzzles are less frequent today, the problem-solving and logical reasoning skills tested here remain highly relevant for modern Software Engineering, Data Engineering, SQL, and system design interviews . Why These Puzzles Still Matter in 2026 Although most Software &   Data Engineering interviews today focus on Programming, SQL, data pipelines, cloud platforms, and system design , interviewers still care deeply about how you think . These puzzles test: Logical reasoning Edge-case handling Understanding of execution flow Ability to reason under pressure The language may change , but the thinking patterns do not . How These Skills Apply to Data Engineering Interviews The same skills tested by C/C++ puzzles appear in modern interviews as: SQL edge cases and NULL handling Data pipeline failure scenarios Incremental vs ...

Decorators in Python

Decorators provide additional functionality to the functions without directly changing their definition of them. Basically, it takes the functions as an argument, adds functionality to them, and returns it.  Before diving deep into the concept of Decorators, let's first try to understand  What are functions in Python and what is an inner function? In Python everything is Objects, be it Class, Variables, and Functions . So functions are python first-class objects that can be used or passed as an argument. You can store the functions in variables, you can pass a function to another function as parameters, and you can also return the function from the function. Below is one simple example where we are treating functions as objects . def make_me_lowercase ( str ): return str .lower() print (make_me_lowercase( "HELLO World" )) copy_of_you = make_me_lowercase print (copy_of_you( "HELLO World" )) The output of the above calls for both the functio...

Program to uncompress a string ie a2b3c4 to aabbbcccc

Below is the program to uncompress a string #include<stdio.h> #include<conio.h> #include<stdlib.h> int main() { char str[100]="a2b3c4d8u7"; for(int i=0;str[i]!='\0';i++) { if(i%2!=0) { for(int j=0;j<atoi(&str[i]);j++) { printf("%c",str[i-1]); } } } getch(); } Want to become a Data Engineer? Check out below blog posts  1.  5 Key Skills Every Data Engineer needs in 2023 2.  How to prepare for Data Engineering Interviews 3.  Top 25 Data Engineer Questions