Article Categories
- All Categories
-
Data Structure
-
Networking
-
RDBMS
-
Operating System
-
Java
-
MS Excel
-
iOS
-
HTML
-
CSS
-
Android
-
Python
-
C Programming
-
C++
-
C#
-
MongoDB
-
MySQL
-
Javascript
-
PHP
-
Economics & Finance
C Program to Find if there is any subarray with a sum equal to 0
A subarray is a contiguous part of the array. The sum of elements in a subarray is the cumulative sum of elements of a subarray. For example: in the given array [1, 2, 3, 4, 5, 6] the subarray is [3, 4, 5].
In this article, we are given an array and need to find if any subarray has a sum equal to zero using C.
Problem Statement
Example 1
Input:
arr[] = {4, 2, -3, 1}
Output:
Yes
Explanation:
The subarray {2, -3, 1} has a sum equal to 0.
Example 2
Input:
arr[] = {1, 2, 3, 4, 5}
Output:
No
Explanation:
There is no subarray with a sum equal to 0 in this case.
Below are different approaches to check if a subarray with sum zero exists
Method 1: Using Brute Force Approach
In the brute force approach, we check for every possible subarray in the given array. In this approach we calculate the sum for each subarray and check if any subarray has a sum equal to zero.
Algorithm
- Start iterating through all possible subarrays.
- For each subarray, calculate the sum.
- If the sum equals 0, print "Yes" and stop the program.
- If no such subarray is found after checking all possible subarrays, print "No".
Example
The following example implements above mentioned steps to find the subarray with sum as 0
#include <stdio.h>
int hasZeroSumSubarray(int arr[], int n) {
for (int start = 0; start < n; start++) {
int sum = 0;
for (int end = start; end < n; end++) {
sum += arr[end];
if (sum == 0) {
return 1; // Found a subarray with sum zero
}
}
}
return 0; // No subarray with sum 0
}
int main() {
int arr[] = {4, 2, -3, 1};
int n = sizeof(arr) / sizeof(arr[0]);
if (hasZeroSumSubarray(arr, n)) {
printf("Yes<br>");
} else {
printf("No<br>");
}
return 0;
}
The output of the above code is
Yes
Method 2: Using Hashing Approach
In the hashing approach, we use a hash set to store the cumulative sum. If the cumulative sum at any index is 0 or if the cumulative sum has already been found before, we know that a subarray with a sum equal to zero exists.
Algorithm
- Initialize a hash set to store the cumulative sum values as we traverse the array.
- Traverse the array while keeping track of the cumulative sum.
- If the cumulative sum becomes 0 or if the cumulative sum is already in the hash set, return "Yes".
- If no such subarray is found, return "No".
Example
Here is an example implementing above mentioned steps to find the subarray with sum as 0
#include <stdio.h>
#define MAX_SIZE 10001
#define OFFSET 5000
int hasZeroSumSubarray(int arr[], int n) {
int hash[MAX_SIZE] = {0};
int sum = 0;
for (int i = 0; i < n; i++) {
sum += arr[i];
// If sum is 0 or the sum has been seen before
if (sum == 0 || hash[sum + OFFSET]) {
return 1;
}
// Mark this cumulative sum as seen
hash[sum + OFFSET] = 1;
}
return 0; // No subarray found with sum 0
}
int main() {
int arr[] = {4, 2, -3, 1};
int n = sizeof(arr) / sizeof(arr[0]);
if (hasZeroSumSubarray(arr, n)) {
printf("Yes<br>");
} else {
printf("No<br>");
}
return 0;
}
The output of the above code is
Yes
Method 3: Using Prefix Sum and Hashing Approach
This approach is similar to the hashing approach but explicitly uses the concept of prefix sums. We store prefix sums and check if any prefix sum repeats or equals zero.
Algorithm
- Create a set to store prefix sums.
- Traverse the array while computing the prefix sum.
- If the prefix sum is 0 or found in the set, then return "Yes".
- Otherwise, add the prefix sum to the set.
Example
The following example implements above mentioned steps to find the subarray with sum as 0
#include <stdio.h>
#define MAX_SIZE 10001
#define OFFSET 5000
int hasZeroSumSubarray(int arr[], int n) {
int prefixSum[MAX_SIZE] = {0};
int sum = 0;
for (int i = 0; i < n; i++) {
sum += arr[i];
if (sum == 0 || prefixSum[sum + OFFSET]) {
return 1;
}
prefixSum[sum + OFFSET] = 1;
}
return 0;
}
int main() {
int arr[] = {4, 2, -3, 1};
int n = sizeof(arr) / sizeof(arr[0]);
if (hasZeroSumSubarray(arr, n)) {
printf("Yes<br>");
} else {
printf("No<br>");
}
return 0;
}
The output of the above code is
Yes
Complexity Comparison
Here is a comparison of time and space complexity of all the above approaches
| Approach | Time Complexity | Space Complexity |
|---|---|---|
| Brute Force | O(n²) | O(1) |
| Hashing | O(n) | O(n) |
| Prefix Sum + Hashing | O(n) | O(n) |
Conclusion
The hashing approach provides the optimal solution with O(n) time complexity. It efficiently detects zero-sum subarrays by tracking cumulative sums and identifying when the same sum appears twice.
