From bb8656af5a5703e72ce8faa27149d88996a67d30 Mon Sep 17 00:00:00 2001 From: javy99 Date: Mon, 9 Jun 2025 11:25:58 +0200 Subject: [PATCH] add solution for two sum II where input arr is sorted --- .../solution.go | 64 +++++++++++++++++++ .../solution_test.go | 46 +++++++++++++ 2 files changed, 110 insertions(+) create mode 100644 2_two_pointers/2_two_sum_II_input_array_is_sorted/solution.go create mode 100644 2_two_pointers/2_two_sum_II_input_array_is_sorted/solution_test.go diff --git a/2_two_pointers/2_two_sum_II_input_array_is_sorted/solution.go b/2_two_pointers/2_two_sum_II_input_array_is_sorted/solution.go new file mode 100644 index 0000000..c0ef712 --- /dev/null +++ b/2_two_pointers/2_two_sum_II_input_array_is_sorted/solution.go @@ -0,0 +1,64 @@ +package twosumiiinputarrayissorted + +// Time complexity: O(n^2) +// Space complexity: O(1) +func TwoSumBruteForce(nums []int, target int) []int { + for i := 0; i < len(nums); i++ { + for j := i + 1; j < len(nums); j++ { + if nums[i]+nums[j] == target { + return []int{i + 1, j + 1} + } + } + } + return nil +} + +// Time complexity: O(n * log n) +// Space complexity: O(1) +func TwoSumBinarySearch(nums []int, target int) []int { + for i := range nums { + left, right := i+1, len(nums)-1 + needed := target - nums[i] + for left <= right { + mid := left + (right-left)/2 + if nums[mid] == needed { + return []int{i + 1, mid + 1} + } else if nums[mid] < needed { + left = mid + 1 + } else { + right = mid - 1 + } + } + } + return nil +} + +// Time complexity: O(n) +// Space complexity: O(n) +func TwoSumHashMap(nums []int, target int) []int { + seen := make(map[int]int) + for i, num := range nums { + if j, ok := seen[target-num]; ok { + return []int{i + 1, j + 1} + } + seen[num] = i + } + return nil +} + +// Time complexity: O(n) +// Space complexity: O(1) +func TwoSumTwoPointers(nums []int, target int) []int { + left, right := 0, len(nums)-1 + for left < right { + sum := nums[left] + nums[right] + if sum == target { + return []int{left + 1, right + 1} + } else if sum < target { + left++ + } else { + right-- + } + } + return nil +} diff --git a/2_two_pointers/2_two_sum_II_input_array_is_sorted/solution_test.go b/2_two_pointers/2_two_sum_II_input_array_is_sorted/solution_test.go new file mode 100644 index 0000000..93a2681 --- /dev/null +++ b/2_two_pointers/2_two_sum_II_input_array_is_sorted/solution_test.go @@ -0,0 +1,46 @@ +package twosumiiinputarrayissorted + +import ( + "reflect" + "testing" +) + +func TestTwoSum(t *testing.T) { + cases := []struct { + name string + fn func([]int, int) []int + nums []int + target int + expected []int + }{ + // TwoSumBruteForce + {"Brute Force - Basic", TwoSumBruteForce, []int{2, 7, 11, 15}, 9, []int{1, 2}}, + {"Brute Force - Not Found", TwoSumBruteForce, []int{1, 2, 3}, 10, nil}, + {"Brute Force - Single", TwoSumBruteForce, []int{5}, 5, nil}, + {"Brute Force - Negative Numbers", TwoSumBruteForce, []int{-3, 0, 3, 4}, 0, []int{1, 3}}, + + // TwoSumBinarySearch + {"Binary Search - Basic", TwoSumBinarySearch, []int{2, 7, 11, 15}, 9, []int{1, 2}}, + {"Binary Search - Not Found", TwoSumBinarySearch, []int{1, 2, 3}, 6, nil}, + {"Binary Search - Negative Numbers", TwoSumBinarySearch, []int{-4, -1, 1, 2, 5}, 1, []int{1, 5}}, + + // TwoSumHashMap + {"Hash Map - Basic", TwoSumHashMap, []int{2, 7, 11, 15}, 9, []int{2, 1}}, + {"Hash Map - Not Found", TwoSumHashMap, []int{1, 2, 3}, 0, nil}, + {"Hash Map - Negative", TwoSumHashMap, []int{-3, 4, 3, 90}, 0, []int{3, 1}}, + + // TwoSumTwoPointers + {"Two Pointers - Basic", TwoSumTwoPointers, []int{2, 7, 11, 15}, 9, []int{1, 2}}, + {"Two Pointers - Not Found", TwoSumTwoPointers, []int{1, 2, 3}, 7, nil}, + {"Two Pointers - Edge", TwoSumTwoPointers, []int{-10, -3, 0, 1, 6}, -3, []int{2, 3}}, + } + + for _, c := range cases { + t.Run(c.name, func(t *testing.T) { + got := c.fn(c.nums, c.target) + if !reflect.DeepEqual(got, c.expected) { + t.Errorf("%s failed: expected %v, got %v", c.name, c.expected, got) + } + }) + } +}