Skip to main content

Cisco

Cisco OA 2022 - Water Balancing between 4 Jugs

Problem:

Given 4 Jugs namely [J1, J2, J3, J4] with capacities [C1, C2, C3, C4] and initial water content as [S1, S2, S3, S4].

Goal:
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.

Constraints:

  1. 0 < C[i] <= 500 for each i: [1, 4]
  2. 0 <= S[i], F[i] <= C[i] for each i: [1, 4]
  3. Sum(S[i]) = Sum(F[i])

Input:

  1. The first line specifies the number of entries in the array, which is 12 in our case.
  2. The next 4 lines contain the capacities of the 4 jugs.
  3. The next 4 lines contain the initial content of the 4 jugs.
  4. The next 4 lines contain the final content of the 4 jugs.

Output:

The minimum number of steps required to reach the final state (F) from the initial state (S). Return -1 if it's not possible.

Examples:

12
13
12
10
5
6
6
0
0
12
0
0
0

Output: 1


Explanation: Step 1: Pour 6 Litres of water from Jug 2 to Jug 1.