Amazon Interview Question
Software Engineer / Developersint MinDis(int *a,int b1,int b2){
int i=0,ti=0,tdis=0,dis=SIZE;
for(i=0;i<SIZE;i++){
if(a[i]==b1||a[i]==b2){
if(a[i]!=a[ti]&&ti!=0){
tdis=i-ti;
if(tdis<dis)
dis=tdis;
}
ti=i;
}
}
return dis;
}
your code will fail in the following case:
2 5 4 3 2 1
2 is the 0th element, 5 is the 1st element. dis will still remain as SIZE due to your if condition checking if ti != 0
int idxb1 = -1, idxb2 = -1;
int dis = SIZE;
for(int i = 0; i < SIZE; i++) {
if(a[i] == b1) {
idxb1 = i;
if(idxb2 != -1)
if((idxb1 - idxb2) < dis)
dis = idxb1 - idxb2;
}
else if(a[i] == b2) {
idxb2 = i;
if(idxb1 != -1)
if((idxb2 - idxb1) < dis)
dis = idxb2 - idxb1;
}
}
My code is obviously incomplete, but you would have hopefully got the idea.
include <stdio.h>
#include <algorithm>
#include <limits.h>
int minDist(int a[], int n, int b, int c) {
int ret = INT_MAX, cur = -1;
while(n--) {
if(a[n] == b || a[n] == c) {
if(cur != -1 && a[cur] != a[n]) {
ret = std::min(ret, cur - n);
}
cur = n;
}
}
return ret;
}
int main() {
int a[] = {1, 2, 10, 2, 3, 5, 2, 1, 5};
printf("%d\n", minDist(a, sizeof(a)/sizeof(int), 2, 5));
return 0;
}
#include<iostream.h>
#include<string.h>
#include<conio.h>
void find_dist(int arr[],int a,int b,int size)
{
int dist=9999;
int strt,fin;
strt=-1;
fin=-1;
for(int i=0;i<size;i++)
{
if(arr[i]==a)
{
strt=i;
}
else if(arr[i]==b)
{
fin=i;
}
if(strt!=-1 && fin!=-1)
{
if(strt>fin)
{
if((strt-fin)<dist)
dist=strt-fin;
}
else
{
if((fin-strt)<dist)
dist=fin-strt;
}
}
}
cout<<strt<<" "<<fin<<endl;
if(strt==-1 && fin==-1) cout<<a<<" and "<<b<<" not found\n";
else if(strt==-1) cout<<a<<" not found\n";
else if(fin==-1)
{
if(a==b)
cout<<"the min dist is "<<0<<endl;
else
cout<<b<<" not found\n";
}
else
cout<<"the min dist is "<<dist<<endl;
}
int main()
{
int arr[]={1,2,10,2,3,5,2,1,5,10};
int size = sizeof(arr) / sizeof(arr[0]);
cout<<size<<endl;
find_dist(arr,1,10,size);
getch();
return 0;
}
#include<iostream.h>
#include<string.h>
#include<conio.h>
void find_dist(int arr[],int a,int b,int size)
{
int dist=9999;
int strt,fin;
strt=-1;
fin=-1;
for(int i=0;i<size;i++)
{
if(arr[i]==a)
{
strt=i;
}
else if(arr[i]==b)
{
fin=i;
}
if(strt!=-1 && fin!=-1)
{
if(strt>fin)
{
if((strt-fin)<dist)
dist=strt-fin;
}
else
{
if((fin-strt)<dist)
dist=fin-strt;
}
}
}
cout<<strt<<" "<<fin<<endl;
if(strt==-1 && fin==-1) cout<<a<<" and "<<b<<" not found\n";
else if(strt==-1) cout<<a<<" not found\n";
else if(fin==-1)
{
if(a==b)
cout<<"the min dist is "<<0<<endl;
else
cout<<b<<" not found\n";
}
else
cout<<"the min dist is "<<dist<<endl;
}
int main()
{
int arr[]={1,2,10,2,3,5,2,1,5,10};
int size = sizeof(arr) / sizeof(arr[0]);
cout<<size<<endl;
find_dist(arr,1,10,size);
getch();
return 0;
}
void main()
{
int arr[]={2,5,4,3,2,1};
int p1,p2,minval,n,v1,v2,temp;
n=6;
p1=0;
p2=0;
v1=2;
v2=5;
while(arr[p1]!=v1)
p1++;
while(arr[p2]!=v2)
p2++;
minval=abs(p1-p2);
while(p1<n || p2<n)
{
if(arr[p1]==v1 && arr[p2]==v2)
if(abs(p1-p2)<minval)
minval=abs(p1-p2);
if(p2>p1)
p1++;
else
p2++;
}
printf("MIN = %d\n",minval-1);
}
This is a repeat of the question 8226041.
- BM March 28, 2011