Divide array into two arrays which does not contain any pair with sum K | C ++ 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
#include <bits/stdc++.h> NEW_LINE using namespace std ; void splitArray ( int a [ ] , int n , int k ) { vector < int > first , second ; for ( int i = 0 ; i < n ; i ++ ) { if ( a [ i ] <= k / 2 ) first . push_back ( a [ i ] ) ; else second . push_back ( a [ i ] ) ; } for ( int i = 0 ; i < first . size ( ) ; i ++ ) { cout << first [ i ] << " ▁ " ; } cout << " STRNEWLINE " ; for ( int i = 0 ; i < second . size ( ) ; i ++ ) { cout << second [ i ] << " ▁ " ; } } int main ( ) { int k = 5 ; int a [ ] = { 0 , 1 , 3 , 2 , 4 , 5 , 6 , 7 , 8 , 9 , 10 } ; int n = sizeof ( a ) / sizeof ( int ) ; splitArray ( a , n , k ) ; return 0 ; }