GeezerVisonThe World Wide Web, restored to 800 pixels of glory. |
| Home|About|Gaming cabinet|Archives|How it works|Starter sites |
GeezerVison is showing github.com — coding-problems/Arrays/subarray_with_sum_k.py at master · digitechit07/coding-problems Expand file tree/ Copy pathsubarray_with_sum_k.pyMore file actions 62 lines (46 loc) · 1.33 KB / Copy pathsubarray_with_sum_k.pyFile metadata and controls62 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)) |
| You are visitor 01402792 — GeezerVison does not store the pages you look up. Best viewed with Netscape Navigator 4.7 or Internet Explorer 6 at 800×600. No cookies required. |