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