Count of jumps to reach the end of Array by jumping from arr[i] to arr[arr[i]]
Given an array arr[] of N integers, the task is to find the number of jumps required to escape the array arr[] for all values of i as the starting indices in the range [0, N) where the only possible jump from arr[i] is to arr[arr[i]] and escaping the array means arr[i]>=N, i.e, the index for the next jump does not exist.
Examples:
Input: arr[] = {2, 3, 4, 1, 10}
Output: 3 -1 2 -1 1
Explanation:
- For i = 0, initially the current index x is 0. After the 1st jump, the current index becomes x = arr[x] = arr[0] = 2. Similarly, after the 2nd jump, the current index becomes x = arr[2] = 4. After the 3rd jump x = arr[4] =10, which is greater than the array size. Therefore the number of steps required to escape the array is 3.
- For i = 1, initially the current index x is 1. After the 1st jump, x = arr[1] = 3. After the 2nd jump, x = arr[3] = 1, which has already been visited and hence forming a closed loop. Therefore it is impossible to escape the array from index 1.
Input: arr[] = {3, 12, 2, 7, 4, 10, 35, 5, 9, 27}
Output: 4 1 -1 3 -1 1 1 2 2 1
Approach: The given problem can be solved with the help of Recursion. Below are the steps to follow:
- Create an array visited[], which stores whether the current index has been visited already. Initially, visited = {0}.
- Create an array cntJumps[], which stores the number of jumps required for all indices in the range [0, N). Initially, cntJumps = {0}.
- Create a recursive function countJumps() which calculates the number of jumps required to escape the array from the current index.
- In the function countJumps(), if the answer of the current index is already calculated, return answer else if the current node is already visited, return -1 else if the array will be escaped after the jump from the current index, return 1.
- Recursively calculate the count of jumps after the current jump i.e, countJumps(i) = 1 + countJumps(arr[i]). Store the answers in the array cntJumps[].
- Print the array cntJumps[], which is the required answer.
Below is the implementation of the above approach:
C++
#include <bits/stdc++.h>
using namespace std;
int cntJumps[100];
int visited[100];
int countJumps( int arr[], int N, int i)
{
if (cntJumps[i] != 0) {
return cntJumps[i];
}
if (visited[i]) {
return -1;
}
visited[i] = true ;
if (arr[i] >= N) {
return cntJumps[i] = 1;
}
int val = countJumps(arr, N, arr[i]);
if (val == -1)
cntJumps[i] = -1;
else
cntJumps[i] = 1 + val;
return cntJumps[i];
}
void printCountJumps( int arr[], int N)
{
memset (visited, 0, sizeof (visited));
memset (cntJumps, 0, sizeof (cntJumps));
for ( int i = 0; i < N; i++) {
if (!visited[i]) {
countJumps(arr, N, i);
}
}
for ( int i = 0; i < N; i++) {
cout << cntJumps[i] << " " ;
}
}
int main()
{
int arr[] = { 3, 12, 2, 7, 4, 10, 35, 5, 9, 27 };
int N = sizeof (arr) / sizeof (arr[0]);
printCountJumps(arr, N);
return 0;
}
|
Java
public class GFG {
static int cntJumps[] = new int [ 100 ];
static int visited[] = new int [ 100 ];
static int countJumps( int arr[], int N, int i)
{
if (cntJumps[i] != 0 ) {
return cntJumps[i];
}
if (visited[i] != 0 ) {
return - 1 ;
}
visited[i] = 1 ;
if (arr[i] >= N) {
cntJumps[i] = 1 ;
return cntJumps[i];
}
int val = countJumps(arr, N, arr[i]);
if (val == - 1 )
cntJumps[i] = - 1 ;
else
cntJumps[i] = 1 + val;
return cntJumps[i];
}
static void printCountJumps( int arr[], int N)
{
for ( int i = 0 ; i < visited.length; i++)
visited[i] = 0 ;
for ( int i = 0 ; i < cntJumps.length; i++)
cntJumps[i] = 0 ;
for ( int i = 0 ; i < N; i++) {
if (visited[i] == 0 ) {
countJumps(arr, N, i);
}
}
for ( int i = 0 ; i < N; i++) {
System.out.print(cntJumps[i] + " " );
}
}
public static void main (String[] args)
{
int arr[] = { 3 , 12 , 2 , 7 , 4 , 10 , 35 , 5 , 9 , 27 };
int N = arr.length;
printCountJumps(arr, N);
}
}
|
Python3
cntJumps = [ 0 for _ in range ( 100 )]
visited = [ 0 for _ in range ( 100 )]
def countJumps(arr, N, i):
global visited
global cntJumps
if (cntJumps[i] ! = 0 ):
return cntJumps[i]
if (visited[i]):
return - 1
visited[i] = True
if (arr[i] > = N):
cntJumps[i] = 1
return cntJumps[i]
val = countJumps(arr, N, arr[i])
if (val = = - 1 ):
cntJumps[i] = - 1
else :
cntJumps[i] = 1 + val
return cntJumps[i]
def printCountJumps(arr, N):
for i in range ( 0 , N):
if ( not visited[i]):
countJumps(arr, N, i)
for i in range ( 0 , N):
print (cntJumps[i], end = " " )
if __name__ = = "__main__" :
arr = [ 3 , 12 , 2 , 7 , 4 , 10 , 35 , 5 , 9 , 27 ]
N = len (arr)
printCountJumps(arr, N)
|
C#
using System;
public class GFG {
static int []cntJumps = new int [100];
static int []visited = new int [100];
static int countJumps( int []arr, int N, int i)
{
if (cntJumps[i] != 0) {
return cntJumps[i];
}
if (visited[i] != 0) {
return -1;
}
visited[i] = 1;
if (arr[i] >= N) {
cntJumps[i] = 1;
return cntJumps[i];
}
int val = countJumps(arr, N, arr[i]);
if (val == -1)
cntJumps[i] = -1;
else
cntJumps[i] = 1 + val;
return cntJumps[i];
}
static void printCountJumps( int []arr, int N)
{
for ( int i = 0; i < visited.Length; i++)
visited[i] = 0;
for ( int i = 0; i < cntJumps.Length; i++)
cntJumps[i] = 0;
for ( int i = 0; i < N; i++) {
if (visited[i] == 0) {
countJumps(arr, N, i);
}
}
for ( int i = 0; i < N; i++) {
Console.Write(cntJumps[i] + " " );
}
}
public static void Main ( string [] args)
{
int []arr = { 3, 12, 2, 7, 4, 10, 35, 5, 9, 27 };
int N = arr.Length;
printCountJumps(arr, N);
}
}
|
Javascript
<script>
let cntJumps = new Array(100);
let visited = new Array(100);
function countJumps(arr, N, i)
{
if (cntJumps[i] != 0) {
return cntJumps[i];
}
if (visited[i]) {
return -1;
}
visited[i] = true ;
if (arr[i] >= N) {
return (cntJumps[i] = 1);
}
let val = countJumps(arr, N, arr[i]);
if (val == -1) cntJumps[i] = -1;
else cntJumps[i] = 1 + val;
return cntJumps[i];
}
function printCountJumps(arr, N) {
visited.fill(0);
cntJumps.fill(0);
for (let i = 0; i < N; i++) {
if (!visited[i]) {
countJumps(arr, N, i);
}
}
for (let i = 0; i < N; i++) {
document.write(cntJumps[i] + " " );
}
}
let arr = [3, 12, 2, 7, 4, 10, 35, 5, 9, 27];
let N = arr.length;
printCountJumps(arr, N);
</script>
|
Output:
4 1 -1 3 -1 1 1 2 2 1
Time Complexity: O(N)
Auxiliary Space: O(N)
Last Updated :
23 Dec, 2021
Like Article
Save Article
Share your thoughts in the comments
Please Login to comment...