forked from MTrajK/coding-problems
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathsubarray_with_sum_k.py
More file actions
62 lines (46 loc) · 1.33 KB
/
subarray_with_sum_k.py
File metadata and controls
62 lines (46 loc) · 1.33 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
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
'''
Subarray with given sum
Given an unsorted array A of size N of non-negative integers, find a continuous sub-array
which adds to a given number. Find starting and ending positions(1 indexing) of first such
occuring subarray from the left if sum equals to subarray, else print -1.
Input: [1, 2, 3, 4, 5, 6, 7, 8, 9, 10], 15
Output: 1, 5
=========================================
Adjust the start and end index, in each step increase start or end idx.
If sum is bigger than K, remove element from the start idx from the sum.
Else add element from the end idx to the sum.
Time Complexity: O(N)
Space Complexity: O(1)
'''
############
# Solution #
############
def find_subarray(arr, k):
n = len(arr)
if n == 0:
return -1
start = 0
end = 0
current_sum = arr[0]
while end < n:
if current_sum == k:
return (start + 1, end + 1)
if current_sum < k:
end += 1
current_sum += arr[end]
else:
current_sum -= arr[start]
start += 1
return -1
###########
# Testing #
###########
# Test 1
# Correct result => (1, 5)
print(find_subarray([1, 2, 3, 4, 5, 6, 7, 8, 9, 10], 15))
# Test 2
# Correct result => (2, 4)
print(find_subarray([1, 2, 3, 7, 5], 12))
# Test 3
# Correct result => (5, 5)
print(find_subarray([6, 6, 6, 6, 3], 3))