📌  相关文章
📜  以非递增顺序具有最大总和的最小大小子数组

📅  最后修改于: 2021-09-04 08:24:42             🧑  作者: Mango

给定一个数组arr ,任务是找到数组元素的子数组,其总和严格大于其余元素。子数组的大小应该是最小的,总和应该是最大的,并且必须是非递增的。
例子:

方法:

  1. 最初我们将对数组arr 进行排序并定义一个名为A的向量。现在我们将首先计算整个数组的总和,然后从后面迭代整个循环,同时连续更新temp 。循环运行直到温度变为零。
  2. 实际上,从背面迭代循环时发生的情况是,当我们从背面迭代循环时,我们正在考虑最大值。因此,我们可以说我们可以在更短的时间内达到目标条件,也可以通过使用更少的变量。
  3. 现在,随着循环运行,我们通过将nums[i]添加到它并将nums[i]添加到向量 A来不断更新 temp 的值。最后我们返回代表我们想要的输出结果的向量A。

下面是上述方法的实现:

C++
// C++ program to find the Minimum size Subarray
// with maximum sum in non-increasing order
 
#include 
using namespace std;
 
// Function to find the Minimum size Subarray
void minSet(vector& nums)
{
 
    vector A;
 
    // sort numbers in descending order
    sort(nums.begin(), nums.end());
 
    // get total sum of the given array.
    int sum = 0;
    for (int i = 0; i < nums.size(); i++)
        sum += nums[i];
 
    int temp = 0;
 
    // Loop until the sum of numbers
    // is greater than sum/2
    for (int i = nums.size() - 1;
         i >= 0 && temp <= sum / 2;
         i--) {
 
        A.push_back(nums[i]);
        temp += nums[i];
    }
 
    // Print the Minimum size Subarray
    for (int i = 0; i < A.size(); i++)
        cout << A[i] << " ";
}
 
// Driver code
int main()
{
    vector vec
        = { 7, 6, 13, 13, 12, 11 };
 
    minSet(vec);
 
    return 0;
}


Java
// Java program to find the Minimum size Subarray
// with maximum sum in non-increasing order
import java.util.*;
 
class GFG {
 
  // Function to find the Minimum size Subarray
  static void minSet(ArrayList nums) {
 
    ArrayList A = new ArrayList ();
 
    // sort numbers in descending order
    Collections.sort(nums);
 
    // get total sum of the given array.
    int sum = 0;
    for (int i = 0; i= 0 && temp<= sum / 2;
      i--) {
 
      A.add(nums.get(i));
      temp += nums.get(i);
    }
 
    // Print the Minimum size Subarray
    for (int i = 0; i gfg = new ArrayList ();
    gfg.add(7);
    gfg.add(6);
    gfg.add(13);
    gfg.add(13);
    gfg.add(12);
    gfg.add(11);
 
    // Function calling
    minSet(gfg);
  }
}
 
// This code is contributed by rutvik_56


Python3
# Python3 program to find the Minimum size Subarray
# with maximum sum in non-increasing order
 
# Function to find the Minimum size Subarray
def minSet(nums) :
 
    A = []
 
    # sort numbers in descending order
    nums.sort()
 
    # get total sum of the given array.
    sum = 0
    for i in range(0,len(nums)):
        sum += nums[i]
 
    temp = 0
 
    # Loop until the sum of numbers
    # is greater than sum/2
    for i in range(len(nums)-1, -1, -1):
        if(temp > sum / 2):
            break
        A.append(nums[i])
        temp += nums[i]
 
    # Print the Minimum size Subarray
    for i in range(0, len(A)):
        print(A[i], end = ' ')
 
# Driver code
vec = [ 7, 6, 13, 13, 12, 11 ]
 
minSet(vec);
 
# This code is contributed by Sanjit_Prasad


C#
// C# program to find the Minimum size Subarray
// with maximum sum in non-increasing order
using System;
using System.Collections.Generic;
 
class GFG {
  
  // Function to find the Minimum size Subarray
  static void minSet(List nums) {
  
    List A = new List ();
  
    // sort numbers in descending order
    nums.Sort();
  
    // get total sum of the given array.
    int sum = 0;
    for (int i = 0; i < nums.Count; i++)
      sum += nums[i];
  
    int temp = 0;
  
    // Loop until the sum of numbers
    // is greater than sum/2
    for (int i = nums.Count - 1; 
      i >= 0 && temp<= sum / 2;
      i--) {
  
      A.Add(nums[i]);
      temp += nums[i];
    }
  
    // Print the Minimum size Subarray
    for (int i = 0; i gfg = new List ();
    gfg.Add(7);
    gfg.Add(6);
    gfg.Add(13);
    gfg.Add(13);
    gfg.Add(12);
    gfg.Add(11);
  
    // Function calling
    minSet(gfg);
  }
}
  
// This code is contributed by sapnasingh4991


Javascript


输出:
13 13 12

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

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