Replace every element with the smallest of the others | Python3 code for the above approach . ; There should be atleast two elements ; If current element is smaller than firstSmallest then update both firstSmallest and secondSmallest ; If arr [ i ] is in between firstSmallest and secondSmallest then update secondSmallest ; Replace every element by smallest of all other elements ; Print the modified array . ; Driver code
def ReplaceElements ( arr , n ) : NEW_LINE INDENT if ( n < 2 ) : NEW_LINE INDENT print ( " Invalid ▁ Input " ) NEW_LINE return NEW_LINE DEDENT firstSmallest = 10 ** 18 NEW_LINE secondSmallest = 10 ** 18 NEW_LINE for i in range ( n ) : NEW_LINE INDENT if ( arr [ i ] < firstSmallest ) : NEW_LINE INDENT secondSmallest = firstSmallest NEW_LINE firstSmallest = arr [ i ] NEW_LINE DEDENT elif ( arr [ i ] < secondSmallest and arr [ i ] != firstSmallest ) : NEW_LINE INDENT secondSmallest = arr [ i ] NEW_LINE DEDENT DEDENT for i in range ( n ) : NEW_LINE INDENT if ( arr [ i ] != firstSmallest ) : NEW_LINE INDENT arr [ i ] = firstSmallest NEW_LINE DEDENT else : NEW_LINE INDENT arr [ i ] = secondSmallest NEW_LINE DEDENT DEDENT for i in arr : NEW_LINE INDENT print ( i , end = " , ▁ " ) NEW_LINE DEDENT DEDENT if __name__ == ' _ _ main _ _ ' : NEW_LINE INDENT arr = [ 4 , 2 , 1 , 3 ] NEW_LINE n = len ( arr ) NEW_LINE ReplaceElements ( arr , n ) NEW_LINE DEDENT