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.

**Examples:**

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,

## C++

`// C++ program to print weights for alternating ` `// the weighting scale ` `#include <bits/stdc++.h> ` `using` `namespace` `std; ` ` ` `// DFS method to traverse among states of weighting scales ` `bool` `dfs(` `int` `residue, ` `int` `curStep, ` `int` `wt[], ` `int` `arr[], ` ` ` `int` `N, ` `int` `steps) ` `{ ` ` ` `// If we reach to more than required steps, ` ` ` `// return true ` ` ` `if` `(curStep > steps) ` ` ` `return` `true` `; ` ` ` ` ` `// Try all possible weights and choose one which ` ` ` `// returns 1 afterwards ` ` ` `for` `(` `int` `i = 0; i < N; i++) ` ` ` `{ ` ` ` `/* Try this weight only if it is greater than ` ` ` `current residueand not same as previous chosen ` ` ` `weight */` ` ` `if` `(arr[i] > residue && arr[i] != wt[curStep - 1]) ` ` ` `{ ` ` ` `// assign this weight to array and recur for ` ` ` `// next state ` ` ` `wt[curStep] = arr[i]; ` ` ` `if` `(dfs(arr[i] - residue, curStep + 1, wt, ` ` ` `arr, N, steps)) ` ` ` `return` `true` `; ` ` ` `} ` ` ` `} ` ` ` ` ` `// if any weight is not possible, return false ` ` ` `return` `false` `; ` `} ` ` ` `// method prints weights for alternating scale and if ` `// not possible prints 'not possible' ` `void` `printWeightsOnScale(` `int` `arr[], ` `int` `N, ` `int` `steps) ` `{ ` ` ` `int` `wt[steps]; ` ` ` ` ` `// call dfs with current residue as 0 and current ` ` ` `// steps as 0 ` ` ` `if` `(dfs(0, 0, wt, arr, N, steps)) ` ` ` `{ ` ` ` `for` `(` `int` `i = 0; i < steps; i++) ` ` ` `cout << wt[i] << ` `" "` `; ` ` ` `cout << endl; ` ` ` `} ` ` ` `else` ` ` `cout << ` `"Not possible\n"` `; ` `} ` ` ` `// Driver code to test above methods ` `int` `main() ` `{ ` ` ` `int` `arr[] = {2, 3, 5, 6}; ` ` ` `int` `N = ` `sizeof` `(arr) / ` `sizeof` `(` `int` `); ` ` ` ` ` `int` `steps = 10; ` ` ` `printWeightsOnScale(arr, N, steps); ` ` ` `return` `0; ` `} ` |

*chevron_right*

*filter_none*

## Java

`// Java program to print weights for alternating ` `// the weighting scale ` `class` `GFG ` `{ ` ` ` ` ` `// DFS method to traverse among ` ` ` `// states of weighting scales ` ` ` `static` `boolean` `dfs(` `int` `residue, ` `int` `curStep, ` ` ` `int` `[] wt, ` `int` `[] arr, ` ` ` `int` `N, ` `int` `steps) ` ` ` `{ ` ` ` ` ` `// If we reach to more than required steps, ` ` ` `// return true ` ` ` `if` `(curStep >= steps) ` ` ` `return` `true` `; ` ` ` ` ` `// Try all possible weights and ` ` ` `// choose one which returns 1 afterwards ` ` ` `for` `(` `int` `i = ` `0` `; i < N; i++) ` ` ` `{ ` ` ` ` ` `/* ` ` ` `* Try this weight only if it is ` ` ` `* greater than current residue ` ` ` `* and not same as previous chosen weight ` ` ` `*/` ` ` `if` `(curStep - ` `1` `< ` `0` `|| ` ` ` `(arr[i] > residue && ` ` ` `arr[i] != wt[curStep - ` `1` `])) ` ` ` `{ ` ` ` ` ` `// assign this weight to array and ` ` ` `// recur for next state ` ` ` `wt[curStep] = arr[i]; ` ` ` `if` `(dfs(arr[i] - residue, curStep + ` `1` `, ` ` ` `wt, arr, N, steps)) ` ` ` `return` `true` `; ` ` ` `} ` ` ` `} ` ` ` ` ` `// if any weight is not possible, ` ` ` `// return false ` ` ` `return` `false` `; ` ` ` `} ` ` ` ` ` `// method prints weights for alternating scale ` ` ` `// and if not possible prints 'not possible' ` ` ` `static` `void` `printWeightOnScale(` `int` `[] arr, ` ` ` `int` `N, ` `int` `steps) ` ` ` `{ ` ` ` `int` `[] wt = ` `new` `int` `[steps]; ` ` ` ` ` `// call dfs with current residue as 0 ` ` ` `// and current steps as 0 ` ` ` `if` `(dfs(` `0` `, ` `0` `, wt, arr, N, steps)) ` ` ` `{ ` ` ` `for` `(` `int` `i = ` `0` `; i < steps; i++) ` ` ` `System.out.print(wt[i] + ` `" "` `); ` ` ` `System.out.println(); ` ` ` `} ` ` ` `else` ` ` `System.out.println(` `"Not Possible"` `); ` ` ` `} ` ` ` ` ` `// Driver Code ` ` ` `public` `static` `void` `main(String[] args) ` ` ` `{ ` ` ` `int` `[] arr = { ` `2` `, ` `3` `, ` `5` `, ` `6` `}; ` ` ` `int` `N = arr.length; ` ` ` `int` `steps = ` `10` `; ` ` ` `printWeightOnScale(arr, N, steps); ` ` ` `} ` `} ` ` ` `// This code is contributed by ` `// sanjeev2552 ` |

