Move weighting scale alternate under given constraints
Last Updated :
20 Feb, 2023
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++
#include <bits/stdc++.h>
using namespace std;
bool dfs( int residue, int curStep, int wt[], int arr[],
int N, int steps)
{
if (curStep > steps)
return true ;
for ( int i = 0; i < N; i++)
{
if (arr[i] > residue && arr[i] != wt[curStep - 1])
{
wt[curStep] = arr[i];
if (dfs(arr[i] - residue, curStep + 1, wt,
arr, N, steps))
return true ;
}
}
return false ;
}
void printWeightsOnScale( int arr[], int N, int steps)
{
int wt[steps];
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" ;
}
int main()
{
int arr[] = {2, 3, 5, 6};
int N = sizeof (arr) / sizeof ( int );
int steps = 10;
printWeightsOnScale(arr, N, steps);
return 0;
}
|
Java
class GFG
{
static boolean dfs( int residue, int curStep,
int [] wt, int [] arr,
int N, int steps)
{
if (curStep >= steps)
return true ;
for ( int i = 0 ; i < N; i++)
{
if (curStep - 1 < 0 ||
(arr[i] > residue &&
arr[i] != wt[curStep - 1 ]))
{
wt[curStep] = arr[i];
if (dfs(arr[i] - residue, curStep + 1 ,
wt, arr, N, steps))
return true ;
}
}
return false ;
}
static void printWeightOnScale( int [] arr,
int N, int steps)
{
int [] wt = new int [steps];
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" );
}
public static void main(String[] args)
{
int [] arr = { 2 , 3 , 5 , 6 };
int N = arr.length;
int steps = 10 ;
printWeightOnScale(arr, N, steps);
}
}
|
Python3
def dfs(residue, curStep, wt, arr, N, steps):
if (curStep > = steps):
return True
for i in range (N):
if (arr[i] > residue and
arr[i] ! = wt[curStep - 1 ]):
wt[curStep] = arr[i]
if (dfs(arr[i] - residue, curStep + 1 ,
wt, arr, N, steps)):
return True
return False
def printWeightsOnScale(arr, N, steps):
wt = [ 0 ] * (steps)
if (dfs( 0 , 0 , wt, arr, N, steps)):
for i in range (steps):
print (wt[i], end = " " )
else :
print ( "Not possible" )
if __name__ = = '__main__' :
arr = [ 2 , 3 , 5 , 6 ]
N = len (arr)
steps = 10
printWeightsOnScale(arr, N, steps)
|
C#
using System;
namespace GFG
{
class Program
{
static bool dfs( int residue, int curStep,
int [] wt, int [] arr,
int N, int steps)
{
if (curStep >= steps)
return true ;
for ( int i = 0; i < N; i++)
{
if (curStep - 1 < 0 || (arr[i] > residue && arr[i] != wt[curStep - 1]))
{
wt[curStep] = arr[i];
if (dfs(arr[i] - residue, curStep + 1, wt, arr, N, steps))
return true ;
}
}
return false ;
}
static void printWeightOnScale( int [] arr, int N, int steps)
{
int [] wt = new int [steps];
if (dfs(0, 0, wt, arr, N, steps))
{
for ( int i = 0; i < steps; i++)
Console.Write(wt[i] + " " );
Console.WriteLine();
}
else
Console.WriteLine( "Not Possible" );
}
static void Main( string [] args)
{
int [] arr = { 2, 3, 5, 6 };
int N = arr.Length;
int steps = 10;
printWeightOnScale(arr, N, steps);
}
}
}
|
Javascript
function dfs(residue, curStep, wt, arr, N, steps)
{
if (curStep > steps) {
return true ;
}
for (let i = 0; i < N; i++)
{
if (arr[i] > residue && arr[i] !== wt[curStep - 1])
{
wt[curStep] = arr[i];
if (dfs(arr[i] - residue, curStep + 1, wt, arr, N, steps)) {
return true ;
}
}
}
return false ;
}
function printWeightsOnScale(arr, N, steps) {
const wt = new Array(steps);
if (dfs(0, 1, wt, arr, N, steps)) {
for (let i = 1; i <= steps; i++) {
process.stdout.write(`${wt[i]} `);
}
console.log();
} else {
console.log( "Not possible" );
}
}
const arr = [2, 3, 5, 6];
const N = arr.length;
const steps = 10;
printWeightsOnScale(arr, N, steps);
|
Output:
2 3 2 3 5 6 5 3 2 3
Share your thoughts in the comments
Please Login to comment...