forked from krshubhamiisc/Algorithms
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathgame.cpp
More file actions
34 lines (31 loc) · 901 Bytes
/
game.cpp
File metadata and controls
34 lines (31 loc) · 901 Bytes
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
#include <bits/stdc++.h>
using namespace std;
int throwTheBall(vector<int> receiver, long seconds) {
vector<int> pattern;
unordered_set<int> seen;
int curr = 0;
seen.insert(curr);
pattern.push_back(curr+1);
while(true) {
curr = receiver[curr] - 1;
if (seen.find(curr) != seen.end()) {
pattern.push_back(curr+1);
break;
}
pattern.push_back(curr+1);
seen.insert(curr);
}
int start = 0;
while(pattern[start] != pattern.back()) {
start++;
}
int non_repeating_length = start;
int repeating_length = pattern.size() - start - 1;
//now you can make the repeating part of the sequence and non repeating part of the sequence and answer the value accordingly
return 0;
}
int main() {
vector<int> receiver{ 2,4,1,2,3 };
cout<<throwTheBall(receiver, 6)<<endl;
return 0;
}