Arrange numbers to form a valid sequence | C ++ implementation of the above approach ; Function to organize the given numbers to form a valid sequence . ; Sorting the array ; Two pointer technique to organize the numbers ; Driver code
#include <bits/stdc++.h> NEW_LINE using namespace std ; vector < int > orgazineInOrder ( vector < int > vec , vector < int > op , int n ) { vector < int > result ( n ) ; sort ( vec . begin ( ) , vec . end ( ) ) ; int 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 ; } int main ( ) { vector < int > vec ( { 8 , 2 , 7 , 1 , 5 , 9 } ) ; vector < int > op ( { ' > ' , ' > ' , ' < ' , ' > ' , ' < ' } ) ; vector < int > result = orgazineInOrder ( vec , op , vec . size ( ) ) ; for ( int i = 0 ; i < result . size ( ) ; i ++ ) { cout << result [ i ] << " ▁ " ; } return 0 ; }