-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathFind subarray with given sum.py
51 lines (31 loc) · 1.02 KB
/
Find subarray with given sum.py
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
39
40
41
42
43
44
45
46
47
48
49
50
# Python3 program to print subarray with sum as given sum
# Function to print subarray with sum as given sum
def subArraySum(arr, n, Sum):
# create an empty map
Map = {}
# Maintains sum of elements so far
curr_sum = 0
for i in range(0,n):
# add current element to curr_sum
curr_sum = curr_sum + arr[i]
# if curr_sum is equal to target sum
# we found a subarray starting from index 0
# and ending at index i
if curr_sum == Sum:
print("Sum found between indexes 0 to", i)
return
# If curr_sum - sum already exists in map
# we have found a subarray with target sum
if (curr_sum - Sum) in Map:
print("Sum found between indexes", \
Map[curr_sum - Sum] + 1, "to", i)
return
Map[curr_sum] = i
# If we reach here, then no subarray exists
print("No subarray with given sum exists")
# Driver program to test above function
if __name__ == "__main__":
arr = list(map(int,input().split()))
n = len(arr)
Sum = int(input())
subArraySum(arr, n, Sum)