VOOZH about

URL: https://www.geeksforgeeks.org/dsa/find-index-after-traversing-a-permutation-array-of-1-to-n-by-k-steps/

⇱ Find index after traversing a permutation Array of 1 to N by K steps - GeeksforGeeks


  • Courses
  • Tutorials
  • Interview Prep

Find index after traversing a permutation Array of 1 to N by K steps

Last Updated : 20 May, 2021

Given an integer K and an index array arr[] of length N which contains elements in the range [1, N], the task is to find the index after traversing the array by K steps starting from the index 1.
Traversal of index array: In the traversal of the index array the next index to be visited is the value at the current index. 
Examples: 
 

Input: arr[] = {3, 2, 4, 1}, K = 5 
Output:
Explanation: 
Traversal to the indices starting from index 1: 
1 => 3 => 4 => 1 => 3 => 4 
Finally, after K traversals the index is 4
Input: arr[] = {6, 5, 2, 5, 3, 2}, K = 2 
Output:
 


 


Approach: The key observation in the problem is that after traversing the index array N times it repeats itself. Therefore, we can find the value of the and then finally find the index after traversal.
Below is the implementation of the above approach:
 


Output: 
4

 
Comment