Amazon Interview Question
SDE-2sCountry: United States
Interview Type: In-Person
you probably can find tons of answers everywhere. They key is to use binary search to find the index of that smallest element and also make sure to ask the elements are unique.
using System;
public class Program
{
public static void Main()
{
int[] testArray = {7, 9, 11, 12, 15};
Console.WriteLine(BinarySearch(testArray , 0 , testArray.Length-1));
}
public static int BinarySearch (int[] rotatedArray , int min , int max)
{
if (min > max)
{
return 0;
}
else if (max == min)
{
return min;
}
else
{
int mid = (min+max)/2;
if (mid-1 > 0)
{
if (rotatedArray[mid-1] > rotatedArray[mid])
{
return mid;
}
}
if (mid+1 < max)
{
if (rotatedArray[mid] > rotatedArray[mid+1] )
{
return mid+1;
}
}
if (rotatedArray[mid] < rotatedArray[max])
return BinarySearch (rotatedArray , min , mid-1);
else
return BinarySearch (rotatedArray , mid+1 , max);
}
}
}
int main() {
- tryhard March 22, 2017int n = 0;
for(int i = 0; i < array.size()-1; i++) {
if(array[i+1] - array[i] > 0)
n++;
else
break;
}
cout << ++n%5 << endl;
return 0;
}