Generate an N | Python3 program for above approach ; Function to print target array ; Sort the given array ; Seeking for index of elements with minimum diff . ; Seeking for index ; To store target array ; Copying element ; Copying remaining element ; Printing target array ; Driver Code ; Given Input ; Function Call
import sys NEW_LINE def printArr ( arr , n ) : NEW_LINE INDENT arr . sort ( ) NEW_LINE minDifference = sys . maxsize NEW_LINE minIndex = - 1 NEW_LINE for i in range ( 1 , n , 1 ) : NEW_LINE INDENT if ( minDifference > abs ( arr [ i ] - arr [ i - 1 ] ) ) : NEW_LINE INDENT minDifference = abs ( arr [ i ] - arr [ i - 1 ] ) NEW_LINE minIndex = i - 1 NEW_LINE DEDENT DEDENT Arr = [ 0 for i in range ( n ) ] NEW_LINE Arr [ 0 ] = arr [ minIndex ] NEW_LINE Arr [ n - 1 ] = arr [ minIndex + 1 ] NEW_LINE pos = 1 NEW_LINE for i in range ( minIndex + 2 , n , 1 ) : NEW_LINE INDENT Arr [ pos ] = arr [ i ] NEW_LINE pos += 1 NEW_LINE DEDENT for i in range ( minIndex ) : NEW_LINE INDENT Arr [ pos ] = arr [ i ] NEW_LINE pos += 1 NEW_LINE DEDENT for i in range ( n ) : NEW_LINE INDENT print ( Arr [ i ] , end = " ▁ " ) NEW_LINE DEDENT DEDENT if __name__ == ' _ _ main _ _ ' : NEW_LINE INDENT N = 8 NEW_LINE arr = [ 4 , 6 , 2 , 6 , 8 , 2 , 6 , 4 ] NEW_LINE printArr ( arr , N ) NEW_LINE DEDENT