Skip to content
Related Articles

# Minimum number of jumps required to sort numbers placed on a number line

• Last Updated : 29 Jun, 2021

Given two arrays W[] and L[] consisting of N positive integers, where W[i] is initially located at position i on an infinite number line. In each forward jump, W[i] can jump to the position (j + L[i]) from its current position j to any vacant position. The task is to find the minimum number of forward jumps required to sort the array.

Examples:

Input: W[] = {2, 1, 4, 3}, L[] = {4, 1, 2, 4}
Output: 5
Explanation:
Initially, 2 is at position 0, 1 is at position 1, 4 is at position 2, 3 is at position 3 on the number line as: 2 1 4 3
Push number 2 to jump from its current position 0 to position 4, arrangement on the number line: _ 1 4 3 2
Push number 3 to jump from its current position 3 to position 7, arrangement on the number line: _ 1 4 _ 2 _ _ 3
Push number 4 to jump from its current position 2 to position 8, arrangement on the number line: _ 1 _ _ 2 _ _ 3 4

Therefore, the total number of jumps required is 1 + 1 + 3 = 5.

Input: W[] = {3, 1, 2}, L[] = {1, 4, 5}
Output: 3

Approach: The given problem can be solved by using the Greedy Approach by minimizing the number of jumps required for the smallest element in the array which is not correctly positioned in every operation and update the number of jumps. Follow the steps below to solve the problem:

Approach: The given problem can be solved by using the Greedy Approach by minimizing the number of jumps required for the smallest element in the array which is not correctly positioned in every operation and update the number of jumps. Follow the steps below to solve the problem:

• Initialize a variable, say ans as 0 to store the minimum number of jumps required.
• Create a copy of the array W[] and store the elements in sorted order in array arr[].
• Initialize a HashMap pos that stores the current position of the element W[i].
• Traverse the array W[] and update the position of W[i] in pos.
• Traverse the array arr[] in the range [1, N – 1] and perform the following steps:
• Store the position of arr[i] and the position of arr[i – 1] in the array W[] in the variables, say curr and prev respectively.
• If the value of curr is greater than prev, then continue to the next iteration.
• Otherwise, iterate until curr ≤ prev or curr is already occupied and increment the value of curr by the jump and increment the value of ans by 1.
• Update the position of arr[i] in the HashMap pos to curr.
• After completing the above steps, print the value of ans as the result.

Below is the implementation of the above approach:

## C++

 `// C++ program for the above approach``#include ``using` `namespace` `std;` `// Function to find the minimum number``// of jumps required to sort the array``void` `minJumps(``int` `w[], ``int` `l[], ``int` `n)``{``    ``// Base Case``    ``if` `(n == 1) {``        ``cout << 0;``        ``return``;``    ``}` `    ``// Store the required result``    ``int` `ans = 0;` `    ``// Stores the current position of``    ``// elements and their respective``    ``// maximum jump``    ``unordered_map<``int``, ``int``> pos, jump;` `    ``// Used to check if a position is``    ``// already taken by another element``    ``unordered_map<``int``, ``bool``> filled;` `    ``// Stores the sorted array a[]``    ``int` `a[n];` `    ``// Traverse the array w[] & update``    ``// positions jumps array a[]``    ``for` `(``int` `i = 0; i < n; i++) {` `        ``pos[w[i]] = i;``        ``filled[i] = ``true``;``        ``jump[w[i]] = l[i];``        ``a[i] = w[i];``    ``}` `    ``// Sort the array a[]``    ``sort(a, a + n);` `    ``// Traverse the array a[] over``    ``// the range [1, N-1]``    ``for` `(``int` `curr = 1;``         ``curr < n; curr++) {` `        ``// Store the index of current``        ``// element and its just smaller``        ``// element in array w[]``        ``int` `currElementPos``            ``= pos[a[curr]];``        ``int` `prevElementPos``            ``= pos[a[curr - 1]];` `        ``if` `(currElementPos``            ``> prevElementPos)``            ``continue``;` `        ``// Iterate until current element``        ``// position is at most its just``        ``// smaller element position``        ``while` `(currElementPos``                   ``<= prevElementPos``               ``|| filled[currElementPos]) {` `            ``currElementPos += jump[a[curr]];``            ``ans++;``        ``}` `        ``// Update the position of the``        ``// current element``        ``pos[a[curr]] = currElementPos;``        ``filled[currElementPos] = ``true``;``    ``}` `    ``// Print the result``    ``cout << ans;``}` `// Driver Code``int` `main()``{``    ``int` `W[] = { 2, 1, 4, 3 };``    ``int` `L[] = { 4, 1, 2, 4 };``    ``int` `N = ``sizeof``(W) / ``sizeof``(W[0]);``    ``minJumps(W, L, N);` `    ``return` `0;``}`

