VOOZH about

URL: https://www.geeksforgeeks.org/dsa/minimize-cost-to-traverse-the-arrays-with-given-array-switching-cost/

⇱ Minimize cost to traverse the Arrays with given Array switching cost - GeeksforGeeks


  • Courses
  • Tutorials
  • Interview Prep

Minimize cost to traverse the Arrays with given Array switching cost

Last Updated : 23 Jul, 2025

Given three arrays A[], B[] and C[] of size N each and two integers X and Y, the task is to, find the minimum cost to reach the end of the arrays with the following conditions:

  • Select one of the three elements at every index. 
  • For switching from 1st array to 2nd or vice versa, extra X points are required and 
  • For switching from 2nd array to 3rd or vice versa, extra Y points are required.
  • The first element (at index 0) must be selected from 1st array only.

Note: It's not possible to switch from 1st to 3rd array directly or vice versa.

Examples:

Input:  X = 6, Y = 3
A[] = { 30, 10, 40, 2, 30, 30 }
B[] = { 10, -5, 10, 50, 7,  18 }
C[] = { 4, 20, 7, 23, 2, 10 }
Output: 75
Explanation: We select this elements at index (0 to N-1). 
A[] = { 30, 10, 40, 2, 30, 30 }
B[] = { 10, -5, 10, 50, 7,  18 }
C[] = { 4, 20, 7, 23, 2, 10 }
From index 0 we have to select 30. So Total cost to traverse up to index 0 is 30.
Now At index 1, 3 options (10, -5, 20). So select -5 from 2nd array.
So extra X (X = 6) cost required to shift from 1st array to 2nd. 
So Total cost to traverse up to index 1 is 30 + 6 - 5 = 31.
At index 2, select 10. So Total cost to traverse upto index 2 is 31+10= 41.
At index 3, select 2 from 1st array,  
So extra X (X = 6) points required to shift from 2nd array to 1st. 
So Total cost to traverse upto index 3 is 41+6+2 = 49 and so on.
So, total cost = 30 + (6+(-5)) + 10 + (6+2) + (6+7) + (3+10) = 75
6 and 3 are the extra costs required to change array.

Input:  X = -5, Y = 4
A[] = { 30, 10, -15, 10, 50, 7 }
B[] = { 4, 20, 7, 23, 2, 18 }
C[] = { 30, 10, 40, 2, 30, 10 }
Output: 34

Approach: The problem can be solved based on the Greedy Approach based on the following idea:

At each index try to include the element from the array that will include the minimum cost. And also keep in mind that we can go from A[] to B[], B[] to C[], B[] to A[] and C[] to B[] not from A[] to C[] or C[] to A[]

Follow the below steps to implement the idea:

  • Create a 2D array (say min_pnts[N][3]).
  • Traverse the array from the last index to the first index and in each iteration:
    • The value of min_pnts[i][0] depends on min_pnts[i+1][0] and min_pnts[i+1][1], value of min_pnts[i][1] depends on min_pnts[i+1][1], min_pnts[i+1][0] and min_pnts[i+1][2], value of min_pnts[i][2] depends on min_pnts[i+1][2] and min_pnts[i+1][1].
    • Check which path required minimum points (i.e. with switching array or not).
    • Store the minimum points required if we select the current index element from the 1st, 2nd, or 3rd array.
  • Return the minimum points required to traverse the array.

Below is the implementation of the above approach.


Output
75

Time Complexity: O(N)
Auxiliary space: O(N)

Comment
Article Tags: