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

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: 4
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: 5

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 K \% N and then finally find the index after K \% N traversal.



Below is the implementation of the above approach:

C++

filter_none

edit
close

play_arrow

link
brightness_4
code

// C++ implementation to find the 
// index after traversing the index 
// array K times
  
#include<bits/stdc++.h>
using namespace std;
  
// Function to find the index after
// traversing the index array K times
int findIndexAfterKTrav(vector<int> arr,
                        int n, int k){
    k = k % n;
    int indi = 1;
      
    // Loop to traverse the index
    // array K times
    while (k){
        indi = arr[indi-1];
        k--;
    }
      
    return arr[indi-1];
}
  
// Driver Code
int main() {
    int n = 4, k = 5;
    vector<int> arr{3, 2, 4, 1};
  
    // Function Call
    cout << findIndexAfterKTrav(arr, n, k);
    return 0;
}

chevron_right


Java

filter_none

edit
close

play_arrow

link
brightness_4
code

// Java implementation to find the 
// index after traversing the index 
// array K times 
class GFG{
      
// Function to find the index after 
// traversing the index array K times
public static int findIndexAfterKTrav(int[] arr, 
                                      int n, int k)
    k = k % n; 
    int indi = 1
          
    // Loop to traverse the index 
    // array K times 
    while (k > 0)
    
        indi = arr[indi - 1]; 
        k--; 
    
    return arr[indi - 1]; 
  
// Driver code
public static void main(String[] args) 
{
    int n = 4, k = 5
    int[] arr = { 3, 2, 4, 1 }; 
      
    // Function Call 
    System.out.print(findIndexAfterKTrav(arr, n, k));
}
}
  
// This code is contributed by divyeshrabadiya07

chevron_right


Python3

filter_none

edit
close

play_arrow

link
brightness_4
code

# Python3 implementation to find the
# index after traversing the index
# array K times
  
# Function to find the index after
# traversing the index array K times
def findIndexAfterKTrav(arr, n, k):
      
    k = k % n;
    indi = 1;
  
    # Loop to traverse the index
    # array K times
    while (k > 0):
        indi = arr[indi - 1];
        k -= 1;
  
    return arr[indi - 1];
  
# Driver code
if __name__ == '__main__':
      
    n = 4;
    k = 5;
    arr = [ 3, 2, 4, 1 ];
  
    # Function Call
    print(findIndexAfterKTrav(arr, n, k));
  
# This code is contributed by Princi Singh

chevron_right


C#

filter_none

edit
close

play_arrow

link
brightness_4
code

// C# implementation to find the 
// index after traversing the index 
// array K times 
using System;
  
class GFG{
       
// Function to find the index after 
// traversing the index array K times
public static int findIndexAfterKTrav(int[] arr, 
                                      int n, int k)
    k = k % n; 
    int indi = 1; 
           
    // Loop to traverse the index 
    // array K times 
    while (k > 0)
    
        indi = arr[indi - 1]; 
        k--; 
    
    return arr[indi - 1]; 
   
// Driver code
public static void Main(String[] args) 
{
    int n = 4, k = 5; 
    int[] arr = { 3, 2, 4, 1 }; 
       
    // Function Call 
    Console.Write(findIndexAfterKTrav(arr, n, k));
}
}
  
// This code is contributed by Princi Singh

chevron_right


Output:

4

Attention reader! Don’t stop learning now. Get hold of all the important DSA concepts with the DSA Self Paced Course at a student-friendly price and become industry ready.




My Personal Notes arrow_drop_up

Recommended Posts:


Check out this Author's contributed articles.

If you like GeeksforGeeks and would like to contribute, you can also write an article using contribute.geeksforgeeks.org or mail your article to contribute@geeksforgeeks.org. See your article appearing on the GeeksforGeeks main page and help other Geeks.

Please Improve this article if you find anything incorrect by clicking on the "Improve Article" button below.