Divide array into two arrays which does not contain any pair with sum K | Java program for the above approach ; Function to split the given array into two separate arrays satisfying given condition ; Stores resultant arrays ; Traverse the array ; If a [ i ] is smaller than or equal to k / 2 ; Print first array ; Print second array ; Driver Code ; Given K ; Given array ; Given size
import java . util . * ; class GFG { static void splitArray ( int a [ ] , int n , int k ) { Vector < Integer > first = new Vector < > ( ) ; Vector < Integer > second = new Vector < > ( ) ; for ( int i = 0 ; i < n ; i ++ ) { if ( a [ i ] <= k / 2 ) first . add ( a [ i ] ) ; else second . add ( a [ i ] ) ; } for ( int i = 0 ; i < first . size ( ) ; i ++ ) { System . out . print ( first . get ( i ) + " ▁ " ) ; } System . out . println ( ) ; for ( int i = 0 ; i < second . size ( ) ; i ++ ) { System . out . print ( second . get ( i ) + " ▁ " ) ; } } public static void main ( String [ ] args ) { int k = 5 ; int a [ ] = { 0 , 1 , 3 , 2 , 4 , 5 , 6 , 7 , 8 , 9 , 10 } ; int n = a . length ; splitArray ( a , n , k ) ; } }