Java: Check if a given array contains a subarray with 0 sum
54. Check if array contains a subarray with sum zero
Write a Java program to check if a given array contains a subarray with 0 sum.
Example:
Input :
nums1= { 1, 2, -2, 3, 4, 5, 6 }
nums2 = { 1, 2, 3, 4, 5, 6 }
nums3 = { 1, 2, -3, 4, 5, 6 }
Output:
Does the said array contain a subarray with 0 sum: true
Does the said array contain a subarray with 0 sum: false
Does the said array contain a subarray with 0 sum: true
Sample Solution:
Java Code:
Sample Output:
Original array: [1, 2, -2, 3, 4, 5, 6] Does the said array contain a subarray with 0 sum: true Original array: [1, 2, 3, 4, 5, 6] Does the said array contain a subarray with 0 sum: false Original array: [1, 2, -3, 4, 5, 6] Does the said array contain a subarray with 0 sum: true
Flowchart:
For more Practice: Solve these Related Problems:
- Write a Java program to find the largest subarray with a given sum in an array.
- Write a Java program to find the longest subarray whose sum is divisible by a given number.
- Write a Java program to find all subarrays in an array whose sum equals a given target.
- Write a Java program to find the number of subarrays with a sum of zero.
Go to:
PREV : Replace each element with the next greatest element.
NEXT : Print all sub-arrays with sum zero in an array.
Java Code Editor:
What is the difficulty level of this exercise?
Test your Programming skills with w3resource's quiz.