Adobe Interview Question
MTSsCountry: India
Interview Type: In-Person
I confess I had to go look up what a rotated array was, but I'm not sure what you mean by the pivot. Is it the "first" element in the sort? If so, it seems like all you'd need to do is iterate through each element in the array, comparing it to the previous element (or the last element in the array when checking the first element) until you found the one element that had a lower sort value than its predecessor.
Wait, scratch that. Compare the first element with an element halfway down the length of the array. That'll tell you which half of the array the pivot is in. Pick an element halfway through that subsection and compare that against the first two elements. Continue dividing down until the pivot is located.
Well in a sorted array any element can be a pivot. But when we rotate an array then the pivot becomes the element which breaks the sorting. This element can be found out by finding the element for which all the elements on the right and left are sorted.
For eg a sorted array would be 1,3,4,5,11,16,18,20. When we rotate it the new array might be 18,20,1,3,4,5,11,16.
Here 20 or 1 can be called as pivot as they break the sorting. The arrays to their left and right are sorted trivially or otherwise.
The following code will work only when all the elements are unique (It works otherwise too but it'll break in certain conditions). The following code also assumes that the sorted array has been rotated i.e. there exists a pivot.
#include <iostream>
using namespace std;
int getPivot( int* arr, int N ) {
if ( NULL == arr ) {
return -1;
}
int low = 0;
int high = N - 1;
int mid;
int last = arr[N - 1];
while ( low <= high ) {
mid = ( low + high ) / 2;
cout << "Mid: " << mid << endl;
if ( arr[mid] < last ) {
high = mid - 1;
} else {
if ( mid < N - 1 && arr[mid] > arr[mid + 1] ) {
return mid;
}
low = mid + 1;
}
}
return mid;
}
void display( int *arr, int N ) {
if ( NULL == arr ) {
return;
}
for ( int i = 0; i < N; i++ ) {
cout << arr[i] << ' ';
}
cout << endl;
}
void rotateByOne( int *arr, int N ) {
if ( arr == NULL ) {
return;
}
int last = arr[N - 1];
for ( int i = N-1; i > 0; i-- ) {
arr[i] = arr[i - 1];
}
arr[0] = last;
}
int main() {
int arr[] = { 1, 2, 3, 4, 5, 6, 7 };
int N = 7;
int pivot;
for ( int i = 0; i <= N; i++ ) {
cout << "=====================================" << endl;
display( arr, N );
pivot = getPivot( arr, N );
cout << "Pivot: " << pivot << endl;
rotateByOne( arr, N );
}
return 0;
}
public static void FindPivotInrotatedSortedArray(int[] a)
{
if (a.Length == 0)
return;
if (a.Length == 1)
{
Console.WriteLine("Pivot is: {0}", a[0]);
return;
}
for (int i = 0; i < a.Length; i++)
{
if (i == 0)
{
if (a[i] < a[i + 1] && a[i] < a[a.Length - 1])
{
Console.WriteLine("Pivot is: {0}", a[i]);
break;
}
}
else if (i == a.Length - 1)
{
if (a[i] < a[0] && a[i] < a[i - 1])
{
Console.WriteLine("Pivot is: {0}", a[i]);
break;
}
}
else
{
if (a[i] < a[i + 1] && a[i] < a[i - 1])
{
Console.WriteLine("Pivot is: {0}", a[i]);
break;
}
}
}
return;
}
PIVOT IS THE ELEMENT WHICH IS LESSER THEN IN LEFT ELEMENT AND RIGHT ELEMENT AS WELL.. so we need to use that...and recurse..where as problem will be divided in three parts when we have one element left then it is the pivot and when we are left with 2 elements then minimum of that...and when of more then three then we need to check the above stated condition and accordingly move to left or right part
- c7c7 October 05, 2012