给定一个整数数组arr ,任务是检查是否存在一个子数组(给定数组除外),使得其元素之和大于或等于给定数组的元素之和。如果没有这样的子数组,则打印No ,否则打印Yes 。
例子:
Input: arr = {5, 6, 7, 8}
Output: No
Explanation:
There isn’t any subarray of the given array such that sum of its elements is greater than or equal to the sum of elements of given array.
Input: arr = {-1, 7, 4}
Output: Yes
Explanation:
There exist a subarray {7, 4} whose sum is greater than the sum of elements of given array.
方法:总和大于原始数组总和的子数组只有在两种情况之一才有可能
- 如果给定数组的所有元素之和小于或等于 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