Arrange N elements in circular fashion such that all elements are strictly less than sum of adjacent elements | Function to print the arrangement that satisifes the given condition ; Sort the array initially ; Array that stores the arrangement ; Once the array is sorted Re - fill the array again in the mentioned way in the approach ; Iterate in the array and check if the arrangement made satisfies the given condition or not ; For the first element the adjacents will be a [ 1 ] and a [ n - 1 ] ; For the last element the adjacents will be a [ 0 ] and a [ n - 2 ] ; If we reach this position then the arrangement is possible ; Driver code
def printArrangement ( a , n ) : NEW_LINE INDENT a = sorted ( a ) NEW_LINE b = [ 0 for i in range ( n ) ] NEW_LINE low = 0 NEW_LINE high = n - 1 NEW_LINE for i in range ( n ) : NEW_LINE INDENT if ( i % 2 == 0 ) : NEW_LINE INDENT b [ low ] = a [ i ] NEW_LINE low += 1 NEW_LINE DEDENT else : NEW_LINE INDENT b [ high ] = a [ i ] NEW_LINE high -= 1 NEW_LINE DEDENT DEDENT for i in range ( n ) : NEW_LINE INDENT if ( i == 0 ) : NEW_LINE INDENT if ( b [ n - 1 ] + b [ 1 ] <= b [ i ] ) : NEW_LINE INDENT print ( " - 1" ) NEW_LINE return NEW_LINE DEDENT DEDENT elif ( i == ( n - 1 ) ) : NEW_LINE INDENT if ( b [ n - 2 ] + b [ 0 ] <= b [ i ] ) : NEW_LINE INDENT print ( " - 1" ) NEW_LINE return NEW_LINE DEDENT DEDENT else : NEW_LINE INDENT if ( b [ i - 1 ] + b [ i + 1 ] <= b [ i ] ) : NEW_LINE INDENT print ( " - 1" ) NEW_LINE return NEW_LINE DEDENT DEDENT DEDENT for i in range ( n ) : NEW_LINE INDENT print ( b [ i ] , end = " ▁ " ) NEW_LINE DEDENT DEDENT a = [ 1 , 4 , 4 , 3 , 2 ] NEW_LINE n = len ( a ) NEW_LINE printArrangement ( a , n ) NEW_LINE