Skip to content
Related Articles

Related Articles

Minimum pairs required to be removed such that the array does not contain any pair with sum K
  • Difficulty Level : Easy
  • Last Updated : 01 Feb, 2021

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 equal to K.

Examples:

Input: arr[] = { 3, 1, 3, 4, 3 }, K = 6 
Output:
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 equal to K(=6), the required output is 1.

Input: arr = { 1, 2, 3, 4 }, K = 5 
Output:
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 equal to K(=5), the required output is 2.

Approach:The problem can be solved using 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

filter_none

edit
close

play_arrow

link
brightness_4
code

// C++14 program to implement
// the above approach
#include <bits/stdc++.h>
using namespace std;
 
// Function to find the maximum count of pairs
// required to be removed such that no pairs
// exist whose sum equal to K
int maxcntPairsSumKRemoved(vector<int> arr, int k)
{
     
    // Stores maximum count of pairs required
    // to be removed such that no pairs
    // exist whose sum equal to K
    int cntPairs = 0;
 
    // Base Case
    if (arr.size() <= 1)
        return cntPairs;
 
    // Sort the array
    sort(arr.begin(), arr.end());
 
    // Stores index of
    // left pointer
    int left = 0;
 
    // Stores index of
    // right pointer
    int right = arr.size() - 1;
 
    while (left < right)
    {
         
        // Stores sum of left
        // and right pointer
        int s = arr[left] + arr[right];
 
        // If s equal to k
        if (s == k)
        {
             
            // Update cntPairs
            cntPairs += 1;
 
            // Update left
            left += 1;
 
            // Update right
            right -= 1;
        }
         
        // If s > k
        else if (s > k)
         
            // Update right
            right -= 1;
        else
         
            // Update left
            left += 1;
    }
     
    // Return the cntPairs
    return cntPairs;
}
 
// Driver Code
int main()
{
    vector<int> arr = { 1, 2, 3, 4 };
    int K = 5;
     
    // Function call
    cout << (maxcntPairsSumKRemoved(arr, K));
     
    return 0;
}
 
// This code is contributed by mohit kumar 29

chevron_right


Java

filter_none

edit
close

play_arrow

link
brightness_4
code

// Java program for the above approach 
import java.util.*;
 
class GFG{
         
// Function to find the maximum count of pairs
// required to be removed such that no pairs
// exist whose sum equal to K
static int maxcntPairsSumKRemoved(int[] arr, int k)
{
     
    // Stores maximum count of pairs required
    // to be removed such that no pairs
    // exist whose sum equal to K
    int cntPairs = 0;
 
    // Base Case
    if (arr.length <= 1)
        return cntPairs;
 
    // Sort the array
    Arrays.sort(arr);
 
    // Stores index of
    // left pointer
    int left = 0;
 
    // Stores index of
    // right pointer
    int right = arr.length - 1;
 
    while (left < right)
    {
         
        // Stores sum of left
        // and right pointer
        int s = arr[left] + arr[right];
 
        // If s equal to k
        if (s == k)
        {
             
            // Update cntPairs
            cntPairs += 1;
 
            // Update left
            left += 1;
 
            // Update right
            right -= 1;
        }
         
        // If s > k
        else if (s > k)
         
            // Update right
            right -= 1;
        else
         
            // Update left
            left += 1;
    }
     
    // Return the cntPairs
    return cntPairs;
}  
 
// Driver Code   
public static void main (String[] args)   
{   
    int[] arr = { 1, 2, 3, 4 };
    int K = 5;
     
    // Function call
    System.out.println (maxcntPairsSumKRemoved(arr, K));  
}
}

chevron_right


Python3

filter_none

edit
close

play_arrow

link
brightness_4
code

# Python3 program to implement
# the above approach
 
# Function to find the maximum count of pairs
# required to be removed such that no pairs
# exist whose sum equal to K
def maxcntPairsSumKRemoved(arr, k):
 
    # Stores maximum count of pairs required
    # to be removed such that no pairs
    # exist whose sum equal to K
    cntPairs = 0
 
    # Base Case
    if not arr or len(arr) == 1:
        return cntPairs
 
    # Sort the array   
    arr.sort()
 
    # Stores index of
    # left pointer
    left = 0
 
    # Stores index of
    # right pointer
    right = len(arr) - 1
 
    while left < right:
 
        # Stores sum of left
        # and right pointer
        s = arr[left] + arr[right]
 
        # If s equal to k
        if s == k:
 
            # Update cntPairs
            cntPairs += 1
 
            # Update left
            left += 1
 
            # Update right
            right-= 1
             
        # If s > k
        elif s > k:
 
            # Update right
            right-= 1
        else:
 
            # Update left
            left+= 1
     
    # Return the cntPairs
    return cntPairs
 
# Driver Code
if __name__ == "__main__":
    arr =[1, 2, 3, 4]
    K = 5
 
    # Function call
    print(maxcntPairsSumKRemoved(arr, K))

chevron_right


C#

filter_none

edit
close

play_arrow

link
brightness_4
code

// C# program for the above approach 
using System;
class GFG{
         
// Function to find the maximum count of pairs
// required to be removed such that no pairs
// exist whose sum equal to K
static int maxcntPairsSumKRemoved(int[] arr, int k)
{
     
    // Stores maximum count of pairs required
    // to be removed such that no pairs
    // exist whose sum equal to K
    int cntPairs = 0;
 
    // Base Case
    if (arr.Length <= 1)
        return cntPairs;
 
    // Sort the array
    Array.Sort(arr);
 
    // Stores index of
    // left pointer
    int left = 0;
 
    // Stores index of
    // right pointer
    int right = arr.Length - 1;
 
    while (left < right)
    {
         
        // Stores sum of left
        // and right pointer
        int s = arr[left] + arr[right];
 
        // If s equal to k
        if (s == k)
        {
             
            // Update cntPairs
            cntPairs += 1;
 
            // Update left
            left += 1;
 
            // Update right
            right -= 1;
        }
         
        // If s > k
        else if (s > k)
         
            // Update right
            right -= 1;
        else
         
            // Update left
            left += 1;
    }
     
    // Return the cntPairs
    return cntPairs;
}  
 
// Driver Code   
public static void Main(String[] args)   
{   
    int[] arr = { 1, 2, 3, 4 };
    int K = 5;
     
    // Function call
    Console.WriteLine (maxcntPairsSumKRemoved(arr, K));  
}
}
 
// This code is contributed by 29AjayKumar

chevron_right


Output: 

2

 

Time Complexity: O(N)
Auxiliary Space: O(1)

Attention reader! Don’t stop learning now. Get hold of all the important DSA concepts with the DSA Self Paced Course at a student-friendly price and become industry ready.

My Personal Notes arrow_drop_up
Recommended Articles
Page :