Count minimum factor jumps required to reach the end of an Array

Given an array of positive integers arr[], the task is to count the minimum factor jumps required to reach the end of an array. From any particular index i, the jump can be made only for K indices where K is a factor of arr[i].
Examples:Â
Input: arr[] = {2, 8, 16, 55, 99, 100}Â
Output: 2Â
Explanation:Â
The optimal jumps are:Â
a) Start from 2.Â
b) Since factors of 2 are [1, 2]. So only 1 or 2 index jumps are available. Therefore, jump 1 index to reach 8.Â
c) Since factors of 8 are [1, 2, 4, 8]. So only 1, 2, 4 or 8 index jumps are available. Therefore, they jumped 4 indices to reach 100.Â
d) We have reached the end, so no more jumps are required.Â
So, 2 jumps were required.Input: arr[] = {2, 4, 6}Â
Output: 1Â
Approach: This problem can be solved using Recursion.
- Firstly, we need to precompute the factors of every number from 1 to 1000000, so that we can get different choices of jumps in O(1) time.
- Then, recursively calculate the minimum jumps required to reach the end of the array and print it.
C++
// C++ code to count minimum factor jumps// to reach the end of array#include <bits/stdc++.h>using namespace std;Â
// vector to store factors of each integervector<int> factors[100005];Â
// Precomputing all factors of integers// from 1 to 100000void precompute(){Â Â Â Â for (int i = 1; i <= 100000; i++) {Â Â Â Â Â Â Â Â for (int j = i; j <= 100000; j += i) {Â Â Â Â Â Â Â Â Â Â Â Â factors[j].push_back(i);Â Â Â Â Â Â Â Â }Â Â Â Â }}Â
// Recursive function to count the minimum jumpsint solve(int arr[], int k, int n){    // If we reach the end of array,    // no more jumps are required    if (k == n - 1) {        return 0;    }Â
    // If the jump results in out of index,    // return INT_MAX    if (k >= n) {        return INT_MAX;    }Â
    // Else compute the answer    // using the recurrence relation    int ans = INT_MAX;Â
    // Iterating over all choices of jumps    for (auto j : factors[arr[k]]) {Â
        // Considering current factor as a jump        int res = solve(arr, k + j, n);Â
        // Jump leads to the destination        if (res != INT_MAX) {            ans = min(ans, res + 1);        }    }Â
    // Return ans    return ans;}Â
// Driver codeint main(){    // pre-calculating the factors    precompute();Â
    int arr[] = { 2, 8, 16, 55, 99, 100 };    int n = sizeof(arr) / sizeof(arr[0]);Â
    cout << solve(arr, 0, n);} |
Java
// Java code to count minimum // factor jumps to reach the // end of arrayimport java.util.*;public class GFG{Â
// vector to store factors // of each integerstatic Vector<Integer> []factors = Â Â Â Â Â Â Â Â Â Â Â Â Â Â new Vector[100005];Â
// Precomputing all factors // of integers from 1 to 100000static void precompute(){Â Â for (int i = 0; i < factors.length; i++)Â Â Â Â factors[i] = new Vector<Integer>();Â Â for (int i = 1; i <= 100000; i++) Â Â {Â Â Â Â for (int j = i; j <= 100000; j += i) Â Â Â Â {Â Â Â Â Â Â factors[j].add(i);Â Â Â Â }Â Â }}Â
// Function to count the // minimum jumpsstatic int solve(int arr[],                  int k, int n){  // If we reach the end of   // array, no more jumps   // are required  if (k == n - 1)   {    return 0;  }Â
  // If the jump results in   // out of index, return   // Integer.MAX_VALUE  if (k >= n)   {    return Integer.MAX_VALUE;  }Â
  // Else compute the answer  // using the recurrence relation  int ans = Integer.MAX_VALUE;Â
  // Iterating over all choices   // of jumps  for (int j : factors[arr[k]])   {    // Considering current factor     // as a jump    int res = solve(arr, k + j, n);Â
    // Jump leads to the destination    if (res != Integer.MAX_VALUE)     {      ans = Math.min(ans, res + 1);    }  }Â
  // Return ans  return ans;}Â
// Driver codepublic static void main(String[] args){  // pre-calculating   // the factors  precompute();Â
  int arr[] = {2, 8, 16,                55, 99, 100};  int n = arr.length;  System.out.print(solve(arr, 0, n));}}Â
