Split array into two subsequences having minimum count of pairs with sum equal to X | Java 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
import java . util . * ; class GFG { static void solve ( int arr [ ] , int N , int X ) { Vector < Integer > A = new Vector < Integer > ( ) , B = new Vector < Integer > ( ) ; 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 ++ ; } } System . out . print ( " The ▁ First ▁ Array ▁ is ▁ - ▁ " ) ; for ( int i = 0 ; i < A . size ( ) ; i ++ ) { System . out . print ( A . get ( i ) + " ▁ " ) ; } System . out . println ( ) ; System . out . print ( " The ▁ Second ▁ Array ▁ is ▁ - ▁ " ) ; for ( int i = 0 ; i < B . size ( ) ; i ++ ) { System . out . print ( B . get ( 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 ) ; } }