Split array into two subsequences having minimum count of pairs with sum equal to X
Last Updated :
01 May, 2021
Given an array arr[] consisting of N integers and an integer X, the task is to split the array into two subsequences such that the number of pairs having a sum equal to X is minimum in both the arrays.
Examples:
Input: arr[] = {1, 2, 3, 4, 5, 6}, X = 7
Output:
The First Array is – 1 2 3
The Second Array is – 4 5 6
Explanation:
The possible 3 pairs in the first array are {(1, 2), (2, 3), (1, 3)}. None of these pairs have sum equal to X (= 7).
The possible 3 pairs in the second array are {(4, 5), (5, 6), (4, 6)}. None of these pairs have sum equal to X (= 7).
Input: arr[] = {3, 3, 3}, X = 6
Output:
The First Array is – 3
The Second Array is – 3 3
Approach: Follow the steps below to solve the problem:
- Create two arrays to store the two splitted subsequences.
- Traverse the array and perform the following operations:
- If arr[i] * 2 < X: Insert it into the first array.
- Since the first array contains all numbers less than X / 2 currently, thus no pair has a sum equal to X in the array currently.
- If arr[i] * 2 > X: Insert it into the second array.
- Since the second array contains all numbers greater than X / 2 currently, thus no pair has a sum equal to X in the array currently.
- If arr[i] * 2 < X: Insert alternatively the elements into the first and second array respectively to get the minimum pairs.
- Finally, after complete traversal of the array, print both the arrays.
Below is the implementation of the above approach:
C++
#include <bits/stdc++.h>
using namespace std;
void solve( int arr[], int N, int X)
{
vector< int > A, B;
int c = 0;
for ( int i = 0; i < N; i++) {
if ((2 * arr[i]) < X) {
A.push_back(arr[i]);
}
else if ((2 * arr[i]) > X) {
B.push_back(arr[i]);
}
else {
if (c % 2 == 0) {
A.push_back(arr[i]);
}
else {
B.push_back(arr[i]);
}
c++;
}
}
cout << "The First Array is - " ;
for ( int i = 0; i < A.size(); i++) {
cout << A[i] << " " ;
}
cout << endl;
cout << "The Second Array is - " ;
for ( int i = 0; i < B.size(); i++) {
cout << B[i] << " " ;
}
}
int main()
{
int arr[] = { 1, 5, 4, 3,
6, 2, 4, 3 };
int X = 7;
int N = sizeof (arr)
/ sizeof (arr[0]);
solve(arr, N, X);
}
|
Java
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);
}
}
|
Python3
def solve(arr, N, X):
A = []
B = []
c = 0
for i in range (N):
if (( 2 * arr[i]) < X):
A.append(arr[i])
elif (( 2 * arr[i]) > X):
B.append(arr[i])
else :
if (c % 2 = = 0 ):
A.append(arr[i])
else :
B.append(arr[i])
c + = 1
print ( "The First Array is - " , end = " " )
for i in range ( len (A)):
print (A[i], end = " " )
print ()
print ( "The Second Array is - " , end = " " )
for i in range ( len (B)):
print (B[i], end = " " )
if __name__ = = '__main__' :
arr = [ 1 , 5 , 4 , 3 , 6 , 2 , 4 , 3 ]
X = 7
N = len (arr)
solve(arr, N, X)
|
C#
using System;
using System.Collections.Generic;
class GFG{
static void solve( int []arr,
int N, int X)
{
List< int > A =
new List< int >(),
B = new List< int >();
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++;
}
}
Console.Write( "The First Array is - " );
for ( int i = 0; i < A.Count; i++)
{
Console.Write(A[i] + " " );
}
Console.WriteLine();
Console.Write( "The Second Array is - " );
for ( int i = 0; i < B.Count; i++)
{
Console.Write(B[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);
}
}
|
Javascript
<script>
function solve(arr, N, X)
{
var A = [], B = [];
var c = 0;
for ( var i = 0; i < N; i++)
{
if ((2 * arr[i]) < X)
{
A.push(arr[i]);
}
else if ((2 * arr[i]) > X)
{
B.push(arr[i]);
}
else
{
if (c % 2 == 0)
{
A.push(arr[i]);
}
else
{
B.push(arr[i]);
}
c++;
}
}
document.write( "The First Array is - " );
for ( var i = 0; i < A.length; i++)
{
document.write( A[i] + " " );
}
document.write( "<br>" );
document.write( "The Second Array is - " );
for ( var i = 0; i < B.length; i++)
{
document.write( B[i] + " " );
}
}
var arr = [ 1, 5, 4, 3, 6, 2, 4, 3 ];
var X = 7;
var N = arr.length;
solve(arr, N, X);
</script>
|
Output
The First Array is - 1 3 2 3
The Second Array is - 5 4 6 4
Time Complexity: O(N)
Auxiliary Space: O(N)
Like Article
Suggest improvement
Share your thoughts in the comments
Please Login to comment...