📌  相关文章
📜  从数组末端删除最小元素,以使总和至少减少K | |。上)

📅  最后修改于: 2021-05-04 18:50:52             🧑  作者: Mango

给定一个由N个元素组成的数组arr [] ,任务是从数组的末端删除最少数量的元素,以使该数组的总和至少减少K。注意K将始终小于或等于数组所有元素的总和。

例子:

方法:在另一篇文章中已经讨论了基于动态编程的方法。在本文中,将讨论一种使用两指针技术的方法。可以看出,任务是找到最长的子数组,其元素的总和最大为sum(arr)– K ,其中sum(arr)是数组arr []的所有元素的总和。
令这样的子数组的长度为L。因此,要从数组中删除的元素的最小数量将等于N –L 。若要查找最长的此类子数组的长度,请参考本文。

下面是上述方法的实现:

C++
// C++ implementation of the approach
#include 
using namespace std;
  
// Function to return the count of minimum
// elements to be removed from the ends
// of the array such that the sum of the
// array decrease by at least K
int minCount(int* arr, int n, int k)
{
  
    // To store the final answer
    int ans = 0;
  
    // Maximum possible sum required
    int sum = 0;
    for (int i = 0; i < n; i++)
        sum += arr[i];
    sum -= k;
  
    // Left point
    int l = 0;
  
    // Right pointer
    int r = 0;
  
    // Total current sum
    int tot = 0;
  
    // Two pointer loop
    while (l < n) {
  
        // If the sum fits
        if (tot <= sum) {
  
            // Update the answer
            ans = max(ans, r - l);
            if (r == n)
                break;
  
            // Update the total sum
            tot += arr[r++];
        }
  
        else {
  
            // Increment the left pointer
            tot -= arr[l++];
        }
    }
  
    return (n - ans);
}
  
// Driver code
int main()
{
    int arr[] = { 1, 11, 5, 5 };
    int n = sizeof(arr) / sizeof(int);
    int k = 11;
  
    cout << minCount(arr, n, k);
  
    return 0;
}


Java
// Java implementation of the approach 
class GFG
{
      
    // Function to return the count of minimum 
    // elements to be removed from the ends 
    // of the array such that the sum of the 
    // array decrease by at least K 
    static int minCount(int arr[], 
                        int n, int k) 
    { 
      
        // To store the final answer 
        int ans = 0; 
      
        // Maximum possible sum required 
        int sum = 0; 
        for (int i = 0; i < n; i++) 
            sum += arr[i]; 
        sum -= k; 
      
        // Left point 
        int l = 0; 
      
        // Right pointer 
        int r = 0; 
      
        // Total current sum 
        int tot = 0; 
      
        // Two pointer loop 
        while (l < n) 
        { 
      
            // If the sum fits 
            if (tot <= sum)
            { 
      
                // Update the answer 
                ans = Math.max(ans, r - l); 
                if (r == n) 
                    break; 
      
                // Update the total sum 
                tot += arr[r++]; 
            } 
            else 
            { 
      
                // Increment the left pointer 
                tot -= arr[l++]; 
            } 
        }
        return (n - ans); 
    } 
      
    // Driver code 
    public static void main (String[] args)
    { 
        int arr[] = { 1, 11, 5, 5 }; 
        int n = arr.length; 
        int k = 11; 
      
        System.out.println(minCount(arr, n, k)); 
    } 
}
  
// This code is contributed by AnkitRai01


Python3
# Python3 implementation of the approach 
  
# Function to return the count of minimum 
# elements to be removed from the ends 
# of the array such that the sum of the 
# array decrease by at least K 
def minCount(arr, n, k) :
  
    # To store the final answer 
    ans = 0; 
  
    # Maximum possible sum required 
    sum = 0; 
    for i in range(n) :
        sum += arr[i]; 
    sum -= k; 
  
    # Left point 
    l = 0; 
  
    # Right pointer 
    r = 0; 
  
    # Total current sum 
    tot = 0; 
  
    # Two pointer loop 
    while (l < n) :
  
        # If the sum fits 
        if (tot <= sum) :
  
            # Update the answer 
            ans = max(ans, r - l); 
            if (r == n) :
                break; 
  
            # Update the total sum 
            tot += arr[r]; 
            r += 1
      
        else :
  
            # Increment the left pointer 
            tot -= arr[l]; 
            l += 1
      
    return (n - ans); 
  
# Driver code 
if __name__ == "__main__" : 
  
    arr = [ 1, 11, 5, 5 ]; 
    n = len(arr); 
    k = 11; 
  
    print(minCount(arr, n, k)); 
  
# This code is contributed by AnkitRai01


C#
// C# implementation of the approach 
using System;
  
class GFG
{
      
    // Function to return the count of minimum 
    // elements to be removed from the ends 
    // of the array such that the sum of the 
    // array decrease by at least K 
    static int minCount(int []arr, 
                        int n, int k) 
    { 
      
        // To store the final answer 
        int ans = 0; 
      
        // Maximum possible sum required 
        int sum = 0; 
        for (int i = 0; i < n; i++) 
            sum += arr[i]; 
        sum -= k; 
      
        // Left point 
        int l = 0; 
      
        // Right pointer 
        int r = 0; 
      
        // Total current sum 
        int tot = 0; 
      
        // Two pointer loop 
        while (l < n) 
        { 
      
            // If the sum fits 
            if (tot <= sum)
            { 
      
                // Update the answer 
                ans = Math.Max(ans, r - l); 
                if (r == n) 
                    break; 
      
                // Update the total sum 
                tot += arr[r++]; 
            } 
            else
            { 
      
                // Increment the left pointer 
                tot -= arr[l++]; 
            } 
        }
        return (n - ans); 
    } 
      
    // Driver code 
    public static void Main()
    { 
        int []arr = { 1, 11, 5, 5 }; 
        int n = arr.Length; 
        int k = 11; 
      
        Console.WriteLine(minCount(arr, n, k)); 
    } 
}
  
// This code is contributed by AnkitRai01


输出:
2