From adc9be2dbc2f0cce65d83c42ac7418bce7b3c559 Mon Sep 17 00:00:00 2001 From: Matej Focko Date: Sat, 8 Jun 2024 22:51:28 +0200 Subject: [PATCH] =?UTF-8?q?go:=20add=20=C2=AB523.=20Continuous=20Subarray?= =?UTF-8?q?=20Sum=C2=BB?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit Signed-off-by: Matej Focko --- go/continuous-subarray-sum.go | 22 ++++++++++++++++++++++ 1 file changed, 22 insertions(+) create mode 100644 go/continuous-subarray-sum.go diff --git a/go/continuous-subarray-sum.go b/go/continuous-subarray-sum.go new file mode 100644 index 0000000..4da0094 --- /dev/null +++ b/go/continuous-subarray-sum.go @@ -0,0 +1,22 @@ +package continuous_subarray_sum + +func checkSubarraySum(nums []int, k int) bool { + first_index_of := make(map[int]int) + first_index_of[0] = -1 + + remainder := 0 + for i, x := range nums { + remainder = (remainder + x) % k + + j, seen := first_index_of[remainder] + if seen { + if i-j > 1 { + return true + } + } else { + first_index_of[remainder] = i + } + } + + return false +}