Arrange N elements in circular fashion such that all elements are strictly less than sum of adjacent elements | Function to prvar 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
function printArrangement ( a , n ) { a . sort ( ) ; var b = Array ( n ) . fill ( 0 ) ; var low = 0 , high = n - 1 ; for ( i = 0 ; i < n ; i ++ ) { if ( i % 2 == 0 ) b [ low ++ ] = a [ i ] ; else b [ high -- ] = a [ i ] ; } for ( i = 0 ; i < n ; i ++ ) { if ( i == 0 ) { if ( b [ n - 1 ] + b [ 1 ] <= b [ i ] ) { document . write ( - 1 ) ; return ; } } else if ( i == ( n - 1 ) ) { if ( b [ n - 2 ] + b [ 0 ] <= b [ i ] ) { document . write ( - 1 ) ; return ; } } else { if ( b [ i - 1 ] + b [ i + 1 ] <= b [ i ] ) { document . write ( - 1 ) ; return ; } } } for ( i = 0 ; i < n ; i ++ ) document . write ( b [ i ] + " " ) ; } var a = [ 1 , 4 , 4 , 3 , 2 ] ; var n = a . length ; printArrangement ( a , n ) ;