Find Next Index
You are given an array next_idx of size N, where next_idx[i] contains the index of the next element to visit. You start at index 0. Following the next_idx array, find the index of the element that is visited exactly K steps after the start. The first step is to index 0. The second step is to next_idx[0], the third step is to next_idx[next_idx[0]], and so on. You are guaranteed that K is small enough so you never go out of bounds or enter an infinite loop before reaching the K-th step.
Input
The first line contains two integers N and K, separated by a space. The second line contains N integers representing the next_idx array.
Output
Print a single integer: the index of the element visited at the K-th step.
Constraints
1 <= N <= 1000, 0 <= K <= N, 0 <= next_idx[i] < N for all i.
Example 1
5 3 1 2 3 4 0
3
Start at 0. Step 1: next_idx[0]=1. Step 2: next_idx[1]=2. Step 3: next_idx[2]=3. Output: 3.
Example 2
4 2 3 0 1 2
2
Start at 0. Step 1: next_idx[0]=3. Step 2: next_idx[3]=2. Output: 2.
Hints
# Read the input from stdin and print the answer.
import sys
def main():
data = sys.stdin.read().split()
# your code here
main()
5 3 1 2 3 4 0
4 2 3 0 1 2