📜  具有最大可能和的最小子数组的长度

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

具有最大可能和的最小子数组的长度

给定一个由N个非负整数组成的数组arr[] ,任务是找到总和最大的子数组的最小长度。

例子:

朴素方法:解决给定问题的最简单方法是生成给定数组的所有可能子数组,并打印该子数组的长度,其总和是具有相同总和的所有可能子数组中具有最小长度的数组的总和。

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

有效方法:上述方法可以通过使用所有元素都是非负的事实来优化,因此子数组的最大和是数组本身的和。因此,我们的想法是从数组的任一端排除尾随的0 ,以最小化结果子数组的长度和最大的总和。请按照以下步骤解决问题:

  • 初始化两个变量,比如i0j(N – 1) ,它们存储结果子数组的开始和结束索引。
  • 从前面遍历给定数组,直到遇到正元素并同时增加i的值。
  • 如果i的值为N ,则将结果子数组的最大长度打印为1 ,因为它包含所有元素为0 。否则,从末尾遍历给定数组,直到遇到正元素并同时递减j的值。
  • 完成上述步骤后,打印(j – i + 1)的值作为结果子数组。

下面是上述方法的实现:

C++
// C++ program for the above approach
 
#include 
using namespace std;
 
// Function to find the minimum length
// of the subarray whose sum is maximum
int minimumSizeSubarray(int arr[],
                        int N)
{
    // Stores the starting and the
    // ending index of the resultant
    // subarray
    int i = 0, j = N - 1;
 
    // Traverse the array until a
    // non-zero element is encountered
    while (i < N and arr[i] == 0) {
        i++;
    }
 
    // If the array contains only of 0s
    if (i == N)
        return 1;
 
    // Traverse the array in reverse until
    // a non-zero element is encountered
    while (j >= 0 and arr[j] == 0) {
        j--;
    }
 
    // Return the resultant
    // size of the subarray
    return (j - i + 1);
}
 
// Driver Code
int main()
{
    int arr[] = { 0, 2, 0, 0, 12,
                  0, 0, 0 };
    int N = sizeof(arr) / sizeof(arr[0]);
    cout << minimumSizeSubarray(arr, N);
 
    return 0;
}


Java
// Java program for the above approach
import java.util.*;
class GFG
{
 
// Function to find the minimum length
// of the subarray whose sum is maximum
static int minimumSizeSubarray(int arr[],
                        int N)
{
   
    // Stores the starting and the
    // ending index of the resultant
    // subarray
    int i = 0, j = N - 1;
 
    // Traverse the array until a
    // non-zero element is encountered
    while (i < N && arr[i] == 0) {
        i++;
    }
 
    // If the array contains only of 0s
    if (i == N)
        return 1;
 
    // Traverse the array in reverse until
    // a non-zero element is encountered
    while (j >= 0 && arr[j] == 0) {
        j--;
    }
 
    // Return the resultant
    // size of the subarray
    return (j - i + 1);
}
 
// Driver Code
public static void main(String[] args)
{
    int arr[] = { 0, 2, 0, 0, 12,
                  0, 0, 0 };
    int N = arr.length;
    System.out.print( minimumSizeSubarray(arr, N));
}
}
 
// This code is contributed by code_hunt.


Python3
# Python3 program for the above approach
 
# Function to find the minimum length
# of the subarray whose sum is maximum
def minimumSizeSubarray(arr, N):
     
    # Stores the starting and the
    # ending index of the resultant
    # subarray
    i, j = 0, N - 1
 
    # Traverse the array until a
    # non-zero element is encountered
    while (i < N and arr[i] == 0):
        i += 1
 
    # If the array contains only of 0s
    if (i == N):
        return 1
 
    # Traverse the array in reverse until
    # a non-zero element is encountered
    while (j >= 0 and arr[j] == 0):
        j -= 1
 
    # Return the resultant
    # size of the subarray
    return(j - i + 1)
 
# Driver Code
if __name__ == '__main__':
     
    arr = [ 0, 2, 0, 0, 12, 0, 0, 0 ]
    N = len(arr)
     
    print(minimumSizeSubarray(arr, N))
 
# This code is contributed by mohit kumar 29


C#
// C# program for the above approach
using System;
 
class GFG{
     
// Function to find the minimum length
// of the subarray whose sum is maximum
static int minimumSizeSubarray(int[] arr,
                               int N)
{
     
    // Stores the starting and the
    // ending index of the resultant
    // subarray
    int i = 0, j = N - 1;
 
    // Traverse the array until a
    // non-zero element is encountered
    while (i < N && arr[i] == 0)
    {
        i++;
    }
 
    // If the array contains only of 0s
    if (i == N)
        return 1;
 
    // Traverse the array in reverse until
    // a non-zero element is encountered
    while (j >= 0 && arr[j] == 0)
    {
        j--;
    }
 
    // Return the resultant
    // size of the subarray
    return (j - i + 1);
}
 
// Driver code
public static void Main()
{
    int[] arr = { 0, 2, 0, 0, 12, 0, 0, 0 };
    int N = arr.Length;
     
    Console.Write( minimumSizeSubarray(arr, N));
}
}
 
// This code is contributed by code_hunt


Javascript


输出:
4

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