-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathsubarray_sum.py
More file actions
44 lines (32 loc) · 1.06 KB
/
subarray_sum.py
File metadata and controls
44 lines (32 loc) · 1.06 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
#Subarray with given sum
#Given an unsorted array of nonnegative integers, find a continuous subarray which adds to a given number.
#https://www.geeksforgeeks.org/find-subarray-with-given-sum/
# Input: arr[] = {1, 4, 20, 3, 10, 5}, sum = 33
# Ouptut: Sum found between indexes 2 and 4
# Sum of elements between indices
# 2 and 4 is 20 + 3 + 10 = 33
# Input: arr[] = {1, 4, 0, 0, 3, 10, 5}, sum = 7
# Ouptut: Sum found between indexes 1 and 4
# Sum of elements between indices
# 1 and 4 is 4 + 0 + 0 + 3 = 7
# Input: arr[] = {1, 4}, sum = 0
# Output: No subarray found
# There is no subarray with 0 sum
def subarraySum(arr,n,sum):
curr_sum=0
for i in range(n):
curr_sum=arr[i]
j=i+1
while j<=n:
if curr_sum==sum:
print("Found")
output=[i,j-1]
print(str(output))
return 1
if curr_sum > sum or j == n:
break
curr_sum=curr_sum+arr[j]
j = j+1
print("not found")
return 0
subarraySum([1, 4, 20, 3, 10, 5],6,33)