diff --git a/.gitignore b/.gitignore index a1c09e19..21b5bc82 100644 --- a/.gitignore +++ b/.gitignore @@ -10,3 +10,5 @@ Thumbs.db # binary files *.out *.class +*.idea + diff --git a/algorithms/Go/README.md b/algorithms/Go/README.md index ffa0542a..bbc1737a 100644 --- a/algorithms/Go/README.md +++ b/algorithms/Go/README.md @@ -1,5 +1,9 @@ # Go +## Arrays +1. [Maximum subarray sum (Kadane's Algorithm)](arrays/maximum-subarray-sum.go) +2. [Two Sum](arrays/two-sum.go) + ## Scheduling 1. [Interval Scheduling](scheduling/interval-scheduling.go) diff --git a/algorithms/Go/arrays/maximum-subarray-sum.go b/algorithms/Go/arrays/maximum-subarray-sum.go new file mode 100644 index 00000000..01d5b239 --- /dev/null +++ b/algorithms/Go/arrays/maximum-subarray-sum.go @@ -0,0 +1,38 @@ +/* + Package arrays maximumSubarraySum problem + Input: An array of integers + Output: Subarray with maximum sum + The Strategy: Keep track of the previous encounter sum and the current element. + If the current element is largest than the previous sum + current element + then throw the previous sum and start with the current element as the largest sum. + Algorithm Type: Kadane's Algorithm + Time Complexity: O(n) + Dry Run : + [-2,1,-3,4,-1,2,1,-5,4] + initial := largestSum = -2 , currentSum = -2 + i=1 -> currentSum = max(1, (-2 + 1 )) = 1 , largestSum = max(-2, 1) = 1 + i=2 -> currentSum = max(-3, (1 + -3 )) = -2, largestSum = max(1, -2) = 1 + i=3 -> currentSum = max(4, (-2 + 4 )) = 4 , largestSum = max(1, 4) = 4 + i=4 -> currentSum = max(-1, (4 + -1 )) = 3 , largestSum = max(4, 3) = 4 + i=5 -> currentSum = max(2, (3 + 2 )) = 5 , largestSum = max(4, 5) = 5 + i=6 -> currentSum = max(1, (5 + 1 )) = 6 , largestSum = max(5, 6) = 6 + i=7 -> currentSum = max(-5, (6 + -5 )) = 1 , largestSum = max(6, 1) = 6 + i=8 -> currentSum = max(4, (1 + 4 )) = 5 , largestSum = max(6, 5) = 6 +*/ +package arrays + +import ( + "math" +) + +// To find the maximum subarray sum. +func maximumSubarraySum(nums []int) int { + maximumSum := nums[0] + currentSum := maximumSum + for i := 1; i < len(nums); i++ { + currentSum = int(math.Max(float64(nums[i]), float64(nums[i]+currentSum))) + maximumSum = int(math.Max(float64(maximumSum), float64(currentSum))) + } + + return maximumSum +} diff --git a/algorithms/Go/arrays/maximum_subarray_sum_test.go b/algorithms/Go/arrays/maximum_subarray_sum_test.go new file mode 100644 index 00000000..93feb57b --- /dev/null +++ b/algorithms/Go/arrays/maximum_subarray_sum_test.go @@ -0,0 +1,11 @@ +package arrays + +import "testing" + +func TestMaximumSubarraySum(t *testing.T) { + sampleArray := []int{-2, 1, -3, 4, -1, 2, 1, -5, 4} + want := 6 + if got := maximumSubarraySum(sampleArray); want != got { + t.Errorf("want %v , got %v", want, got) + } +} diff --git a/algorithms/Go/arrays/two-sum.go b/algorithms/Go/arrays/two-sum.go new file mode 100644 index 00000000..f86cd2ec --- /dev/null +++ b/algorithms/Go/arrays/two-sum.go @@ -0,0 +1,46 @@ +/* + Problem Statement : Given an array of integers nums and an integer target, return indices of + the two numbers such that they add up to target. + You may assume that each input would have exactly one solution, and you may + not use the same element twice. + + Input: An array of integers and a target (int) + Output: array of indexes of len(2) with sum of element at that index equal to target or nil +*/ + +package arrays + +/* + Using Brute Force : For every element check for another element if it exist in the array such that sum of + both the element is equals to the target + Time Complexity : O(n^2) +*/ +func twoSum(arr []int, target int) []int { + for i := 0; i < len(arr); i++ { + for j := i + 1; j < len(arr); j++ { + if arr[i]+arr[j] == target { + return []int{i, j} + } + } + } + return nil +} + +/* + Using Map := While traversing every element add the element as key and its position as its value in a map + Check the required value (i.e target - arr[i]) in the map + If the map contains the required value then we have two elements with the required sum and + return the positions. + Time Complexity : O(n) +*/ +func twoSumEfficient(arr []int, target int) []int { + m := make(map[int]int) + for i := 0; i < len(arr); i++ { + compliment := target - arr[i] + if _, ok := m[compliment]; ok { + return []int{m[compliment], i} + } + m[arr[i]] = i + } + return []int{} +} diff --git a/algorithms/Go/arrays/two_sum_test.go b/algorithms/Go/arrays/two_sum_test.go new file mode 100644 index 00000000..d0b92c8c --- /dev/null +++ b/algorithms/Go/arrays/two_sum_test.go @@ -0,0 +1,35 @@ +package arrays + +import ( + "testing" +) + +func TestTwoSum(t *testing.T) { + + arr := []int{2, 7, 11, 15, 1, 13, 18} + elem := 26 + + want := []int{2, 3} + + if got := twoSum(arr, elem); got == nil || got[0] != want[0] && got[1] != want[1] { + if got == nil { + t.Errorf("got no indexes") + } + t.Errorf("want indexes [%d,%d] and got indexes [%d,%d]", want[0], want[1], got[0], got[1]) + } +} + +func TestTwoSumEfficient(t *testing.T) { + + arr := []int{2, 7, 11, 15, 1, 13, 18} + elem := 26 + + want := []int{2, 3} + + if got := twoSumEfficient(arr, elem); got == nil || got[0] != want[0] && got[1] != want[1] { + if got == nil { + t.Errorf("got no indexes") + } + t.Errorf("want indexes [%d,%d] and got indexes [%d,%d]", want[0], want[1], got[0], got[1]) + } +}