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 4Input: 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
and then finally find the index after
traversal.
Below is the implementation of the above approach:
C++
// 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; } |
Java
// 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 |
Python3
# 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 |
C#
// 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 |
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.
Recommended Posts:
- Minimum index i such that all the elements from index i to given index are equal
- Minimum steps to convert an Array into permutation of numbers from 1 to N
- Minimum number of given operations required to convert a permutation into an identity permutation
- Minimum number of adjacent swaps required to convert a permutation to another permutation by given condition
- Find array sum using Bitwise OR after splitting given array in two halves after K circular shifts
- Find the index of the left pointer after possible moves in the array
- Lexicographically smallest permutation with no digits at Original Index
- Rearrange array such that even index elements are smaller and odd index elements are greater
- Queries for number of distinct elements from a given index till last index in an array
- Sort Array such that smallest is at 0th index and next smallest it at last index and so on
- Farthest index that can be reached from the Kth index of given array by given operations
- A permutation where each element indicates either number of elements before or after it
- Largest permutation after at most k swaps
- Find element at given index after a number of rotations
- Find element at given index after given range reversals
- Find the index which is the last to be reduced to zero after performing a given operation
- Find an index such that difference between product of elements before and after it is minimum
- Most frequent element in Array after replacing given index by K for Q queries
- Queries to print count of distinct array elements after replacing element at index P by a given element
- Find permutation array from the cumulative sum array
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.

