Given an array of integers a, find and return whether the given array contains a subarray with a sum equal to 0. If the given array contains a sub-array with sum zero return 1 else return 0. Note: length of sub array should be at least one.
Answers
Answered by
0
Input: {4, 2, -3, 1, 6}
Output: true
There is a subarray with zero sum from index 1 to 3.
Input: {4, 2, 0, 1, 6}
Output: true
There is a subarray with zero sum from index 2 to 2.
Input: {-3, 2, 3, 1, 6}
Output: false
There is no subarray with zero sum.
Recommended: Please solve it on “PRACTICE ” first, before moving on to the solution.
A simple solution is to consider all subarrays one by one and check the sum of every subarray. We can run two loops: the outer loop picks a starting point i and the inner loop tries all subarrays starting from i (See this for implementation). Time complexity of this method is O(n2).
PLEASE MAKE ME AS A BRAINLIST ANSWER
Similar questions
English,
5 months ago
Physics,
5 months ago
Computer Science,
10 months ago
Computer Science,
10 months ago
Math,
1 year ago