📌  相关文章
📜  检查是否存在总和大于给定数组的子数组

📅  最后修改于: 2021-09-06 11:25:26             🧑  作者: Mango

给定一个整数数组arr ,任务是检查是否存在一个子数组(给定数组除外),使得其元素之和大于或等于给定数组的元素之和。如果没有这样的子数组,则打印No ,否则打印Yes
例子:

方法:总和大于原始数组总和的子数组只有在两种情况之一才有可能

  • 如果给定数组的所有元素之和小于或等于 0
  • 如果存在总和为负的前缀或后缀子数组

所以检查所有可能的前缀和后缀子数组的总和是否小于或等于零,答案是肯定的。否则答案是否定的。
下面是上述方法的实现

C++
// C++ program to check if a subarray exists
// with sum greater than the given Array
#include 
using namespace std;
 
// Function to check whether there exists
// a subarray whose sum is greater than
// or equal to sum of given array elements
int subarrayPossible(int arr[], int n)
{
    // Initialize sum with 0
    int sum = 0;
 
    // Checking possible prefix subarrays.
    // If sum of them is less than or equal
    // to zero, then return 1
    for (int i = 0; i < n; i++) {
        sum += arr[i];
 
        if (sum <= 0)
            return 1;
    }
 
    // again reset sum to zero
    sum = 0;
 
    // Checking possible suffix subarrays.
    // If sum of them is less than or equal
    // to zero, then return 1
    for (int i = n - 1; i >= 0; i--) {
        sum += arr[i];
 
        if (sum <= 0)
            return 1;
    }
 
    // Otherwise return 0
    return 0;
}
 
// Driver Code
int main()
{
    int arr[] = { 10, 5, -12, 7, -10, 20,
                  30, -10, 50, 60 };
 
    int size = sizeof(arr) / sizeof(arr[0]);
 
    if (subarrayPossible(arr, size))
        cout << "Yes"
             << "\n";
    else
        cout << "No"
             << "\n";
 
    return 0;
}


Java
// Java program to check if a subarray exists
// with sum greater than the given Array
import java.util.*;
 
class GFG{
 
    // Function to check whether there exists
    // a subarray whose sum is greater than
   // or equal to sum of given array elements
    static boolean subarrayPossible(int arr[], int n)
    {
        // Initialize sum with 0
        int sum = 0;
     
        // Checking possible prefix subarrays.
        // If sum of them is less than or equal
        // to zero, then return 1
        for (int i = 0; i < n; i++) {
            sum += arr[i];
     
            if (sum <= 0)
                return true;
        }
     
        // again reset sum to zero
        sum = 0;
     
        // Checking possible suffix subarrays.
        // If sum of them is less than or equal
        // to zero, then return 1
        for (int i = n - 1; i >= 0; i--) {
            sum += arr[i];
     
            if (sum <= 0)
                return true;
        }
     
        // Otherwise return 0
        return false;
    }
     
    // Driver Code
    public static void main(String args[])
    {
        int arr[] = { 10, 5, -12, 7, -10, 20, 30, -10, 50, 60 };
     
        int size = arr.length;
     
        if (subarrayPossible(arr, size))
            System.out.print("Yes"+"\n");
        else
            System.out.print("No"+"\n");
    }
}
 
// This code is contributed by AbhiThakur


Python3
# Python3 program to check if a subarray exists
# with sum greater than the given Array
 
# Function to check whether there exists
# a subarray whose sum is greater than
# or equal to sum of given array elements
def subarrayPossible(arr, n):
    # Initialize sum with 0
    sum = 0;
 
    # Checking possible prefix subarrays.
    # If sum of them is less than or equal
    # to zero, then return 1
    for i in range(n):
        sum += arr[i];
 
        if (sum <= 0):
            return True;
     
 
    # again reset sum to zero
    sum = 0;
 
    # Checking possible suffix subarrays.
    # If sum of them is less than or equal
    # to zero, then return 1
    for i in range(n-1, -1,-1):
        sum += arr[i];
 
        if (sum <= 0):
            return True;
     
 
    # Otherwise return 0
    return False;
 
# Driver Code
if __name__ == '__main__':
    arr = [ 10, 5, -12, 7, -10, 20, 30, -10, 50, 60 ];
 
    size = len(arr);
 
    if (subarrayPossible(arr, size)):
        print("Yes");
    else:
        print("No");
 
# This code is contributed by Princi Singh


C#
// C# program to check if a subarray exists
// with sum greater than the given Array
using System;
 
class GFG{
  
// Function to check whether there exists
// a subarray whose sum is greater than
// or equal to sum of given array elements
    static bool subarrayPossible(int []arr, int n)
    {
        // Initialize sum with 0
        int sum = 0;
      
        // Checking possible prefix subarrays.
        // If sum of them is less than or equal
        // to zero, then return 1
        for (int i = 0; i < n; i++) {
            sum += arr[i];
      
            if (sum <= 0)
                return true;
        }
      
        // again reset sum to zero
        sum = 0;
      
        // Checking possible suffix subarrays.
        // If sum of them is less than or equal
        // to zero, then return 1
        for (int i = n - 1; i >= 0; i--) {
            sum += arr[i];
      
            if (sum <= 0)
                return true;
        }
      
        // Otherwise return 0
        return false;
    }
      
    // Driver Code
    public static void Main(String []args)
    {
        int []arr = { 10, 5, -12, 7, -10, 20, 30, -10, 50, 60 };
      
        int size = arr.Length;
      
        if (subarrayPossible(arr, size))
            Console.Write("Yes"+"\n");
        else
            Console.Write("No"+"\n");
    }
}
 
// This code is contributed by Princi Singh


Javascript


输出:
Yes

性能分析

  • 时间复杂度:在上述方法中,我们对长度为 N 的数组进行了两次迭代,因此时间复杂度为O(N)
  • 辅助空间复杂度:在上面的方法中,我们只使用了几个常量,所以辅助空间复杂度是O(1)

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