📌  相关文章
📜  需要删除的最小对,以便数组不包含任何总和为K的对

📅  最后修改于: 2021-05-17 06:30:36             🧑  作者: Mango

给定大小为N的数组arr []和整数K ,任务是找到需要删除的对的最小数量,以使在数组中元素对之和等于K的对不存在。

例子:

方法:可以使用两指针技术解决问题。请按照以下步骤解决此问题:

  • 以升序对数组进行排序。
  • 初始化两个变量,例如left = 0right = N – 1 ,分别存储左指针和右指针的索引。
  • 初始化一个变量,例如cntPairs ,以存储需要删除的对的最小数量,以使数组中的和不等于K的对不存在。
  • 遍历数组并检查以下条件。
    • 如果arr [left] + arr [right] == K ,则将cntPairs的值增加1并更新left + = 1right- = 1。
    • 如果arr [left] + arr [right] ,则更新left + = 1
    • 如果arr [left] + arr [right] ,则更新right-= 1
  • 最后,打印cntPairs的值。

下面是上述方法的实现:

C++14
// C++14 program to implement
// the above approach
#include 
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 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 arr = { 1, 2, 3, 4 };
    int K = 5;
     
    // Function call
    cout << (maxcntPairsSumKRemoved(arr, K));
     
    return 0;
}
 
// This code is contributed by mohit kumar 29


Java
// 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));  
}
}


Python3
# 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))


C#
// 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


输出:
2

时间复杂度: O(N)
辅助空间: O(1)