
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 - 4 1 2 3
Final - 1 0 1 2
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 - 1 2 3 4
final - 4 1 2 3
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).