// This code is contributed by Samim Hossain Mondal. |
C#
// C# code to count minimum // factor jumps to reach the // end of arrayusing System;using System.Collections.Generic;class GFG{Â
// vector to store factors // of each integerstatic List<int> []factors = Â Â Â Â Â Â Â Â Â Â Â Â new List<int>[100005];Â
// Precomputing all factors // of integers from 1 to 100000static void precompute(){Â Â for (int i = 0; Â Â Â Â Â Â Â Â Â Â Â i < factors.Length; i++)Â Â Â Â factors[i] = new List<int>();Â Â for (int i = 1; i <= 100000; i++) Â Â {Â Â Â Â for (int j = i; Â Â Â Â Â Â Â Â Â Â Â Â Â j <= 100000; j += i) Â Â Â Â {Â Â Â Â Â Â factors[j].Add(i);Â Â Â Â }Â Â }}Â
// Function to count the // minimum jumpsstatic int solve(int []arr,                  int k, int n){  // If we reach the end of   // array, no more jumps   // are required  if (k == n - 1)   {    return 0;  }Â
  // If the jump results in   // out of index, return   // int.MaxValue  if (k >= n)   {    return int.MaxValue;  }Â
  // Else compute the answer  // using the recurrence relation  int ans = int.MaxValue;Â
  // Iterating over all choices   // of jumps  foreach (int j in factors[arr[k]])   {    // Considering current     // factor as a jump    int res = solve(arr, k + j, n);Â
    // Jump leads to the     // destination    if (res != int.MaxValue)     {      ans = Math.Min(ans, res + 1);    }  }Â
  // Return ans  return ans;}Â
// Driver codepublic static void Main(String[] args){  // pre-calculating   // the factors  precompute();Â
  int []arr = {2, 8, 16,                55, 99, 100};  int n = arr.Length;  Console.Write(solve(arr, 0, n));}}Â
// This code is contributed by Samim Hossain Mondal. |
Python
# Python3 code to count minimum factor jumps# to reach the end of arrayÂ
# vector to store factors of each integerfactors = [[] for i in range(100005)]Â
# Precomputing all factors of integers# from 1 to 100000def precompute():Â
    for i in range(1, 100001):        for j in range(i, 100001, i):Â
            factors[j].append(i)Â
# Function to count the minimum jumpsdef solve(arr, k, n):Â
    # If we reach the end of array,    # no more jumps are required    if (k == n - 1):        return 0Â
    # If the jump results in out of index,    # return INT_MAX    if (k >= n):        return 1000000000Â
    # Else compute the answer    # using the recurrence relation    ans = 1000000000Â
    # Iterating over all choices of jumps    for j in factors[arr[k]]:Â
        # Considering current factor as a jump        res = solve(arr, k + j, n)Â
        # Jump leads to the destination        if (res != 1000000000):            ans = min(ans, res + 1)Â
    # Return ans    return ansÂ
Â
# Driver codeif __name__ == '__main__':Â
    # pre-calculating the factors    precompute()Â
    arr = [2, 8, 16, 55, 99, 100]    n = len(arr)Â
    print(solve(arr, 0, n))Â
# This code is contributed by Samim Hossain Mondal. |
Javascript
<script>// Javascript code to count minimum factor jumps// to reach the end of arrayÂ
// vector to store factors of each integerlet factors = new Array();Â
for (let i = 0; i < 100005; i++) {Â Â Â Â factors.push(new Array());}Â
// Precomputing all factors of integers// from 1 to 100000function precompute() {Â Â Â Â for (let i = 1; i <= 100000; i++) {Â Â Â Â Â Â Â Â for (let j = i; j <= 100000; j += i) {Â Â Â Â Â Â Â Â Â Â Â Â factors[j].push(i);Â Â Â Â Â Â Â Â }Â Â Â Â }}Â
// Function to count the minimum jumpsfunction solve(arr, k, n) {Â
    // If we reach the end of array,    // no more jumps are required    if (k == n - 1) {        return 0;    }Â
    // If the jump results in out of index,    // return INT_MAX    if (k >= n) {        return Number.MAX_SAFE_INTEGER;    }Â
    // Else compute the answer    // using the recurrence relation    let ans = Number.MAX_SAFE_INTEGER;Â
    // Iterating over all choices of jumps    for (let j of factors[arr[k]]) {Â
        // Considering current factor as a jump        let res = solve(arr, k + j, n);Â
        // Jump leads to the destination        if (res != Number.MAX_SAFE_INTEGER) {            ans = Math.min(ans, res + 1);        }    }Â
    // Return ans    return ans;}Â
