Arrange numbers to form a valid sequence | Function to organize the given numbers to form a valid sequence . ; Sorting the array ; Two pointer technique to organize the numbers ; Driver code
function orgazineInOrder ( vec , op , n ) { let result = [ n ] ; vec . sort ( ) ; let i = 0 , j = n - 1 , k = 0 ; while ( i <= j && k <= n - 2 ) { if ( op [ k ] == ' ' ) { result [ k ] = vec [ i ++ ] ; } else { result [ k ] = vec [ j -- ] ; } k ++ ; } result [ n - 1 ] = vec [ i ] ; return result ; } let vec = [ 8 , 2 , 7 , 1 , 5 , 9 ] ; let op = [ ' ' , ' ' , ' ' , ' ' , ' ' ] ; let result = orgazineInOrder ( vec , op , vec . length ) ; for ( let i = 0 ; i < result . length ; i ++ ) { document . write ( result [ i ] + " " ) ; }