Given an array arr[] consisting of N non-negative distinct integers and an integer K, the task is to distribute the array in two arrays such that both the arrays does not contain a pair with sum K.
Examples:
Input: arr[] = {1, 0, 2, 3, 4, 7, 8, 9}, K = 4
Output:
3, 2, 4, 7, 8, 9
0, 1
Explanation: Pairs (1, 3) and (0, 4) from the given array cannot be placed in the same array. Therefore, 0, 1 can be placed in an array and 3, 4 can be placed in the other array. The remaining array elements can be placed in any of the two arrays.
Input: arr[] = {0, 1, 2, 4, 5, 6, 7, 8, 9}, K = 7
Output:
0, 1, 2, 4
5, 6, 7, 9, 8
Approach: The idea is to traverse the array and place the array elements greater than K / 2 in one array and the remaining elements in the other array. Follow the steps below to solve the problem:
- Initialize two separate vectors first and second to store the two distributed arrays.
- Since all the array elements are distinct, elements greater than K/2 can be stored in one array and the remaining elements in the other.
- Traverse the given array and for each element, check if arr[i] is greater than K/2 or not. If found to be true, insert that element into vector second. Otherwise, insert it into vector first.
- After complete traversal of the array, print elements in both the vectors.
Below is the implementation of the above approach:
C++
#include <bits/stdc++.h>
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 << "\n" ;
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;
}
|
Java
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);
}
}
|
Python3
def splitArray(a, n, k):
first = []
second = []
for i in range (n):
if (a[i] < = k / / 2 ):
first.append(a[i])
else :
second.append(a[i])
for i in range ( len (first)):
print (first[i], end = " " )
print ( "\n" , end = "")
for i in range ( len (second)):
print (second[i], end = " " )
if __name__ = = '__main__' :
k = 5
a = [ 0 , 1 , 3 , 2 , 4 , 5 ,
6 , 7 , 8 , 9 , 10 ]
n = len (a)
splitArray(a, n, k)
|
C#
using System;
using System.Collections.Generic;
class GFG{
static void splitArray( int [] a, int n,
int k)
{
List< int > first = new List< int >();
List< int > second = new List< int >();
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.Count; i++)
{
Console.Write(first[i] + " " );
}
Console.WriteLine();
for ( int i = 0; i < second.Count; i++)
{
Console.Write(second[i] + " " );
}
}
public static void Main()
{
int k = 5;
int [] a = { 0, 1, 3, 2, 4, 5,
6, 7, 8, 9, 10 };
int n = a.Length;
splitArray(a, n, k);
}
}
|
Javascript
<script>
function splitArray(a, n, k)
{
let first = [];
let second = [];
for (let i = 0; i < n; i++) {
if (a[i] <= parseInt(k / 2))
first.push(a[i]);
else
second.push(a[i]);
}
for (let i = 0; i < first.length;
i++) {
document.write(first[i] + " " );
}
document.write( "<br>" );
for (let i = 0; i < second.length;
i++) {
document.write(second[i] + " " );
}
}
let k = 5;
let a = [ 0, 1, 3, 2, 4, 5,
6, 7, 8, 9, 10 ];
let n = a.length;
splitArray(a, n, k);
</script>
|
Output:
0 1 2
3 4 5 6 7 8 9 10
Time Complexity: O(N), where N is the size of the given array.
Auxiliary Space: O(1)
Feeling lost in the world of random DSA topics, wasting time without progress? It's time for a change! Join our DSA course, where we'll guide you on an exciting journey to master DSA efficiently and on schedule.
Ready to dive in? Explore our Free Demo Content and join our DSA course, trusted by over 100,000 geeks!
Last Updated :
08 Mar, 2023
Like Article
Save Article