// Driver codeÂ
// pre-calculating the factorsprecompute();Â
let arr = [2, 8, 16, 55, 99, 100];let n = arr.length;Â
document.write(solve(arr, 0, n));Â
// This code is contributed by Samim Hossain Mondal.</script> |
2
Time Complexity: O(100005*2N)
Auxiliary Space: O(100005)
Another Approach: Dynamic Programming using Memoization.Â
- Firstly, we need to precompute the factors of every number from 1 to 1000000, so that we can get different choices of jumps in O(1) time.
- Then, let dp[i] be the minimum jump required to reach i, we need to find dp[n-1].
- So, the recurrence relation becomes:
Âwhere j is one of the factors of arr[i] & solve() is the recursive functionÂ
- Find the minimum jumps using this recurrence relation and print it.
Below is the recursive implementation of the above approach:Â
C++
// C++ code to count minimum factor jumps// to reach the end of arrayÂ
#include <bits/stdc++.h>using namespace std;Â
// vector to store factors of each integervector<int> factors[100005];Â
// dp arrayint dp[100005];Â
// Precomputing all factors of integers// from 1 to 100000void precompute(){Â Â Â Â for (int i = 1; i <= 100000; i++) {Â Â Â Â Â Â Â Â for (int j = i; j <= 100000; j += i) {Â Â Â Â Â Â Â Â Â Â Â Â factors[j].push_back(i);Â Â Â Â Â Â Â Â }Â Â Â Â }}Â
// Function to count the minimum jumpsint solve(int arr[], int k, int n){Â
    // If we reach the end of array,    // no more jumps are required    if (k == n - 1) {        return 0;    }Â
    // If the jump results in out of index,    // return INT_MAX    if (k >= n) {        return INT_MAX;    }Â
    // If the answer has been already computed,    // return it directly    if (dp[k]) {        return dp[k];    }Â
    // Else compute the answer    // using the recurrence relation    int ans = INT_MAX;Â
    // Iterating over all choices of jumps    for (auto j : factors[arr[k]]) {Â
        // Considering current factor as a jump        int res = solve(arr, k + j, n);Â
        // Jump leads to the destination        if (res != INT_MAX) {            ans = min(ans, res + 1);        }    }Â
    // Return ans and memorize it    return dp[k] = ans;}Â
// Driver codeint main(){Â
    // pre-calculating the factors    precompute();Â
    int arr[] = { 2, 8, 16, 55, 99, 100 };    int n = sizeof(arr) / sizeof(arr[0]);Â
    cout << solve(arr, 0, n);} |
Java
// Java code to count minimum // factor jumps to reach the // end of arrayimport java.util.*;class GFG{Â
// vector to store factors // of each integerstatic Vector<Integer> []factors = Â Â Â Â Â Â Â Â Â Â Â Â Â Â new Vector[100005];Â
// dp arraystatic int []dp = new int[100005];Â
// Precomputing all factors // of integers from 1 to 100000static void precompute(){Â Â for (int i = 0; i < factors.length; i++)Â Â Â Â factors[i] = new Vector<Integer>();Â Â for (int i = 1; i <= 100000; i++) Â Â {Â Â Â Â for (int j = i; j <= 100000; j += i) Â Â Â Â {Â Â Â Â Â Â factors[j].add(i);Â Â Â Â }Â Â }}Â
// Function to count the // minimum jumpsstatic int solve(int arr[],                  int k, int n){  // If we reach the end of   // array, no more jumps   // are required  if (k == n - 1)   {    return 0;  }Â
  // If the jump results in   // out of index, return   // Integer.MAX_VALUE  if (k >= n)   {    return Integer.MAX_VALUE;  }Â
  // If the answer has been   // already computed, return   // it directly  if (dp[k] != 0)   {    return dp[k];  }Â
  // Else compute the answer  // using the recurrence relation  int ans = Integer.MAX_VALUE;Â
  // Iterating over all choices   // of jumps  for (int j : factors[arr[k]])   {    // Considering current factor     // as a jump    int res = solve(arr, k + j, n);Â
    // Jump leads to the destination    if (res != Integer.MAX_VALUE)     {      ans = Math.min(ans, res + 1);    }  }Â
  // Return ans and memorize it  return dp[k] = ans;}Â
