Description
Solutions
Minimum Steps to Achieve Target State
🔥 FULLTIME

Given 4 Jugs namely [J1, J2, J3, J4] with capacities [C1, C2, C3, C4] and initial water content as [S1, S2, S3, S4]. Determine how many steps are needed to achieve the final state of [F1, F2, F3, F4] by transferring water from one jug to another without losing any water.

Input:

Total number of entries = 13

  • First line specifies the number of entries in the array, which is 12 in our case
  • Next 4 lines contain the capacities of the 4 jugs
  • Next 4 lines contain the initial content of the 4 jugs
  • Next 4 lines contain the final content of the 4 jugs
  • Output:

    The minimum number of steps required to reach Final State (F) from Initial State (S).

    Return -1 if not possible.

    Example 1:

    Input:  capacities = [12, 13, 12, 10], initial = [6, 6, 0, 0], target = [12, 0, 0, 0]
    Output: 1
    Explanation:

    In one step, we can transfer 6 units of water from J1 and J2 to J3, achieving the final state of [12, 0, 0, 0].

    Constraints:
      0 < Ci <= 500 for each i: [1,4]
      0 < Si, Fi <= Ci for each i: [1,4]
      Sum(Si) = Sum(Fi)
    Thumbnail 0
    Testcase

    Result
    Case 1

    input:

    output: