📌  相关文章
📜  需要从 X 中减去任一端的最小数组元素数以将 X 减少到 0

📅  最后修改于: 2021-09-06 06:43:19             🧑  作者: Mango

给定一个数组nums[]和一个整数X ,任务是通过删除最左边或最右边的数组元素并将其值从X 中减去最小次数来将X减少到0 。如果可以将X减少到0 ,则打印所需操作的计数。否则,返回-1。

例子:

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

  • 保持左右两个指针指向X 中排除的左右子数组的末端。
  • 初始化left以考虑整个数组,而right不包含任何内容。
  • 因此,将X减少数组的总和。
  • 现在,迭代直到left到达数组的前面。
    • 如果子阵列之和超过X(,X <0),偏移由索引到左和增加X该元素。
    • 如果子阵列之和小于X(,X> 0),用指数右移指针向左和由该元素减少X。
    • 如果在任何时候发现 X 为 0,则更新所需的最小操作数。
  • 打印所需的最少操作次数。
  • 下面是上述方法的实现:
C++14
// C++ program to implement
// the above approach
#include 
using namespace std;
 
// Function to count the minimum
// number of operations required
// to reduce x to 0
static int minOperations(int nums[], int N,
                         int x)
{
     
    // If sum of the array
    // is less than x
    int sum = 0;
     
    for(int i = 0; i < x; i++)
        sum += nums[i];
         
    if (sum < x)
        return -1;
     
    // Stores the count
    // of operations
    int ans = INT_MAX;
     
    // Two pointers to traverse the array
    int l = N - 1, r = N;
     
    // Reduce x by the sum
    // of the entire array
    x -= sum;
     
    // Iterate until l reaches
    // the front of the array
    while (l >= 0)
    {
     
        // If sum of elements from
        // the front exceeds x
        if (x <= 0)
        {
         
            // Shift towards left
            x += nums[l];
            l -= 1;
        }
         
        // If sum exceeds 0
        if (x > 0)
        {
         
            // Reduce x by elements
            // from the right
            r -= 1;
            x -= nums[r];
        }
         
        // If x is reduced to 0
        if (x == 0)
        {
         
            // Update the minimum count
            // of operations required
            ans = min(ans,
            (l + 1) + (N - r));
        }
    }
     
    if (ans < INT_MAX)
        return ans;
    else
        return -1;
}
 
// Driver Code
int main()
{
    int nums[] = { 1, 1, 4, 2, 3 };
     
     // Size of the array
    int N = sizeof(nums) / sizeof(nums[0]);
     
    int x = 5;
    cout << minOperations(nums, N, x);
 
    return 0;
}
 
// This code is contributed by code_hunt


Java
// Java program to implement
// the above approach
import java.util.*;
 
class GFG
{
 
  // Function to count the minimum
  // number of operations required
  // to reduce x to 0
  static int minOperations(int nums[], int x)
  {
 
    // If sum of the array
    // is less than x
    int sum = 0;
    for (int i = 0; i < x; i++)
      sum += nums[i];
    if (sum < x)
      return -1;
 
    // Stores the count
    // of operations
    int ans = Integer.MAX_VALUE;
 
    // Two pointers to traverse the array
    int l = nums.length - 1, r = nums.length;
 
    // Reduce x by the sum
    // of the entire array
    x -= sum;
 
    // Iterate until l reaches
    // the front of the array
    while (l >= 0) {
 
      // If sum of elements from
      // the front exceeds x
      if (x <= 0) {
 
        // Shift towards left
        x += nums[l];
        l -= 1;
      }
 
      // If sum exceeds 0
      if (x > 0) {
 
        // Reduce x by elements
        // from the right
        r -= 1;
        x -= nums[r];
      }
 
      // If x is reduced to 0
      if (x == 0) {
 
        // Update the minimum count
        // of operations required
        ans = Math.min(ans,
                       (l + 1) + (nums.length - r));
      }
    }
    if (ans < Integer.MAX_VALUE)
      return ans;
    else
      return -1;
  }
 
  // Driver Code
  public static void main(String[] args)
  {
    int[] nums = { 1, 1, 4, 2, 3 };
    int x = 5;
    System.out.println(minOperations(nums, x));
  }
}
 
// This code is contributed by shubhamsingh10


Python3
# Python3 Program to implement
# the above approach
 
import math
 
# Function to count the minimum
# number of operations required
# to reduce x to 0
def minOperations(nums, x):
 
    # If sum of the array
    # is less than x
    if sum(nums) < x:
        return -1
 
    # Stores the count
    # of operations
    ans = math.inf
 
    # Two pointers to traverse the array
    l, r = len(nums)-1, len(nums)
 
    # Reduce x by the sum
    # of the entire array
    x -= sum(nums)
 
    # Iterate until l reaches
    # the front of the array
    while l >= 0:
 
        # If sum of elements from
        # the front exceeds x
        if x <= 0:
 
            # Shift towards left
            x += nums[l]
            l -= 1
 
        # If sum exceeds 0
        if x > 0:
 
            # Reduce x by elements
            # from the right
            r -= 1
            x -= nums[r]
 
        # If x is reduced to 0
        if x == 0:
 
            # Update the minimum count
            # of operations required
            ans = min(ans, (l+1) + (len(nums)-r))
 
    return ans if ans < math.inf else -1
 
 
# Driver Code
nums = [1, 1, 4, 2, 3]
x = 5
print(minOperations(nums, x))


C#
// C# Program to implement
// the above approach
using System;
class GFG {
 
  // Function to count the minimum
  // number of operations required
  // to reduce x to 0
  static int minOperations(int[] nums, int x)
  {
 
    // If sum of the array
    // is less than x
    int sum = 0;
    for (int i = 0; i < x; i++)
      sum += nums[i];
    if (sum < x)
      return -1;
 
    // Stores the count
    // of operations
    int ans = Int32.MaxValue;
 
    // Two pointers to traverse the array
    int l = nums.Length - 1, r = nums.Length;
 
    // Reduce x by the sum
    // of the entire array
    x -= sum;
 
    // Iterate until l reaches
    // the front of the array
    while (l >= 0) {
 
      // If sum of elements from
      // the front exceeds x
      if (x <= 0) {
 
        // Shift towards left
        x += nums[l];
        l -= 1;
      }
 
      // If sum exceeds 0
      if (x > 0) {
 
        // Reduce x by elements
        // from the right
        r -= 1;
        x -= nums[r];
      }
 
      // If x is reduced to 0
      if (x == 0) {
 
        // Update the minimum count
        // of operations required
        ans = Math.Min(ans,
                       (l + 1) + (nums.Length - r));
      }
    }
    if (ans < Int32.MaxValue)
      return ans;
    else
      return -1;
  }
 
  // Driver Code
  public static void Main()
  {
    int[] nums = { 1, 1, 4, 2, 3 };
    int x = 5;
    Console.Write(minOperations(nums, x));
  }
}
 
// This code is contributed by ukasp.


Javascript


输出:
2

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

如果您想与行业专家一起参加直播课程,请参阅Geeks Classes Live