// Driver codepublic static void main(String[] args){  // pre-calculating   // the factors  precompute();Â
  int arr[] = {2, 8, 16,                55, 99, 100};  int n = arr.length;  System.out.print(solve(arr, 0, n));}}Â
// This code is contributed by Rajput-Ji |
Python3
# Python3 code to count minimum factor jumps# to reach the end of array  # vector to store factors of each integerfactors= [[] for i in range(100005)];  # dp arraydp = [0 for i in range(100005)];  # Precomputing all factors of integers# from 1 to 100000def precompute():Â
    for i in range(1, 100001):        for j in range(i, 100001, i):                 factors[j].append(i);  # Function to count the minimum jumpsdef solve(arr, k, n):      # If we reach the end of array,    # no more jumps are required    if (k == n - 1):        return 0;         # If the jump results in out of index,    # return INT_MAX    if (k >= n):        return 1000000000         # If the answer has been already computed,    # return it directly    if (dp[k]):        return dp[k];         # Else compute the answer    # using the recurrence relation    ans = 1000000000      # Iterating over all choices of jumps    for j in factors[arr[k]]:          # Considering current factor as a jump        res = solve(arr, k + j, n);          # Jump leads to the destination        if (res != 1000000000):            ans = min(ans, res + 1);             # Return ans and memorize it    dp[k] = ans;    return ansÂ
# Driver codeif __name__=='__main__':      # pre-calculating the factors    precompute()      arr = [ 2, 8, 16, 55, 99, 100 ]    n = len(arr)         print(solve(arr, 0, n))  # This code is contributed by rutvik_56 |
C#
// C# code to count minimum // factor jumps to reach the // end of arrayusing System;using System.Collections.Generic;class GFG{Â
// vector to store factors // of each integerstatic List<int> []factors = Â Â Â Â Â Â Â Â Â Â Â Â new List<int>[100005];Â
// dp arraystatic int []dp = new int[100005];Â
// Precomputing all factors // of integers from 1 to 100000static void precompute(){Â Â for (int i = 0; Â Â Â Â Â Â Â Â Â Â Â i < factors.Length; i++)Â Â Â Â factors[i] = new List<int>();Â Â for (int i = 1; i <= 100000; i++) Â Â {Â Â Â Â for (int j = i; Â Â Â Â Â Â Â Â Â Â Â Â Â j <= 100000; j += i) Â Â Â Â {Â Â Â Â Â Â factors[j].Add(i);Â Â Â Â }Â Â }}Â
// Function to count the // minimum jumpsstatic int solve(int []arr,                  int k, int n){  // If we reach the end of   // array, no more jumps   // are required  if (k == n - 1)   {    return 0;  }Â
  // If the jump results in   // out of index, return   // int.MaxValue  if (k >= n)   {    return int.MaxValue;  }Â
  // If the answer has been   // already computed, return   // it directly  if (dp[k] != 0)   {    return dp[k];  }Â
  // Else compute the answer  // using the recurrence relation  int ans = int.MaxValue;Â
  // Iterating over all choices   // of jumps  foreach (int j in factors[arr[k]])   {    // Considering current     // factor as a jump    int res = solve(arr, k + j, n);Â
    // Jump leads to the     // destination    if (res != int.MaxValue)     {      ans = Math.Min(ans, res + 1);    }  }Â
  // Return ans and   // memorize it  return dp[k] = ans;}Â
// Driver codepublic static void Main(String[] args){  // pre-calculating   // the factors  precompute();Â
  int []arr = {2, 8, 16,                55, 99, 100};  int n = arr.Length;  Console.Write(solve(arr, 0, n));}}Â
