Split array into two subsequences having minimum count of pairs with sum equal to X | C # program for the above approach ; 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
using System ; using System . Collections . Generic ; class GFG { static void solve ( int [ ] arr , int N , int X ) { List < int > A = new List < int > ( ) , B = new List < int > ( ) ; int c = 0 ; for ( int i = 0 ; i < N ; i ++ ) { if ( ( 2 * arr [ i ] ) < X ) { A . Add ( arr [ i ] ) ; } else if ( ( 2 * arr [ i ] ) > X ) { B . Add ( arr [ i ] ) ; } else { if ( c % 2 == 0 ) { A . Add ( arr [ i ] ) ; } else { B . Add ( arr [ i ] ) ; } c ++ ; } } Console . Write ( " The ▁ First ▁ Array ▁ is ▁ - ▁ " ) ; for ( int i = 0 ; i < A . Count ; i ++ ) { Console . Write ( A [ i ] + " ▁ " ) ; } Console . WriteLine ( ) ; Console . Write ( " The ▁ Second ▁ Array ▁ is ▁ - ▁ " ) ; for ( int i = 0 ; i < B . Count ; i ++ ) { Console . Write ( B [ i ] + " ▁ " ) ; } } public static void Main ( String [ ] args ) { int [ ] arr = { 1 , 5 , 4 , 3 , 6 , 2 , 4 , 3 } ; int X = 7 ; int N = arr . Length ; solve ( arr , N , X ) ; } }