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

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

Programs and Puzzles in technical interviews i faced

I have attended interview of nearly 10 companies in my campus placements and sharing their experiences with you,though i did not got selected in any of the companies but i had great experience facing their interviews and it might help you as well in preparation of interviews.Here are some of the puzzles and programs asked to me in interview in some of the good companies. 1) SAP Labs I attended sap lab online test in my college through campus placements.It had 3 sections,the first one is usual aptitude questions which i would say were little tricky to solve.The second section was Programming test in which you were provided snippet of code and you have to complete the code (See Tricky Code Snippets  ).The code are from different data structures like Binary Tree, AVL Tree etc.Then the third section had questions from Database,OS and Networks.After 2-3 hours we got the result and i was shortlisted for the nest round of interviews scheduled next day.Then the next day we had PPT of t...