// This code is contributed by shikhasingrajput |
Javascript
<script>// Javascript code to count minimum factor jumps// to reach the end of arrayÂ
Â
// vector to store factors of each integerlet factors = new Array();Â
// dp arraylet dp = new Array(100005);Â
for (let i = 0; i < 100005; i++) {Â Â Â Â factors.push(new Array());}Â
Â
// Precomputing all factors of integers// from 1 to 100000function precompute() {Â Â Â Â for (let i = 1; i <= 100000; i++) {Â Â Â Â Â Â Â Â for (let j = i; j <= 100000; j += i) {Â Â Â Â Â Â Â Â Â Â Â Â factors[j].push(i);Â Â Â Â Â Â Â Â }Â Â Â Â }}Â
// Function to count the minimum jumpsfunction solve(arr, k, n) {Â
    // If we reach the end of array,    // no more jumps are required    if (k == n - 1) {        return 0;    }Â
    // If the jump results in out of index,    // return INT_MAX    if (k >= n) {        return Number.MAX_SAFE_INTEGER;    }Â
    // If the answer has been already computed,    // return it directly    if (dp[k]) {        return dp[k];    }Â
    // Else compute the answer    // using the recurrence relation    let ans = Number.MAX_SAFE_INTEGER;Â
    // Iterating over all choices of jumps    for (let j of factors[arr[k]]) {Â
        // Considering current factor as a jump        let res = solve(arr, k + j, n);Â
        // Jump leads to the destination        if (res != Number.MAX_SAFE_INTEGER) {            ans = Math.min(ans, res + 1);        }    }Â
    // Return ans and memorize it    return dp[k] = ans;}Â
// Driver codeÂ
// pre-calculating the factorsprecompute();Â
let arr = [2, 8, 16, 55, 99, 100];let n = arr.length;Â
document.write(solve(arr, 0, n));Â
// This code is contributed by _saurabh_jaiswal</script> |
2
Time Complexity: O(100000*N)
Auxiliary Space: O(100005)
Given below is the Iterative Bottom-Up Approach:
C++
// C++ program for bottom up approach#include <bits/stdc++.h>using namespace std;Â
// Vector to store factors of each integervector<int> factors[100005];Â
// Initialize the dp arrayint dp[100005];Â
// Precompute all the// factors of every integervoid precompute(){Â Â Â Â for (int i = 1; i <= 100000; i++) {Â Â Â Â Â Â Â Â for (int j = i; j <= 100000; j += i)Â Â Â Â Â Â Â Â Â Â Â Â factors[j].push_back(i);Â Â Â Â }}Â
// Function to count the// minimum factor jumpint solve(int arr[], int n){Â
    // Initialise minimum jumps to    // reach each cell as INT_MAX    for (int i = 0; i <= 100005; i++) {        dp[i] = INT_MAX;    }Â
    // 0 jumps required to    // reach the first cell    dp[0] = 0;Â
    // Iterate over all cells    for (int i = 0; i < n; i++) {        // calculating for each jump        for (auto j : factors[arr[i]]) {            // If a cell is in bound            if (i + j < n)                dp[i + j] = min(dp[i + j], 1 + dp[i]);        }    }    // Return minimum jumps    // to reach last cell    return dp[n - 1];}Â
// Driver codeint main(){    // Pre-calculating the factors    precompute();    int arr[] = { 2, 8, 16, 55, 99, 100 };    int n = sizeof(arr) / sizeof(arr[0]);Â
    // Function call    cout << solve(arr, n);} |
