Prepin
Log in

Verify whether a subarray with a sum of zero exists.

Approach / Explanation

This question requires you to develop a program or method that determines whether there is a subarray within a given array of integers that sums to zero. Here, "subarray" refers to a contiguous segment of the array. Your program should return true if such a subarray exists; otherwise, it should return false. To successfully address this question, you should focus on the following key points: Comprehension of the subarray concept in arrays. Effective utilization of data structures like sets and arrays, along with looping constructs. Proficiency in the relevant programming language needed to implement the solution.

Suggested Answer

In this instance, I will utilize Python to address the issue. def zero_sum_subarray_exists ( arr ) : # initialize sum of elements sum = 0 # create an empty set to store sum of elements of each subarray s = set ( ) for i in range ( len ( arr ) ) : sum += arr [ i ] # If sum is zero, or it is already present in set, return true if sum == 0 or sum in s : return True # insert sum into set s . add ( sum ) # No subarray with zero sum exists return False # driver code to test above function arr = [ 4 , 2 , - 3 , 1 , 6 ] print ( zero_sum_subarray_exists ( arr ) ) # prints: True In this example, we generate a set and for each element in the array, we add it to the current sum. If the sum equals zero or this sum has already been encountered, it indicates the presence of a zero-sum subarray, and we return true. If we complete the iteration over the array without identifying any zero-sum subarray, we return false.

Alternative Answer

In this instance, I will utilize Java to address the issue. import java . util . HashSet ; public class Main { public static Boolean zeroSumSubarrayExists ( int [ ] arr ) { // create an empty set to store the sum of elements of each subarray `A[0…i]` where `0 <= i < arr.length` HashSet < Integer > set = new HashSet < > ( ) ; // stores prefix sum int sum = 0 ; for ( int value : arr ) { // sum of elements so far sum += value ; // if sum is seen before, we have found a subarray with 0 sum if ( sum == 0 || set . contains ( sum ) ) { return true ; } // insert sum so far into the set set . add ( sum ) ; } // we reach here when no subarray with 0 sum is found return false ; } public static void main ( String [ ] args ) { int [ ] arr = { 4 , 2 , - 3 , 1 , 6 } ; System . out . println ( zeroSumSubarrayExists ( arr ) ) ; // prints: true } } This Java example follows a similar approach to the Python example, utilizing a HashSet to keep track of the sums of the subarrays. It checks whether the sum equals zero or if that sum has been encountered previously, returning true if so. If no such subarray is found by the end of the loop, it returns false.

Question Details

Difficulty & Category

Medium

Product

For CandidatesFor RecruitersCompaniesBrowse JobsInterview QuestionsDeep ProfileSkill AssessmentOpportunity Matching
Prepin.ai

© 2026 Prepin LLC · Philadelphia, PA · All rights reserved.