📜  最小大小的子数组,其总和按非递增顺序递增

📅  最后修改于: 2021-04-22 08:15:41             🧑  作者: Mango

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

例子:

方法:

  1. 最初,我们将对数组arr进行排序,并定义一个名为A的向量。现在,我们将首先计算整个数组的总和,然后从后侧迭代整个循环,同时连续更新temp 。循环运行直到温度变为零。
  2. 实际上,从背面迭代循环时发生的事情是,当我们从背面迭代循环时,我们正在考虑最大值。因此,可以说我们可以在更短的时间内并通过减少变量来达到目标条件。
  3. 现在,作为循环运行,我们继续只需添加NUMS更新温度值[I]到它,并且还加入NUMS [I],矢量A。最后,我们返回向量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


输出:
13 13 12

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