Maximum path sum in the given arrays with at most K jumps

Given three arrays A, B and C each having N elements, the task is to find the maximum sum that can be obtained along any valid path with at most K jumps.

A path is valid if it follows the following properties:

  1. It starts from 0th index of an array.
  2. It ends at (N-1)th index of an array.
  3. For any element in the path at index i, the next element should be on the index i+1 of either current or adjacent array only.
  4. If the path involves selecting the next (i + 1)th element from the adjacent array, instead of current one, then it is said to be 1 jump

Examples:

Input: A[] = {4, 5, 1, 2, 10}, B[] = {9, 7, 3, 20, 16}, C[] = {6, 12, 13, 9, 8}, K = 2
Output: 70
Explanation:
Starting from array B and selecting the elements as follows:
Select B[0]: 9 => sum = 9
Jump to C[1]: 12 => sum = 21
Select C[2]: 13 => sum = 34
Jump to B[3]: 20 => sum = 54
Select B[4]: 16 => sum = 70
Therefore maximum sum with at most 2 jumps = 70

Input: A[] = {10, 4, 1, 8}, B[] = {9, 0, 2, 5}, C[] = {6, 2, 7, 3}, K = 2
Output: 24



Intuitive Greedy Approach (Not Correct): One possible idea to solve the problem could be to pick the maximum element at the current index and move to the next index having maximum value either from the current array or the adjacent array if jumps are left.

For example:

Given,
A[] = {4, 5, 1, 2, 10},
B[] = {9, 7, 3, 20, 16},
C[] = {6, 12, 13, 9, 8},
K = 2

Finding the solution using Greedy approach:

Current maximum: 9, K = 2, sum = 9
Next maximum: 12, K = 1, sum = 12
Next maximum: 13, K = 1, sum = 25
Next maximum: 20, K = 0, sum = 45
Adding rest of elements: 16, K = 0, sum = 61

Clearly, this is not the maximum sum.

Hence this approach is incorrect.