Java
// Java program for bottom up approachimport java.util.*;Â
class GFG{Â
// Vector to store factors of each integer@SuppressWarnings("unchecked")static Vector<Integer> []factors = new Vector[100005];Â
// Initialize the dp arraystatic int []dp = new int[100005];Â
// Precompute all the// factors of every integerstatic void precompute(){Â Â Â Â for(int i = 1; i <= 100000; i++) Â Â Â Â {Â Â Â Â Â Â Â Â for(int j = i; j <= 100000; j += i)Â Â Â Â Â Â Â Â Â Â Â Â factors[j].add(i);Â Â Â Â }}Â
// Function to count the// minimum factor jumpstatic int solve(int arr[], int n){         // Initialise minimum jumps to    // reach each cell as Integer.MAX_VALUE    for(int i = 0; i < 100005; i++)     {        dp[i] = Integer.MAX_VALUE;    }Â
    // 0 jumps required to    // reach the first cell    dp[0] = 0;Â
    // Iterate over all cells    for(int i = 0; i < n; i++)    {                 // Calculating for each jump        for(int j : factors[arr[i]])         {                         // If a cell is in bound            if (i + j < n)                dp[i + j] = Math.min(dp[i + j],                                         1 + dp[i]);        }    }         // Return minimum jumps    // to reach last cell    return dp[n - 1];}Â
// Driver codepublic static void main(String[] args){    for(int i = 0; i < factors.length; i++)        factors[i] = new Vector<Integer>();             // Pre-calculating the factors    precompute();    int arr[] = { 2, 8, 16, 55, 99, 100 };    int n = arr.length;Â
    // Function call    System.out.print(solve(arr, n));}}Â
// This code is contributed by Princi Singh |
Python3
# Python3 program for bottom up approach  # Vector to store factors of each integerfactors=[[] for i in range(100005)];  # Initialize the dp arraydp=[1000000000 for i in range(100005)];  # Precompute all the# factors of every integerdef precompute():         for i in range(1, 100001):                 for j in range(i, 100001, i):                 factors[j].append(i);      # Function to count the# minimum factor jumpdef solve(arr, n):       # 0 jumps required to    # reach the first cell    dp[0] = 0;      # Iterate over all cells    for i in range(n):             # calculating for each jump        for j in factors[arr[i]]:                     # If a cell is in bound            if (i + j < n):                dp[i + j] = min(dp[i + j], 1 + dp[i]);             # Return minimum jumps    # to reach last cell    return dp[n - 1];  # Driver codeif __name__=='__main__':         # Pre-calculating the factors    precompute();    arr = [ 2, 8, 16, 55, 99, 100 ]    n=len(arr)      # Function call    print(solve(arr,n))         # This code is contributed by pratham76 |
C#
// C# program for bottom up approachusing System;using System.Collections.Generic;Â
class GFG{Â Â Â Â Â // Vector to store factors of each integerstatic List<List<int>> factors = new List<List<int>>();Â
// Initialize the dp arraystatic int[] dp;Â
// Precompute all the// factors of every integerstatic void precompute(){Â Â Â Â for(int i = 1; i <= 100000; i++) Â Â Â Â {Â Â Â Â Â Â Â Â for(int j = i; j <= 100000; j += i)Â Â Â Â Â Â Â Â Â Â Â Â factors[j].Add(i);Â Â Â Â }}Â
// Function to count the// minimum factor jumpstatic int solve(int[] arr, int n){         // Initialise minimum jumps to    // reach each cell as Integer.MAX_VALUE    for(int i = 0; i < 100005; i++)     {        dp[i] = int.MaxValue;    }Â
    // 0 jumps required to    // reach the first cell    dp[0] = 0;Â
    // Iterate over all cells    for(int i = 0; i < n; i++)    {                 // Calculating for each jump        foreach(int j in factors[arr[i]])         {                         // If a cell is in bound            if (i + j < n)                dp[i + j] = Math.Min(dp[i + j],                                         1 + dp[i]);        }    }         // Return minimum jumps    // to reach last cell    return dp[n - 1];}Â
// Driver codestatic public void Main (){    for(int i = 0; i < 100005; i++)        factors.Add(new List<int>());         dp = new int[100005];         // Pre-calculating the factors    precompute();    int[] arr = { 2, 8, 16, 55, 99, 100 };    int n = arr.Length;         // Function call    Console.Write(solve(arr, n));}}Â
// This code is contributed by offbeat |
Javascript
<script>Â
// Javascript program for bottom up approachÂ
// Vector to store factors of each integervar factors = Array.from(Array(100005), ()=>Array());Â
// Initialize the dp arrayvar dp = Array(100005);Â
// Precompute all the// factors of every integerfunction precompute(){Â Â Â Â for (var i = 1; i <= 100000; i++) {Â Â Â Â Â Â Â Â for (var j = i; j <= 100000; j += i)Â Â Â Â Â Â Â Â Â Â Â Â factors[j].push(i);Â Â Â Â }}Â
// Function to count the// minimum factor jumpfunction solve(arr, n){Â
    // Initialise minimum jumps to    // reach each cell as INT_MAX    for (var i = 0; i <= 100005; i++) {        dp[i] = 1000000000;    }Â
    // 0 jumps required to    // reach the first cell    dp[0] = 0;Â
    // Iterate over all cells    for (var i = 0; i < n; i++) {        // calculating for each jump        for (var j of factors[arr[i]]) {            // If a cell is in bound            if (i + j < n)                dp[i + j] = Math.min(dp[i + j], 1 + dp[i]);        }    }    // Return minimum jumps    // to reach last cell    return dp[n - 1];}Â
 // Driver code // Pre-calculating the factors precompute(); var arr = [2, 8, 16, 55, 99, 100]; var n = arr.length // Function call document.write( solve(arr, n));Â
</script> |
2
Time Complexity: O(N2)
Auxiliary Space:Â O(100005)
Ready to dive in? Explore our Free Demo Content and join our DSA course, trusted by over 100,000 zambiatek!



