Answered step by step
Verified Expert Solution
Link Copied!

Question

1 Approved Answer

Water Balancing between 4 Jugs Problem: Given 4 Jugs namely J 1 , J 2 , J 3 , J 4 with capacities [ C

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:
i:[1,4]1,4?S=um(Fi)=130Ci for each i : 1,4
Sum(Si)S=um(Fi)
Input:
Total number of entries =130 for each i:[1,4]
0Ci for each i : 1,4
Sum(Si)S=um(Fi)
Input:
Total number of entries =13
Question is to find minimum number of steps to make water in jars from initial to final. Question seems to be straight forward but I do not know what I am missing only half testcases passed.
So after checking edge cases like final state of no jar is greater than its capacity and total initial water is equal to total final water.
I tried some like this. Created 2 variable less and more. less will store number of jars that will have less water in final jar than in initial jar and more will store number of jars that will have more water in final jar than in initial jar. If min(less, more)=1, then answer will be max(less, more). This can be justified. Lets say we have less water in one jar only this means at least one other jar has more water that we need to pour.
Take examples,
Initial -4123
Final -1012
In this case we need to pour water from jars that has more water, it can be 1,2 or 3.
In min(less, more)==0, means all are equal because only one jar can not have more or less water since total water is same so total more and total less should be same.
lets say
Initial -1234
final -4123
Like previous case min(less, more) is 1, and we need to pour all water from jars that has less water.
Case when less == more ==2, I take all values of less and more and check if these are complimentary. For example if one jar has 1 unit extra water then if another jar has 1 unit less water then answer will be 2 since these will complete each other. If not then answer will be 3 because we either water to take some water from jar or give and all will interact if there are 3 steps (I am not sure I just tried multiple examples and this comes to be true , comment if you get it wrong).
image text in transcribed

Step by Step Solution

There are 3 Steps involved in it

Step: 1

blur-text-image

Get Instant Access to Expert-Tailored Solutions

See step-by-step solutions with expert insights and AI powered tools for academic success

Step: 2

blur-text-image

Step: 3

blur-text-image

Ace Your Homework with AI

Get the answers you need in no time with our AI-driven, step-by-step assistance

Get Started

Recommended Textbook for

Management Accounting For Beginners

Authors: Nicholas Apostolides

1st Edition

0815351224, 978-0815351221

Students also viewed these Databases questions