## Java

 `// Java program for the above approach``import` `java.util.*;``class` `GFG {` `    ``// Function to find the minimum number``    ``// of jumps required to sort the array``    ``static` `void` `minJumps(``int``[] w, ``int``[] l, ``int` `n)``    ``{` `        ``// Base Case``        ``if` `(n == ``1``) {``            ``System.out.print(``0``);``            ``return``;``        ``}` `        ``// Store the required result``        ``int` `ans = ``0``;` `        ``// Stores the current position of``        ``// elements and their respective``        ``// maximum jump``        ``HashMap pos``            ``= ``new` `HashMap();``        ``HashMap jump``            ``= ``new` `HashMap();` `        ``// Used to check if a position is``        ``// already taken by another element``        ``HashMap filled``            ``= ``new` `HashMap();` `        ``// Stores the sorted array a[]``        ``int``[] a = ``new` `int``[n];` `        ``// Traverse the array w[] & update``        ``// positions jumps array a[]``        ``for` `(``int` `i = ``0``; i < n; i++) {``            ``if` `(pos.containsKey(w[i]))``                ``pos.put(w[i], i);``            ``else``                ``pos.put(w[i], i);``            ``if` `(filled.containsKey(w[i]))``                ``filled.put(i, ``true``);``            ``else``                ``filled.put(i, ``true``);``            ``if` `(jump.containsKey(w[i]))``                ``jump.put(w[i], l[i]);``            ``else``                ``jump.put(w[i], l[i]);` `            ``a[i] = w[i];``        ``}` `        ``// Sort the array a[]``        ``Arrays.sort(a);` `        ``// Traverse the array a[] over``        ``// the range [1, N-1]``        ``for` `(``int` `curr = ``1``; curr < n; curr++) {` `            ``// Store the index of current``            ``// element and its just smaller``            ``// element in array w[]``            ``int` `currElementPos = pos.get(a[curr]);``            ``int` `prevElementPos = pos.get(a[curr - ``1``]);` `            ``if` `(currElementPos > prevElementPos)``                ``continue``;` `            ``// Iterate until current element``            ``// position is at most its just``            ``// smaller element position``            ``while` `(currElementPos <= prevElementPos``                   ``|| filled.containsKey(currElementPos)``                          ``&& filled.containsKey(``                              ``currElementPos)) {``                ``currElementPos += jump.get(a[curr]);``                ``ans++;``            ``}` `            ``// Update the position of the``            ``// current element``            ``if` `(pos.containsKey(a[curr]))``                ``pos.put(a[curr], currElementPos);``            ``else``                ``pos.put(a[curr], currElementPos);``            ``if` `(filled.containsKey(currElementPos))``                ``filled.put(currElementPos, ``true``);``            ``else``                ``filled.put(currElementPos, ``true``);``        ``}` `        ``// Print the result``        ``System.out.print(ans);``    ``}` `    ``// Driver Code``    ``public` `static` `void` `main(String[] args)``    ``{``        ``int``[] W = { ``2``, ``1``, ``4``, ``3` `};``        ``int``[] L = { ``4``, ``1``, ``2``, ``4` `};``        ``int` `N = W.length;` `        ``minJumps(W, L, N);``    ``}``}` `// This code is contributed by ukasp.`

## Python3

 `# Python3 program for the above approach` `# Function to find the minimum number``# of jumps required to sort the array``def` `minJumps(w, l, n):``    ` `    ``# Base Case``    ``if` `(n ``=``=` `1``):``        ``print``(``0``)``        ``return` `    ``# Store the required result``    ``ans ``=` `0` `    ``# Stores the current position of``    ``# elements and their respective``    ``# maximum jump``    ``pos ``=` `{}``    ``jump ``=` `{}` `    ``# Used to check if a position is``    ``# already taken by another element``    ``filled ``=` `{}` `    ``# Stores the sorted array a[]``    ``a ``=` `[``0` `for` `i ``in` `range``(n)]` `    ``# Traverse the array w[] & update``    ``# positions jumps array a[]``    ``for` `i ``in` `range``(n):``        ``pos[w[i]] ``=` `i``        ``filled[i] ``=` `True``        ``jump[w[i]] ``=` `l[i]``        ``a[i] ``=` `w[i]` `    ``# Sort the array a[]``    ``a.sort()` `    ``# Traverse the array a[] over``    ``# the range [1, N-1]``    ``for` `curr ``in` `range``(``1``, n, ``1``):``        ` `        ``# Store the index of current``        ``# element and its just smaller``        ``# element in array w[]``        ``currElementPos ``=` `pos[a[curr]]``        ``prevElementPos ``=` `pos[a[curr ``-` `1``]]` `        ``if` `(currElementPos > prevElementPos):``            ``continue` `        ``# Iterate until current element``        ``# position is at most its just``        ``# smaller element position``        ``while` `(currElementPos <``=` `prevElementPos ``or``              ``(currElementPos ``in` `filled ``and``              ``filled[currElementPos])):``            ``currElementPos ``+``=` `jump[a[curr]]``            ``ans ``+``=` `1` `        ``# Update the position of the``        ``# current element``        ``pos[a[curr]] ``=` `currElementPos``        ``filled[currElementPos] ``=` `True` `    ``# Print the result``    ``print``(ans)` `# Driver Code``if` `__name__ ``=``=` `'__main__'``:``    ` `    ``W ``=` `[ ``2``, ``1``, ``4``, ``3` `]``    ``L ``=` `[ ``4``, ``1``, ``2``, ``4` `]``    ``N ``=` `len``(W)``    ` `    ``minJumps(W, L, N)` `# This code is contributed by SURENDRA_GANGWAR`

