forked from harshitbansal373/hacktoberfest2020
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathAmazon_problem1.cpp
66 lines (66 loc) · 1.66 KB
/
Amazon_problem1.cpp
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
/*
This problem was asked in Amazon interview.
Given an array of integers and you can jump from index to and index either left or right by the current value of the index. For example - if you have something like [3,2,4,1,5]. So from index[4] i.e val = 1, I can take a left and come to 4 or come to 5 (jumping 1 in either direction). Now, given a start and end point we need to find the smallest number of steps to reach the end point.
*/
// BFS Approach
#include<bits/stdc++.h>
using namespace std;
#define loop1(size) for(int i=0;i<size;i++)
void fun1(vector<int> v,int start,int dst)
{
int size=v.size();
vector<int> temp(size,INT_MAX);
temp[start]=0;
queue<pair<int,int> > q;
q.push({start,0});
int res=INT_MAX;
temp[start]=0;
while(!q.empty())
{
int s=q.size();
for(int i=0;i<s;i++)
{
pair<int,int> p1=q.front();
q.pop();
int pos=p1.first;
int steps=p1.second;
cout<<"pos value : "<<v[pos]<<" steps : "<<steps<<"\n";
if(pos==dst)
{
res=min(res,steps);
continue;
}
if(steps>temp[pos]) continue;
temp[pos]=steps;
int next_pos1=pos+v[pos];
int next_pos2=pos-v[pos];
if(next_pos1<size)
q.push({next_pos1,steps+1});
if(next_pos2>=0)
q.push({next_pos2,steps+1});
}
}
if(res==INT_MAX)
cout<<"NOT POSSIBLE\n";
else
cout<<res<<"\n";
}
int main()
{
vector<int> v;
int size,val,start,dst;
cout<<"ENTER SIZE : ";
cin>>size;
loop1(size)
{
cin>>val;
v.push_back(val);
}
cout<<"ENTER START POS : ";
cin>>start;
cout<<"ENTER DESTINATION : ";
cin>>dst;
fun1(v,start,dst);
int in;
cin>>in;
}