Split array into two subsequences having minimum count of pairs with sum equal to X | Function to split the array into two subsequences ; Stores the two subsequences ; Flag to set / reset to split arrays elements alternately into two arrays ; Traverse the given array ; If 2 * arr [ i ] is less than X ; Push element into the first array ; If 2 * arr [ i ] is greater than X ; Push element into the second array ; If 2 * arr [ i ] is equal to X ; Alternatively place the elements into the two arrays ; Print both the arrays ; Driver Code ; Size of the array ; Function Call
function solve ( arr , N , X ) { var A = [ ] , B = [ ] ; var c = 0 ; for ( var i = 0 ; i < N ; i ++ ) { if ( ( 2 * arr [ i ] ) < X ) { A . push ( arr [ i ] ) ; } else if ( ( 2 * arr [ i ] ) > X ) { B . push ( arr [ i ] ) ; } else { if ( c % 2 == 0 ) { A . push ( arr [ i ] ) ; } else { B . push ( arr [ i ] ) ; } c ++ ; } } document . write ( " " ) ; for ( var i = 0 ; i < A . length ; i ++ ) { document . write ( A [ i ] + " " ) ; } document . write ( " " ) ; document . write ( " " ) ; for ( var i = 0 ; i < B . length ; i ++ ) { document . write ( B [ i ] + " " ) ; } } var arr = [ 1 , 5 , 4 , 3 , 6 , 2 , 4 , 3 ] ; var X = 7 ; var N = arr . length ; solve ( arr , N , X ) ;