*chevron_right*

*filter_none*

## Python3

`# Python3 program to print weights for ` `# alternating the weighting scale ` ` ` `# DFS method to traverse among states ` `# of weighting scales ` `def` `dfs(residue, curStep, wt, arr, N, steps): ` ` ` ` ` `# If we reach to more than required ` ` ` `# steps, return true ` ` ` `if` `(curStep >` `=` `steps): ` ` ` `return` `True` ` ` ` ` `# Try all possible weights and choose ` ` ` `# one which returns 1 afterwards ` ` ` `for` `i ` `in` `range` `(N): ` ` ` ` ` `# Try this weight only if it is greater ` ` ` `# than current residueand not same as ` ` ` `# previous chosen weight ` ` ` `if` `(arr[i] > residue ` `and` ` ` `arr[i] !` `=` `wt[curStep ` `-` `1` `]): ` ` ` ` ` `# assign this weight to array and ` ` ` `# recur for next state ` ` ` `wt[curStep] ` `=` `arr[i] ` ` ` `if` `(dfs(arr[i] ` `-` `residue, curStep ` `+` `1` `, ` ` ` `wt, arr, N, steps)): ` ` ` `return` `True` ` ` ` ` `# if any weight is not possible, ` ` ` `# return false ` ` ` `return` `False` ` ` `# method prints weights for alternating scale ` `# and if not possible prints 'not possible' ` `def` `printWeightsOnScale(arr, N, steps): ` ` ` `wt ` `=` `[` `0` `] ` `*` `(steps) ` ` ` ` ` `# call dfs with current residue as 0 ` ` ` `# and current steps as 0 ` ` ` `if` `(dfs(` `0` `, ` `0` `, wt, arr, N, steps)): ` ` ` `for` `i ` `in` `range` `(steps): ` ` ` `print` `(wt[i], end ` `=` `" "` `) ` ` ` `else` `: ` ` ` `print` `(` `"Not possible"` `) ` ` ` `# Driver Code ` `if` `__name__ ` `=` `=` `'__main__'` `: ` ` ` ` ` `arr ` `=` `[` `2` `, ` `3` `, ` `5` `, ` `6` `] ` ` ` `N ` `=` `len` `(arr) ` ` ` ` ` `steps ` `=` `10` ` ` `printWeightsOnScale(arr, N, steps) ` ` ` `# This code is contributed by PranchalK ` |

*chevron_right*

*filter_none*

**Output:**

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 contribute@geeksforgeeks.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.

Attention reader! Don’t stop learning now. Get hold of all the important DSA concepts with the **DSA Self Paced Course** at a student-friendly price and become industry ready.

## Recommended Posts:

- Minimum steps to reach end of array under constraints
- Barabasi Albert Graph (for Scale Free Models)
- Check loop in array according to given constraints
- Count of elements which form a loop in an Array according to given constraints
- Check if alternate path exists from U to V with smaller individual weight in a given Graph
- Find the minimum number of moves needed to move from one cell of matrix to another
- Check if the given array can be constructed uniquely from the given set of subsequences
- Check whether a given graph is Bipartite or not
- Given a sorted dictionary of an alien language, find order of characters
- Given a matrix of ‘O’ and ‘X’, replace 'O' with 'X' if surrounded by 'X'
- Minimize Cash Flow among a given set of friends who have borrowed money from each other
- Print all paths from a given source to a destination
- Optimal read list for given number of days
- Print all Jumping Numbers smaller than or equal to a given value
- Check whether given degrees of vertices represent a Graph or Tree
- Find all reachable nodes from every node present in a given set
- Check if removing a given edge disconnects a graph
- Check if a given directed graph is strongly connected | Set 2 (Kosaraju using BFS)
- Check if a given graph is tree or not
- Print all paths from a given source to a destination using BFS