human
Sort ugly numbers in an array at their relative positions | Function that returns true if n is an ugly number.
While divisible by 2 , keep dividing.
While divisible by 3 , keep dividing.
While divisible by 5 , keep dividing.
n must be 1 if it was ugly.
Function to sort ugly numbers in their relative positions.
To store the ugly numbers from the array.
If current element is an ugly number.
Add it to the ArrayList and set arr [ i ] to - 1.
Sort the ugly numbers.
Position of an ugly number.
Driver code"
Solution in Python:
assistant
#Here is code to sort ugly numbers in an array at their relative positions and the other steps required.
 def isUgly ( n ) :
	while n % 2 == 0 :
		n = n // 2
	while n % 3 == 0 :
		n = n // 3
	while n % 5 == 0 :
		n = n // 5
	if n == 1 :
		return True
	return False
def sortUglyNumbers ( arr , n ) :
	list = [ ]
	for i in range ( 0 , n ) :
		if isUgly ( arr [ i ] ) :
			list . append ( arr [ i ] )
			arr [ i ] = - 1
	list . sort ( )
	j = 0
	for i in range ( 0 , n ) :
		if arr [ i ] == - 1 :
			print ( list [ j ] , end = " ▁ " )
			j += 1
		else :
			print ( arr [ i ] , end = " ▁ " )
if __name__ == " _ _ main _ _ " :
	arr = [ 1 , 2 , 3 , 7 , 12 , 10 ]
	n = len ( arr )
	sortUglyNumbers ( arr , n )