Minimize steps required to make all array elements same by adding 1 , 2 or 5 | Function to calculate the minimum number of steps ; count stores number of operations required to make all elements equal to minimum value ; Remark , the array should remain unchanged for further calculations with different minimum ; Storing the current value of arr [ i ] in val ; Finds how much extra amount is to be removed ; Subtract the maximum number of 5 and stores remaining ; Subtract the maximum number of 2 and stores remaining ; Restores the actual value of arr [ i ] ; Return the count ; Function to find the minimum number of steps to make array elements same ; Sort the array in descending order ; Stores the minimum array element ; Stores the operations required to make array elements equal to minimum ; Stores the operations required to make array elements equal to minimum - 1 ; Stores the operations required to make array elements equal to minimum - 2 ; Return minimum of the three counts ; Driver Code
def calculate_steps ( arr , n , minimum ) : NEW_LINE INDENT count = 0 NEW_LINE for i in range ( n ) : NEW_LINE INDENT val = arr [ i ] NEW_LINE if ( arr [ i ] > minimum ) : NEW_LINE INDENT arr [ i ] = arr [ i ] - minimum NEW_LINE count += arr [ i ] // 5 NEW_LINE arr [ i ] = arr [ i ] % 5 NEW_LINE count += arr [ i ] // 2 NEW_LINE arr [ i ] = arr [ i ] % 2 NEW_LINE if ( arr [ i ] ) : NEW_LINE INDENT count += 1 NEW_LINE DEDENT DEDENT arr [ i ] = val NEW_LINE DEDENT return count NEW_LINE DEDENT def solve ( arr , n ) : NEW_LINE INDENT arr = sorted ( arr ) NEW_LINE arr = arr [ : : - 1 ] NEW_LINE minimum = arr [ n - 1 ] NEW_LINE count1 = 0 NEW_LINE count2 = 0 NEW_LINE count3 = 0 NEW_LINE count1 = calculate_steps ( arr , n , minimum ) NEW_LINE count2 = calculate_steps ( arr , n , minimum - 1 ) NEW_LINE count3 = calculate_steps ( arr , n , minimum - 2 ) NEW_LINE return min ( count1 , min ( count2 , count3 ) ) NEW_LINE DEDENT if __name__ == ' _ _ main _ _ ' : NEW_LINE INDENT arr = [ 3 , 6 , 6 ] NEW_LINE N = len ( arr ) NEW_LINE print ( solve ( arr , N ) ) NEW_LINE DEDENT