📌  相关文章
📜  最小化要减少的 Array 元素以使子序列总和 1 到 Array max 成为可能

📅  最后修改于: 2022-05-13 01:56:10.383000             🧑  作者: Mango

最小化要减少的 Array 元素以使子序列总和 1 到 Array max 成为可能

给定一个正整数数组a[] 。从数组的任意数量的元素中减去任意正整数,使得存在总和为1s 的所有可能子序列,其中s表示数组中所有元素的总和。找到要减去的数组元素的最小可能数量。

方法:如果s<2*(数组的大小) 将找到所需的数组,其中s表示数组中所有元素的总和。如果数组不满足这个条件,我们必须从给定数组的元素中减去s-((2*size of array )-1) 。因为我们必须找到要减去的数组元素的最小可能数量。我们将从数组的最大元素中执行最大可能的减法并重复该过程,直到我们得到所需的结果。请按照以下步骤解决问题:

  • 将变量sum初始化为数组arr[]的所有元素的总和。
  • 将变量diff初始化为sum – (2*size) +1。
  • 将变量ans初始化为0,i初始化为size-1。
  • 对数组arr[] 进行排序。
  • 遍历一个while循环直到diff大于0并执行以下任务:
    • 从变量diff中减去arr[i]-1的值,并将ans的值增加1
    • i的值减1。
  • 执行上述步骤后,打印ans的值作为答案。

下面是上述方法的实现。

C++
// C++ program for the above approach
#include 
using namespace std;
 
// Function to find the total number
// of operations required
int find(int arr[], int size)
{
    int sum = 0;
 
    // Find sum of all element of array arr.
    for (int i = 0; i < size; i++) {
        sum += arr[i];
    }
 
    // Variable to store integer which needs
    // to be subtracted from arr in total.
    int diff = sum - ((2 * size) - 1);
    int ans = 0;
    int i = size - 1;
    sort(arr, arr + size);
 
    // Iteration to calcute total number of
    // subtraction required to get desired array.
    while (diff > 0) {
        diff -= (arr[i] - 1);
        i--;
        ans++;
    }
    return ans;
}
 
// Driver Code
int main()
{
    int arr[] = { 4, 3, 2, 2, 1, 6 };
 
    cout << find(arr, 6) << "\n";
 
    return 0;
}


Java
// Java program for the above approach
import java.io.*;
import java.lang.*;
import java.util.*;
 
class GFG
{
 
  // Function to find the total number
  // of operations required
  static int find(int arr[], int size)
  {
    int sum = 0;
 
    // Find sum of all element of array arr.
    for (int i = 0; i < size; i++) {
      sum += arr[i];
    }
 
    // Variable to store integer which needs
    // to be subtracted from arr in total.
    int diff = sum - ((2 * size) - 1);
    int ans = 0;
    int i = size - 1;
    Arrays.sort(arr);
 
    // Iteration to calcute total number of
    // subtraction required to get desired array.
    while (diff > 0) {
      diff -= (arr[i] - 1);
      i--;
      ans++;
    }
    return ans;
  }
 
  // Driver code
  public static void main (String[] args) {
    int arr[] = { 4, 3, 2, 2, 1, 6 };
    int N = arr.length;
    System.out.println( find(arr, N));
  }
}
 
// This code is contributed by hrithikgarg03188


Python
# Python program for the above approach
 
# Function to find the total number
# of operations required
def find(arr, size):
    sum = 0
 
    #  Find sum of all element of array arr.
    for i in range(0, size):
        sum = sum + arr[i]
 
    #  Variable to store integer which needs
    #  to be subtracted from arr in total.
    diff = sum - ((2 * size) - 1)
    ans = 0
    i = size - 1
    arr.sort()
 
    #  Iteration to calcute total number of
    #  subtraction required to get desired array.
    while (diff > 0):
        diff = diff - (arr[i] - 1)
        i = i - 1
        ans = ans + 1
 
    return ans
 
#  Driver Code
arr = [4, 3, 2, 2, 1, 6]
print(find(arr, 6))
 
# This code is contributed by Taranpreet


C#
// C# program for the above approach
using System;
class GFG {
 
  // Function to find the total number
  // of operations required
  static int find(int[] arr, int size)
  {
    int sum = 0;
 
    // Find sum of all element of array arr.
    for (int i = 0; i < size; i++) {
      sum += arr[i];
    }
 
    // Variable to store integer which needs
    // to be subtracted from arr in total.
    int diff = sum - ((2 * size) - 1);
    int ans = 0;
    int j = size - 1;
    Array.Sort(arr);
 
    // Iteration to calcute total number of
    // subtraction required to get desired array.
    while (diff > 0) {
      diff -= (arr[j] - 1);
      j--;
      ans++;
    }
    return ans;
  }
 
  // Driver Code
  public static void Main()
  {
    int[] arr = { 4, 3, 2, 2, 1, 6 };
 
    Console.WriteLine(find(arr, 6));
  }
}
 
// This code is contributed by ukasp.


Javascript



输出:
2

时间复杂度: O(n*log(n))
辅助空间: O(n)