human
Modify given array by reducing each element by its next smaller element | Function to print the final array after reducing each array element by its next smaller element.
Initialize stack.
To store the corresponding element.
If stack is not empty.
If top element is smaller than the current element.
Keep popping until stack is empty or top element is greater than the current element.
If stack is not empty.
Push current element.
Final array.
Driver Code.
Given array.
Function Call"
Solution in Python:
 ... What is a code to modify given array by reducing each element by its next smaller element and the other steps required?
assistant
#Here is code to modify given array by reducing each element by its next smaller element and the other steps required.
 def printFinalPrices ( arr ) :
	minStk = [ ]
	reduce = [ 0 ] * len ( arr )
	for i in range ( len ( arr ) - 1 , - 1 , - 1 ) :
		if minStk :
			if minStk [ - 1 ] <= arr [ i ] :
				reduce [ i ] = minStk [ - 1 ]
			else :
				while minStk and minStk [ - 1 ] > arr [ i ] :
					minStk . pop ( )
				if minStk :
					reduce [ i ] = minStk [ - 1 ]
		minStk . append ( arr [ i ] )
	for i in range ( len ( arr ) ) :
		print ( arr [ i ] - reduce [ i ] , end = ' ▁ ' )
if __name__ == ' _ _ main _ _ ' :
	arr = [ 8 , 4 , 6 , 2 , 3 ]
	printFinalPrices ( arr )