Divide array into two arrays which does not contain any pair with sum K
Last Updated :
08 Mar, 2023
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++
// C++ program for the above approach
#include <bits/stdc++.h>
using namespace std;
// Function to split the given
// array into two separate arrays
// satisfying given condition
void splitArray(int a[], int n,
int k)
{
// Stores resultant arrays
vector<int> first, second;
// Traverse the array
for (int i = 0; i < n; i++) {
// If a[i] is smaller than
// or equal to k/2
if (a[i] <= k / 2)
first.push_back(a[i]);
else
second.push_back(a[i]);
}
// Print first array
for (int i = 0; i < first.size();
i++) {
cout << first[i] << " ";
}
// Print second array
cout << "\n";
for (int i = 0; i < second.size();
i++) {
cout << second[i] << " ";
}
}
// Driver Code
int main()
{
// Given K
int k = 5;
// Given array
int a[] = { 0, 1, 3, 2, 4, 5,
6, 7, 8, 9, 10 };
// Given size
int n = sizeof(a)
/ sizeof(int);
splitArray(a, n, k);
return 0;
}
Java
// Java program for the above approach
import java.util.*;
class GFG{
// Function to split the given
// array into two separate arrays
// satisfying given condition
static void splitArray(int a[], int n,
int k)
{
// Stores resultant arrays
Vector<Integer> first = new Vector<>();
Vector<Integer> second = new Vector<>();
// Traverse the array
for(int i = 0; i < n; i++)
{
// If a[i] is smaller than
// or equal to k/2
if (a[i] <= k / 2)
first.add(a[i]);
else
second.add(a[i]);
}
// Print first array
for(int i = 0; i < first.size(); i++)
{
System.out.print(first.get(i) + " ");
}
// Print second array
System.out.println();
for(int i = 0; i < second.size(); i++)
{
System.out.print(second.get(i) + " ");
}
}
// Driver Code
public static void main(String[] args)
{
// Given K
int k = 5;
// Given array
int a[] = { 0, 1, 3, 2, 4, 5,
6, 7, 8, 9, 10 };
int n = a.length;
splitArray(a, n, k);
}
}
// This code is contributed by code_hunt
Python3
# Python3 program for the above approach
# Function to split the given
# array into two separate arrays
# satisfying given condition
def splitArray(a, n, k):
# Stores resultant arrays
first = []
second = []
# Traverse the array
for i in range(n):
# If a[i] is smaller than
# or equal to k/2
if (a[i] <= k // 2):
first.append(a[i])
else:
second.append(a[i])
# Print first array
for i in range(len(first)):
print(first[i], end = " ")
# Print second array
print("\n", end = "")
for i in range(len(second)):
print(second[i], end = " ")
# Driver Code
if __name__ == '__main__':
# Given K
k = 5
# Given array
a = [ 0, 1, 3, 2, 4, 5,
6, 7, 8, 9, 10 ]
n = len(a)
splitArray(a, n, k)
# This code is contributed by bgangwar59
C#
// C# program for the above approach
using System;
using System.Collections.Generic;
class GFG{
// Function to split the given
// array into two separate arrays
// satisfying given condition
static void splitArray(int[] a, int n,
int k)
{
// Stores resultant arrays
List<int> first = new List<int>();
List<int> second = new List<int>();
// Traverse the array
for(int i = 0; i < n; i++)
{
// If a[i] is smaller than
// or equal to k/2
if (a[i] <= k / 2)
first.Add(a[i]);
else
second.Add(a[i]);
}
// Print first array
for(int i = 0; i < first.Count; i++)
{
Console.Write(first[i] + " ");
}
// Print second array
Console.WriteLine();
for(int i = 0; i < second.Count; i++)
{
Console.Write(second[i] + " ");
}
}
// Driver Code
public static void Main()
{
// Given K
int k = 5;
// Given array
int[] a = { 0, 1, 3, 2, 4, 5,
6, 7, 8, 9, 10 };
int n = a.Length;
splitArray(a, n, k);
}
}
// This code is contributed by susmitakundugoaldanga
JavaScript
<script>
// Javascript program for the above approach
// Function to split the given
// array into two separate arrays
// satisfying given condition
function splitArray(a, n, k)
{
// Stores resultant arrays
let first = [];
let second = [];
// Traverse the array
for (let i = 0; i < n; i++) {
// If a[i] is smaller than
// or equal to k/2
if (a[i] <= parseInt(k / 2))
first.push(a[i]);
else
second.push(a[i]);
}
// Print first array
for (let i = 0; i < first.length;
i++) {
document.write(first[i] + " ");
}
// Print second array
document.write("<br>");
for (let i = 0; i < second.length;
i++) {
document.write(second[i] + " ");
}
}
// Driver Code
// Given K
let k = 5;
// Given array
let a = [ 0, 1, 3, 2, 4, 5,
6, 7, 8, 9, 10 ];
// Given size
let n = a.length;
splitArray(a, n, k);
// This code is contributed by rishavmahato348.
</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)
Similar Reads
Count number of pairs not divisible by any element in the array Given an array arr[] of size N, the task is to count the number of pairs of integers (i, j) for which there does not exist an integer k such that arr[i] is divisible by arr[k] and arr[j] is divisible by arr[k], such that k can be any index between [0, N - 1]. Examples: Input: N = 4, arr[] = {2, 4, 5
5 min read
Divide an array into k subarrays with minimum cost II Given an array of integers arr[] of length n and two positive integers kk and len. The cost of an array is the value of its first element. For example, the cost of [2,3,4] is 2 and the cost of [4,1,2] is 4. You have to divide arr[] into k disjoint contiguous subarrays such that the difference betwee
11 min read
Count of pairs of Array elements which are divisible by K when concatenated Given an array arr[] and an integer K, the task is to count the pair of indices (i, j) such that i !=j and concatenation of a[i] and a[j] is divisible by K. Example: Input: arr[] = [4, 5, 2], K = 2 Output: 4 Explanation: All possible concatenations are {45, 42, 54, 52, 24, 25}. Out of these, the num
8 min read
Count pairs in array whose sum is divisible by K Given an array A[] and positive integer K, the task is to count the total number of pairs in the array whose sum is divisible by K. Note: This question is a generalized version of this Examples: Input : A[] = {2, 2, 1, 7, 5, 3}, K = 4 Output : 5 Explanation : There are five pairs possible whose sum
10 min read
Count pairs in array whose sum is divisible by K Given an array A[] and positive integer K, the task is to count the total number of pairs in the array whose sum is divisible by K. Note: This question is a generalized version of this Examples: Input : A[] = {2, 2, 1, 7, 5, 3}, K = 4 Output : 5 Explanation : There are five pairs possible whose sum
10 min read
Count pairs from two arrays with difference exceeding K Given two integer arrays arr[] and brr[] consisting of distinct elements of size N and M respectively and an integer K, the task is to find the count of pairs(arr[i], brr[j]) such that (brr[j] - arr[i]) > K. Examples: Input: arr[] = {5, 9, 1, 8}, brr[] {10, 12, 7, 4, 2, 3}, K = 3 Output: 6 Explan
9 min read