Given a weighting scale and an array of different positive weights where we have an infinite supply of each weight. Our task is to put weights on left and right pans of scale one by one in such a way that pans move to that side where weight is put i.e. each time, pans of scale moves to alternate sides.
- We are given another integer ‘steps’, times which we need to perform this operation.
- Another constraint is that we can’t put same weight consecutively i.e. if weight w is taken then in next step while putting the weight on opposite pan we can’t take w again.
Let weight array is [7, 11] and steps = 3 then 7, 11, 7 is the sequence in which weights should be kept in order to move scale alternatively. Let another weight array is [2, 3, 5, 6] and steps = 10 then, 3, 2, 3, 5, 6, 5, 3, 2, 3 is the sequence in which weights should be kept in order to move scale alternatively.
This problem can be solved by doing DFS among scale states.
- We traverse among various DFS states for the solution where each DFS state will correspond to actual difference value between left and right pans and current step count.
- Instead of storing weights of both pans, we just store the difference residue value and each time chosen weight value should be greater than this difference and shouldn’t be equal to previously chosen value of weight.
- If it is, then we call the DFS method recursively with new difference value and one more step.
Please see below code for better understanding,
2 3 2 3 5 6 5 3 2 3
This article is contributed by Utkarsh Trivedi. If you like GeeksforGeeks and would like to contribute, you can also write an article using contribute.geeksforgeeks.org or mail your article to firstname.lastname@example.org. See your article appearing on the GeeksforGeeks main page and help other Geeks.
Please write comments if you find anything incorrect, or you want to share more information about the topic discussed above.
- Barabasi Albert Graph (for Scale Free Models)
- Check loop in array according to given constraints
- Minimum steps to reach end of array under constraints
- Find the minimum number of moves needed to move from one cell of matrix to another
- Check if the array can be sorted using swaps between given indices only
- Find the weight of the minimum spanning tree
- Longest path in a directed Acyclic graph | Dynamic Programming
- Find the shortest distance between any pair of two different good nodes
- Even size subtree in n-ary tree
- Number of ways to select a node from each connected component
- Number of special nodes in an n-ary tree
- Minimum distance to the corner of a grid from source
- Ways to Remove Edges from a Complete Graph to make Odd Edges
- Number of connected components in a 2-D matrix of strings
Improved By : PranchalK