Find position after K jumps from start of given Array where each jump is from i to arr[i]
Given an array arr[] of size N containing elements from 1 to N. Find the position after exactly K jumps from index 1 where the jump from ith index sends to arr[i] index.
Examples:
Input: arr[ ] = { 3, 2, 4,1 }, K = 5;
Output: 4
Explanation: Start from index 1 and go to position 3 -> 4 ->1 -> 3 -> 4
Input: arr[ ] = { 3 , 2 , 1 }, K = 3
Output: 3
Explanation: Start from index 1 and go to position 3 -> 1 -> 3
Approach: This problem can be solved using map data structure. The motive is to find the cycle formed when jumps from 1 element to another are made. When a loop or a cycle is observed mark it visited and count number of jumps taken to repeat this position and store it in X using map and if it is visited again the next X jumps are the same. So take modulo from K = K % X.
- Initialize a map.
- Initialize a variable len = 0 and idx = 1.
- Take a while loop and run until value of K is greater than 0 or loop is detected.
- After loop is over subtract the loop len from K.
- Now, Take the remaining jumps.
Below is the implementation of the above approach.
C++
#include <bits/stdc++.h>
using namespace std;
int LastElement( int arr[], int N, int K)
{
for ( int i = 0; i < N; i++) {
--arr[i];
}
unordered_map< int , int > visit;
int elem = 0, len = 0;
while (K and !visit[elem]) {
visit[elem] = ++len;
K--;
elem = arr[elem];
}
K = K % (len + 1 - visit[elem]);
for ( int i = 1; i <= K; i++) {
elem = arr[elem];
}
return elem + 1;
}
int main()
{
int arr[] = { 3, 2, 4, 1 };
int N = 4;
int K = 5;
int ans = LastElement(arr, N, K);
cout << ans;
return 0;
}
|
Java
import java.io.*;
import java.util.*;
class GFG {
static int LastElement( int arr[], int N, int K)
{
for ( int i = 0 ; i < N; i++) {
--arr[i];
}
HashMap<Integer, Integer> visit = new HashMap<Integer, Integer>();
int elem = 0 , len = 0 ;
while (K >= 0 && visit.get(elem) == null )
{
visit.put(elem, ++len);
K--;
elem = arr[elem];
}
K = K % (len + 1 - visit.get(elem));
for ( int i = 1 ; i <= K; i++) {
elem = arr[elem];
}
return elem + 1 ;
}
public static void main (String[] args)
{
int arr[ ] = { 3 , 2 , 4 , 1 };
int N = 4 ;
int K = 5 ;
int ans = LastElement(arr, N, K);
System.out.print(ans);
}
}
|
Python3
def LastElement(arr, N, K) :
for i in range (N):
arr[i] - = 1
visit = [ 0 ] * (N);
elem = 0
_len = 0 ;
while (K and not visit[elem]):
visit[elem] = + + _len;
K - = 1
elem = arr[elem];
K = K % (_len + 1 - visit[elem]);
for i in range ( 1 , K + 1 ):
elem = arr[elem];
return elem + 1 ;
arr = [ 3 , 2 , 4 , 1 ];
N = 4 ;
K = 5 ;
ans = LastElement(arr, N, K);
print (ans);
|
C#
using System;
using System.Collections.Generic;
public class GFG {
static int LastElement( int []arr, int N, int K)
{
for ( int i = 0; i < N; i++) {
--arr[i];
}
Dictionary< int , int > visit = new Dictionary< int , int >();
int elem = 0, len = 0;
while (K >= 0 && !visit.ContainsKey(elem))
{
visit.Add(elem, ++len);
K--;
elem = arr[elem];
}
K = K % (len + 1 - visit[elem]);
for ( int i = 1; i <= K; i++) {
elem = arr[elem];
}
return elem + 1;
}
public static void Main(String[] args)
{
int []arr = { 3, 2, 4, 1 };
int N = 4;
int K = 5;
int ans = LastElement(arr, N, K);
Console.Write(ans);
}
}
|
Javascript
<script>
function LastElement(arr, N, K)
{
for (let i = 0; i < N; i++) {
--arr[i];
}
let visit = new Array(N);
let elem = 0, len = 0;
while (K && !visit[elem]) {
visit[elem] = ++len;
K--;
elem = arr[elem];
}
K = K % (len + 1 - visit[elem]);
for (let i = 1; i <= K; i++) {
elem = arr[elem];
}
return elem + 1;
}
let arr = [3, 2, 4, 1];
let N = 4;
let K = 5;
let ans = LastElement(arr, N, K);
document.write(ans);
</script>
|
Time Complexity: O(N)
Auxiliary Space: O(N)
Last Updated :
07 Feb, 2022
Like Article
Save Article
Share your thoughts in the comments
Please Login to comment...