Minimum pairs required to be removed such that the array does not contain any pair with sum K
Last Updated :
07 Oct, 2022
Given an array arr[] of size N and an integer K, the task is to find the minimum count of pairs required to be removed such that no pair exists in the array whose sum of elements is equal to K.
Examples:
Input: arr[] = { 3, 1, 3, 4, 3 }, K = 6
Output: 1
Explanation:
Removing the pair (arr[0], arr[2]) modifies arr[] to arr[] = { 1, 4, 3 }
Since no pair exists in arr[] whose sum of elements is equal to K(=6), the required output is 1.
Input: arr = { 1, 2, 3, 4 }, K = 5
Output: 2
Explanation:
Removing the pair (arr[0], arr[3]) modifies arr[] to arr[] = { 2, 3 }
Removing the pair (arr[0], arr[1]) modifies arr[] to arr[] = { }
Since no pair exists in arr[] whose sum of elements is equal to K(=5), the required output is 2.
Approach: The problem can be solved using the two-pointer technique. Follow the steps below to solve this problem:
- Sort the array in ascending order.
- Initialize two variables, say left = 0 and right = N – 1 to store the index of left and right pointers respectively.
- Initialize a variable, say cntPairs, to store the minimum count of pairs required to be removed such that no pair exists in the array whose sum is equal to K.
- Traverse the array and check the following conditions.
- If arr[left] + arr[right] == K, then increment the value of cntPairs by 1 and update left += 1 and right -= 1.
- If arr[left] + arr[right] < K, then update left += 1.
- If arr[left] + arr[right] < K, then update right -= 1.
- Finally, print the value of cntPairs.
Below is the implementation of the above approach:
C++14
#include <bits/stdc++.h>
using namespace std;
int maxcntPairsSumKRemoved(vector< int > arr, int k)
{
int cntPairs = 0;
if (arr.size() <= 1)
return cntPairs;
sort(arr.begin(), arr.end());
int left = 0;
int right = arr.size() - 1;
while (left < right)
{
int s = arr[left] + arr[right];
if (s == k)
{
cntPairs += 1;
left += 1;
right -= 1;
}
else if (s > k)
right -= 1;
else
left += 1;
}
return cntPairs;
}
int main()
{
vector< int > arr = { 1, 2, 3, 4 };
int K = 5;
cout << (maxcntPairsSumKRemoved(arr, K));
return 0;
}
|
Java
import java.util.*;
class GFG{
static int maxcntPairsSumKRemoved( int [] arr, int k)
{
int cntPairs = 0 ;
if (arr.length <= 1 )
return cntPairs;
Arrays.sort(arr);
int left = 0 ;
int right = arr.length - 1 ;
while (left < right)
{
int s = arr[left] + arr[right];
if (s == k)
{
cntPairs += 1 ;
left += 1 ;
right -= 1 ;
}
else if (s > k)
right -= 1 ;
else
left += 1 ;
}
return cntPairs;
}
public static void main (String[] args)
{
int [] arr = { 1 , 2 , 3 , 4 };
int K = 5 ;
System.out.println (maxcntPairsSumKRemoved(arr, K));
}
}
|
Python3
def maxcntPairsSumKRemoved(arr, k):
cntPairs = 0
if not arr or len (arr) = = 1 :
return cntPairs
arr.sort()
left = 0
right = len (arr) - 1
while left < right:
s = arr[left] + arr[right]
if s = = k:
cntPairs + = 1
left + = 1
right - = 1
elif s > k:
right - = 1
else :
left + = 1
return cntPairs
if __name__ = = "__main__" :
arr = [ 1 , 2 , 3 , 4 ]
K = 5
print (maxcntPairsSumKRemoved(arr, K))
|
C#
using System;
class GFG{
static int maxcntPairsSumKRemoved( int [] arr, int k)
{
int cntPairs = 0;
if (arr.Length <= 1)
return cntPairs;
Array.Sort(arr);
int left = 0;
int right = arr.Length - 1;
while (left < right)
{
int s = arr[left] + arr[right];
if (s == k)
{
cntPairs += 1;
left += 1;
right -= 1;
}
else if (s > k)
right -= 1;
else
left += 1;
}
return cntPairs;
}
public static void Main(String[] args)
{
int [] arr = { 1, 2, 3, 4 };
int K = 5;
Console.WriteLine (maxcntPairsSumKRemoved(arr, K));
}
}
|
Javascript
<script>
function maxcntPairsSumKRemoved( arr, k)
{
var cntPairs = 0;
if (arr.length <= 1)
return cntPairs;
arr.sort();
var left = 0;
var right = arr.length - 1;
while (left < right)
{
var s = arr[left] + arr[right];
if (s == k)
{
cntPairs += 1;
left += 1;
right -= 1;
}
else if (s > k)
right -= 1;
else
left += 1;
}
return cntPairs;
}
var arr = [ 1,2,3,4];
var K = 5;
document.write(maxcntPairsSumKRemoved(arr, K));
</script>
|
Time Complexity: O(N*logN) as it sorts the given array
Auxiliary Space: O(1)
Like Article
Suggest improvement
Share your thoughts in the comments
Please Login to comment...