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
def orgazineInOrder ( vec , op , n ) : NEW_LINE INDENT result = [ 0 ] * n ; NEW_LINE vec . sort ( ) ; NEW_LINE i = 0 ; NEW_LINE j = n - 1 ; NEW_LINE k = 0 ; NEW_LINE while ( i <= j and k <= n - 2 ) : NEW_LINE INDENT if ( op [ k ] == ' < ' ) : NEW_LINE INDENT result [ k ] = vec [ i ] ; NEW_LINE i += 1 ; NEW_LINE DEDENT else : NEW_LINE INDENT result [ k ] = vec [ j ] ; NEW_LINE j -= 1 ; NEW_LINE DEDENT k += 1 ; NEW_LINE DEDENT result [ n - 1 ] = vec [ i ] ; NEW_LINE return result ; NEW_LINE DEDENT if __name__ == " _ _ main _ _ " : NEW_LINE INDENT vec = [ 8 , 2 , 7 , 1 , 5 , 9 ] ; NEW_LINE op = [ ' > ' , ' > ' , ' < ' , ' > ' , ' < ' ] ; NEW_LINE result = orgazineInOrder ( vec , op , len ( vec ) ) ; NEW_LINE for i in range ( len ( result ) ) : NEW_LINE INDENT print ( result [ i ] , end = " ▁ " ) ; NEW_LINE DEDENT DEDENT