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

📅  最后修改于: 2021-10-26 05:37:10             🧑  作者: Mango

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

例子:

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

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


Javascript


输出:
2

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

如果您希望与专家一起参加现场课程,请参阅DSA 现场工作专业课程学生竞争性编程现场课程