Skip to content

Commit 2de1695

Browse files
author
shiv
committed
added sub-array sum
1 parent 1e0dc85 commit 2de1695

File tree

3 files changed

+37
-4
lines changed

3 files changed

+37
-4
lines changed

.stock-span.py.swp

-12 KB
Binary file not shown.

ip.txt

+4-4
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
2
2-
7
3-
100 80 60 70 60 75 85
4-
6
5-
10 4 5 90 120 80
2+
5 12
3+
1 2 3 7 5
4+
10 15
5+
1 2 3 4 5 6 7 8 9 10

subarray_sum.py

+33
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,33 @@
1+
# SHIV's
2+
# Subarray with given sum
3+
'''
4+
testing cases:
5+
2
6+
5 12
7+
1 2 3 7 5
8+
10 15
9+
1 2 3 4 5 6 7 8 9 10
10+
11+
'''
12+
tn = int(input())
13+
14+
def process(arr, s):
15+
sum_arr = []
16+
for ind, e in enumerate(arr):
17+
sum_arr.append(e)
18+
if sum(sum_arr) == s:
19+
break
20+
if sum(sum_arr) > s:
21+
sum_arr.pop(0)
22+
if sum(sum_arr) == s:
23+
break
24+
inds, inde = arr.index(sum_arr[0]), arr.index(sum_arr[-1])
25+
if sum(arr[inds:inde+1]) == s:
26+
print(inds+1, inde+1)
27+
else: print(-1)
28+
29+
for i in range(tn):
30+
n_s = input().split(' ')
31+
n, s = int(n_s[0]), int(n_s[1])
32+
arr = list(map(int, input().split(' ')))
33+
process(arr, s)

0 commit comments

Comments
 (0)