## C#

 `// C# program for the above approach``using` `System;``using` `System.Collections.Generic;` `class` `GFG{` `// Function to find the minimum number``// of jumps required to sort the array``static` `void` `minJumps(``int` `[]w, ``int` `[]l, ``int` `n)``{``    ` `    ``// Base Case``    ``if` `(n == 1)``    ``{``        ``Console.Write(0);``        ``return``;``    ``}` `    ``// Store the required result``    ``int` `ans = 0;` `    ``// Stores the current position of``    ``// elements and their respective``    ``// maximum jump``    ``Dictionary<``int``,``               ``int``> pos = ``new` `Dictionary<``int``,``                                         ``int``>();``    ``Dictionary<``int``,``               ``int``> jump = ``new` `Dictionary<``int``,``                                          ``int``>();` `    ``// Used to check if a position is``    ``// already taken by another element``     ``Dictionary<``int``,``                ``bool``> filled = ``new` `Dictionary<``int``,``                                              ``bool``>();` `    ``// Stores the sorted array a[]``    ``int` `[]a = ``new` `int``[n];` `    ``// Traverse the array w[] & update``    ``// positions jumps array a[]``    ``for``(``int` `i = 0; i < n; i++)``    ``{``        ``if` `(pos.ContainsKey(w[i]))``           ``pos[w[i]] = i;``        ``else``           ``pos.Add(w[i], i);``        ``if` `(filled.ContainsKey(w[i]))``           ``filled[i] = ``true``;``        ``else``           ``filled.Add(i, ``true``);``        ``if` `(jump.ContainsKey(w[i]))``           ``jump[w[i]] = l[i];``        ``else``           ``jump.Add(w[i], l[i]);``           ` `        ``a[i] = w[i];``    ``}` `    ``// Sort the array a[]``    ``Array.Sort(a);` `    ``// Traverse the array a[] over``    ``// the range [1, N-1]``    ``for``(``int` `curr = 1; curr < n; curr++)``    ``{``        ` `        ``// Store the index of current``        ``// element and its just smaller``        ``// element in array w[]``        ``int` `currElementPos = pos[a[curr]];``        ``int` `prevElementPos = pos[a[curr - 1]];` `        ``if` `(currElementPos > prevElementPos)``            ``continue``;` `        ``// Iterate until current element``        ``// position is at most its just``        ``// smaller element position``        ``while` `(currElementPos <= prevElementPos ||``               ``filled.ContainsKey(currElementPos) &&``               ``filled[currElementPos])``        ``{``            ``currElementPos += jump[a[curr]];``            ``ans++;``        ``}``        ` `        ``// Update the position of the``        ``// current element``        ``if` `(pos.ContainsKey(a[curr]))``            ``pos[a[curr]] = currElementPos;``        ``else``            ``pos.Add(a[curr], currElementPos);``        ``if` `(filled.ContainsKey(currElementPos))``            ``filled[currElementPos] = ``true``;``        ``else``            ``filled.Add(currElementPos, ``true``);``    ``}` `    ``// Print the result``    ``Console.Write(ans);``}` `// Driver Code``public` `static` `void` `Main()``{``    ``int` `[]W = { 2, 1, 4, 3 };``    ``int` `[]L = { 4, 1, 2, 4 };``    ``int` `N = W.Length;``    ` `    ``minJumps(W, L, N);``}``}` `// This code is contributed by ipg2016107`

## Javascript

 ``
Output:
`5`

Time Complexity: O(N*log N)
Auxiliary Space: O(N)

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.  To complete your preparation from learning a language to DS Algo and many more,  please refer Complete Interview Preparation Course.

In case you wish to attend live classes with experts, please refer DSA Live Classes for Working Professionals and Competitive Programming Live for Students.

My Personal Notes arrow_drop_up