Dynamic Programing Approach: The DP can be used in two steps – Recursion and Memorization.

  1. Recursion: The problem could be break down using following recursive relation:
    • On Array A, index i with K jumps

      pathSum(A, i, k) = A[i] + max(pathSum(A, i+1, k), pathSum(B, i+1, k-1));

    • Similiarly on Array B,

      pathSum(B, i, k) = B[i] + max(pathSum(B, i+1, k), max(pathSum(A, i+1, k-1), pathSum(C, i+1, k-1));

    • Similiarly on Array C,

      pathSum(C, i, k) = C[i] + max(pathSum(C, i+1, k), pathSum(B, i+1, k-1));

    • Therefore the maximum sum can be found as:

      maxSum = max(pathSum(A, i, k), max(pathSum(B, i, k), pathSum(C, i, k)));

  2. Memorisation: The complexity of the above recursion solution can be reduce with help of memorisation.
    • Store the results after calculating in a 3-dimensional array (dp) of size [3][N][K].
    • The value of any element of dp array stores the maximum sum on ith index with x jumps left in an array

Below is the implementation of the approach:

C++

filter_none

edit
close

play_arrow

link
brightness_4
code

// C++ program to maximum path sum in
// the given arrays with at most K jumps
  
#include <iostream>
using namespace std;
  
#define M 3
#define N 5
#define K 2
  
int dp[M][N][K];
  
void initializeDp()
{
    for (int i = 0; i < M; i++)
        for (int j = 0; j < N; j++)
            for (int k = 0; k < K; k++)
                dp[i][j][k] = -1;
}
  
// Function to calculate maximum path sum
int pathSum(int* a, int* b, int* c,
            int i, int n,
            int k, int on)
{
    // Base Case
    if (i == n)
        return 0;
  
    if (dp[on][i][k] != -1)
        return dp[on][i][k];
  
    int current, sum;
    switch (on) {
    case 0:
        current = a[i];
        break;
    case 1:
        current = b[i];
        break;
    case 2:
        current = c[i];
        break;
    }
  
    // No jumps available.
    // Hence pathSum can be
    // from current array only
    if (k == 0) {
        return dp[on][i][k]
               = current
                 + pathSum(a, b, c, i + 1,
                           n, k, on);
    }
  
    // Since jumps are available
    // pathSum can be from current
    // or adjacent array
    switch (on) {
    case 0:
        sum = current
              + max(pathSum(a, b, c, i + 1,
                            n, k - 1, 1),
                    pathSum(a, b, c, i + 1,
                            n, k, 0));
        break;
    case 1:
        sum = current
              + max(pathSum(a, b, c, i + 1,
                            n, k - 1, 0),
                    max(pathSum(a, b, c, i + 1,
                                n, k, 1),
                        pathSum(a, b, c, i + 1,
                                n, k - 1, 2)));
        break;
    case 2:
        sum = current
              + max(pathSum(a, b, c, i + 1,
                            n, k - 1, 1),
                    pathSum(a, b, c, i + 1,
                            n, k, 2));
        break;
    }
  
    return dp[on][i][k] = sum;
}
  
void findMaxSum(int* a, int* b,
                int* c, int n, int k)
{
    int sum = 0;
  
    // Creating the DP array for memorisation
    initializeDp();
  
    // Find the pathSum using recursive approach
    for (int i = 0; i < 3; i++) {
  
        // Maximise the sum
        sum = max(sum,
                  pathSum(a, b, c, 0,
                          n, k, i));
    }
  
    cout << sum;
}
  
// Driver Code
int main()
{
    int n = 5, k = 1;
    int A[n] = { 4, 5, 1, 2, 10 };
    int B[n] = { 9, 7, 3, 20, 16 };
    int C[n] = { 6, 12, 13, 9, 8 };
  
    findMaxSum(A, B, C, n, k);
  
    return 0;
}

chevron_right


Java

filter_none

edit
close

play_arrow

link
brightness_4
code

// Java program to maximum path sum in
// the given arrays with at most K jumps
import java.util.*;
class GFG
{
    static int M = 3;
    static int N = 5;
    static int K = 2;
      
    static int dp[][][] = new int[M][N][K];
      
    static void initializeDp()
    {
        for (int i = 0; i < M; i++)
            for (int j = 0; j < N; j++)
                for (int k = 0; k < K; k++)
                    dp[i][j][k] = -1;
    }
      
    // Function to calculate maximum path sum
    static int pathSum(int a[], int b[], int c[],
                int i, int n,
                int k, int on)
    {
        // Base Case
        if (i == n)
            return 0;
      
        if (dp[on][i][k] != -1)
            return dp[on][i][k];
      
        int current = 0, sum = 0;
          
        switch (on) {
        case 0:
            current = a[i];
            break;
        case 1:
            current = b[i];
            break;
        case 2:
            current = c[i];
            break;
        }
      
        // No jumps available.
        // Hence pathSum can be
        // from current array only
        if (k == 0) {
            return dp[on][i][k]
                = current
                    + pathSum(a, b, c, i + 1,
                            n, k, on);
        }
      
        // Since jumps are available
        // pathSum can be from current
        // or adjacent array
        switch (on) {
        case 0:
            sum = current
                + Math.max(pathSum(a, b, c, i + 1,
                                n, k - 1, 1),
                        pathSum(a, b, c, i + 1,
                                n, k, 0));
            break;
        case 1:
            sum = current
                + Math.max(pathSum(a, b, c, i + 1,
                                n, k - 1, 0),
                        Math.max(pathSum(a, b, c, i + 1,
                                    n, k, 1),
                            pathSum(a, b, c, i + 1,
                                    n, k - 1, 2)));
            break;
        case 2:
            sum = current
                + Math.max(pathSum(a, b, c, i + 1,
                                n, k - 1, 1),
                        pathSum(a, b, c, i + 1,
                                n, k, 2));
            break;
        }
      
        return dp[on][i][k] = sum;
    }
      
    static void findMaxSum(int a[], int b[],
                    int c[], int n, int k)
    {
        int sum = 0;
      
        // Creating the DP array for memorisation
        initializeDp();
      
        // Find the pathSum using recursive approach
        for (int i = 0; i < 3; i++) {
      
            // Maximise the sum
            sum = Math.max(sum,
                    pathSum(a, b, c, 0,
                            n, k, i));
        }
      
        System.out.print(sum);
    }
      
    // Driver Code
    public static void main(String []args)
    {
        int n = 5, k = 1;
        int A[] = { 4, 5, 1, 2, 10 };
        int B[] = { 9, 7, 3, 20, 16 };
        int C[] = { 6, 12, 13, 9, 8 };
      
        findMaxSum(A, B, C, n, k);
    }
}
  
// This code is contributed by chitranayal

chevron_right


C#

filter_none

edit
close

play_arrow

link
brightness_4
code

// C# program to maximum path sum in
// the given arrays with at most K jumps
using System;
  
class GFG{
      
static int M = 3;
static int N = 5;
static int K = 2;
      
static int [,,]dp = new int[M, N, K];
      
static void initializeDp()
{
    for(int i = 0; i < M; i++)
       for(int j = 0; j < N; j++)
          for(int k = 0; k < K; k++)
             dp[i, j, k] = -1;
}
      
// Function to calculate maximum path sum
static int pathSum(int []a, int []b, int []c,
                   int i, int n,
                   int k, int on)
{
      
    // Base Case
    if (i == n)
        return 0;
      
    if (dp[on, i, k] != -1)
        return dp[on, i, k];
      
    int current = 0, sum = 0;
          
    switch (on)
    {
    case 0:
        current = a[i];
        break;
    case 1:
        current = b[i];
        break;
    case 2:
        current = c[i];
        break;
    }
      
    // No jumps available.
    // Hence pathSum can be
    // from current array only
    if (k == 0)
    {
        return dp[on, i, k] = current + 
                              pathSum(a, b, c, i + 1,
                                      n, k, on);
    }
      
    // Since jumps are available
    // pathSum can be from current
    // or adjacent array
    switch (on)
    {
    case 0:
        sum = current + Math.Max(pathSum(a, b, c, i + 1,
                                         n, k - 1, 1),
                                 pathSum(a, b, c, i + 1,
                                         n, k, 0));
        break;
    case 1:
        sum = current + Math.Max(pathSum(a, b, c, i + 1,
                                         n, k - 1, 0),
                        Math.Max(pathSum(a, b, c, i + 1,
                                          n, k, 1),
                                 pathSum(a, b, c, i + 1,
                                         n, k - 1, 2)));
        break;
    case 2:
        sum = current + Math.Max(pathSum(a, b, c, i + 1,
                                         n, k - 1, 1),
                                 pathSum(a, b, c, i + 1,
                                         n, k, 2));
        break;
    }
      
    return dp[on, i, k] = sum;
}
      
static void findMaxSum(int []a, int []b,
                       int []c, int n, int k)
{
    int sum = 0;
      
    // Creating the DP array for memorisation
    initializeDp();
      
    // Find the pathSum using recursive approach
    for(int i = 0; i < 3; i++)
    {
         
       // Maximise the sum
       sum = Math.Max(sum, pathSum(a, b, c, 0,
                                   n, k, i));
    }
    Console.Write(sum);
}
      
// Driver Code
public static void Main(String []args)
{
    int n = 5, k = 1;
    int []A = { 4, 5, 1, 2, 10 };
    int []B = { 9, 7, 3, 20, 16 };
    int []C = { 6, 12, 13, 9, 8 };
      
    findMaxSum(A, B, C, n, k);
}
}
  
// This code is contributed by gauravrajput1

chevron_right


Output:

67

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

Don’t stop now and take your learning to the next level. Learn all the important concepts of Data Structures and Algorithms with the help of the most trusted course: DSA Self Paced. Become industry ready at a student-friendly price.




My Personal Notes arrow_drop_up

Check out this Author's contributed articles.

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.



Improved By : chitranayal, GauravRajput1