Cisco Systems Interview Question
Software Engineer / DevelopersTeam: Nexus
Country: United States
Interview Type: In-Person
Wouldn't rotating multiple times be the same as rotating once by the sum of all rotations?
BTW, on personal front, you seem to be good contributor to this site ( I'm quantifying you on your 300+ comments). What are you upto? Sophomore or junior or senior :) ?
#include<iostream>
#include<conio.h>
using namespace std;
int main() {
int a[10]={8,9,10,1,2,3,4,5,6,7};
int i=0; //start index
int j=4; //middle index
int k=9; //array.length-1
while(i!=j) {
if(a[i]<a[j] && a[j]<a[k]) {
k=j;
j=(i+k)/2;
}
else if(a[i]<a[j] && a[j]>a[k]){
i=j;
j=(j+k)/2;
}
else if(a[i]<a[j]) {
k=j;
j=(i+k)/2;
}
else if(a[i]>a[j] && a[j]<a[k]) {
k=j;
j=(i+k)/2;
}
}
cout << "Minimum two elements: " << endl;
if(a[i]<a[k]) {
cout << a[i] << endl;
cout << a[i+1] << endl;
}
else
cout << a[k] << endl;
cout << a[k+1] << endl;
getch();
return 0;
}
If a complete sorted array is rotated (i assume it is not rotating w.r.t. some point inside array as it is not written), then it is still a sorted array but in opposite order. So how many times it is rotated is not a matter. Only we have to check if first element in greater than the second, it is minimum number and second is second minimum and similarily for maximum.
If a complete sorted array is rotated (i assume it is not rotating w.r.t. some point inside array as it is not written), then it is still a sorted array but in opposite order. So how many times it is rotated is not a matter. Only we have to check if first element in greater than the second, it is minimum number and second is second minimum and similarily for maximum.
If a complete sorted array is rotated (i assume it is not rotating w.r.t. some point inside array as it is not written), then it is still a sorted array but in opposite order. So how many times it is rotated is not a matter. Only we have to check if first element in greater than the second, it is minimum number and second is second minimum and similarily for maximum.
struct Pair
{
int min;
int max;
}
struct Pair GetMin(int arr[], int low, int high)
{
struct Pair MinMax, Left, Right;
If (low==high)
{
MinMax.min=arr[low];
MinMax.max=arr[high];
return MinMax;
}
If(low=high+1)
{
MinMax.min=((arr[low]<arr[high])?arr[low]:arr[high]);
MinMax.max=((arr[low]>arr[high])?arr[low]:arr[high]);
return MinMax;
}
int mid=(left+right)/2;
Left=GetMin(arr,low,mid);
Right=GetMin(arr,mid+1,high);
MinMax.min=((Left.min)<(Right.min))?Left.Min:Right.Min;
MinMax.max=((Left.max)>(Right.max))?Left.max:Right.max;
return MinMax;
}
void f(int a[]; n)
- lliyanc April 10, 2012{
int k;
for(k = 0; k < n; k++)
{
if (a[k] > a[(k+1) % n]);
break;
}
printf(“The minimum is: %d”, a[(k+1) % n];
printf(“The second minis: %d”, a[(k+2) % n];
printf(“The second max is: %d”, a[(k+n-1) % n];
}