
Data Structure
Networking
RDBMS
Operating System
Java
MS Excel
iOS
HTML
CSS
Android
Python
C Programming
C++
C#
MongoDB
MySQL
Javascript
PHP
- Selected Reading
- UPSC IAS Exams Notes
- Developer's Best Practices
- Questions and Answers
- Effective Resume Writing
- HR Interview Questions
- Computer Glossary
- Who is Who
C Program to Find Subarray with Sum Equal to 0
What is a Subarray?
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.
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:
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.
- Start iterating through all possible sub-arrays.
- 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 sub-arrays, print "No".
Example
The following example implements above mentioned-steps to find the sub-array 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) { // found a subarray with sum zero return 1; } } } // No subarray with sum 0 return 0; } int main() { int arr[] = {4, 2, -3, 1}; int n = sizeof(arr) / sizeof(arr[0]); if (hasZeroSumSubarray(arr, n)) { printf("Yes
"); } else { printf("No
"); } return 0; }
The output of the above code is:
Yes
Using Hashing Approach
In the hashing approach, we use a hash set to store the cumulative. 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.
- Initialize a hash set to store the cumulative sum values as we traverse the array.
- Now, 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 sub-array with sum as 0.
#include <stdio.h> #include <stdlib.h> #define MAX 1000 int hasZeroSumSubarray(int arr[], int n) { int sum = 0; int hash[MAX] = {0}; // Initialize hash array to 0 for (int i = 0; i < n; i++) { sum += arr[i]; // If sum is 0 or the sum has been seen before, we found a subarray with sum 0 if (sum == 0 || hash[sum] == 1) { return 1; } // Mark this cumulative sum as seen hash[sum] = 1; } // No subarray found with sum 0 return 0; } int main() { int arr[] = {4, 2, -3, 1}; int n = sizeof(arr) / sizeof(arr[0]); if (hasZeroSumSubarray(arr, n)) { printf("Yes
"); } else { printf("No
"); } return 0; }
The output of the above code is:
Yes
Using Prefix Sum and Hashing Approach
In this approach, we sort prefix sums and check if they contain duplicates.
- Create a set to store prefix sums.
- Now, Traverse the array while computing the prefix sum.
- If the prefix sum is found in the set, then return "Yes".
Example
The following example implements above mentioned-steps to find the sub-array with sum as 0.
#include <stdio.h> #include <stdlib.h> #define MAX 10000 int hasZeroSumSubarray(int arr[], int n) { int prefixSum[MAX] = {0}; int sum = 0; for (int i = 0; i < n; i++) { sum += arr[i]; if (sum == 0 || prefixSum[sum + MAX / 2]) { return 1; } prefixSum[sum + MAX / 2] = 1; } return 0; } int main() { int arr[] = {4, 2, -3, 1}; int n = sizeof(arr) / sizeof(arr[0]); if (hasZeroSumSubarray(arr, n)) { printf("Yes
"); } else { printf("No
"); } 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^2) | O(1) |
Hashing | O(n) | O(n) |
Prefix Sum + Hashing | O(n) | O(n) |