Repository: vaskoz/dailycodingproblem-go (go)

Issue:
Day 198
Good morning! Here's your coding interview problem for today.

This problem was asked by Google.

Given a set of distinct positive integers, find the largest subset such that every pair of elements in the subset `(i, j)` satisfies either `i % j = 0` or `j % i = 0`.

For example, given the set `[3, 5, 10, 20, 21]`, you should return `[5, 10, 20]`. Given `[1, 3, 6, 24]`, return `[1, 3, 6, 24]`.

Pull request description:
Day198
Resolves #408.

Patch:
diff --git a/README.md b/README.md
index d334027..ce37247 100644
--- a/README.md
+++ b/README.md
@@ -207,5 +207,6 @@ problems from
 * [Day 195](https://github.com/vaskoz/dailycodingproblem-go/issues/401)
 * [Day 196](https://github.com/vaskoz/dailycodingproblem-go/issues/404)
 * [Day 197](https://github.com/vaskoz/dailycodingproblem-go/issues/405)
+* [Day 198](https://github.com/vaskoz/dailycodingproblem-go/issues/408)
 * [Day 199](https://github.com/vaskoz/dailycodingproblem-go/issues/409)
 * [Day 200](https://github.com/vaskoz/dailycodingproblem-go/issues/411)
diff --git a/day198/problem.go b/day198/problem.go
new file mode 100644
index 0000000..d050ac7
--- /dev/null
+++ b/day198/problem.go
@@ -0,0 +1,44 @@
+package day198
+
+import (
+	"sort"
+)
+
+// LargestSubsetPairs finds the largest subset
+// such that every pair of elements in the subset (i, j)
+// satisfies either i % j = 0 or j % i = 0.
+// Runs in O(N^2) time
+func LargestSubsetPairs(nums []int) []int {
+	sort.Slice(nums, func(i, j int) bool {
+		return nums[i] < nums[j]
+	})
+	dp := make([]int, len(nums))
+	dp[len(dp)-1] = 1
+	var largestI, largestVal int
+	for i := len(dp) - 2; i >= 0; i-- {
+		m := 0
+		for j := i + 1; j < len(dp); j++ {
+			if nums[j]%nums[i] == 0 {
+				m = max(m, dp[j])
+			}
+		}
+		if dp[i] = 1 + m; dp[i] > largestVal {
+			largestI = i
+			largestVal = dp[i]
+		}
+	}
+	result := make([]int, 0, largestVal)
+	for i := largestI; i < len(nums); i++ {
+		if nums[i]%nums[largestI] == 0 {
+			result = append(result, nums[i])
+		}
+	}
+	return result
+}
+
+func max(a, b int) int {
+	if a > b {
+		return a
+	}
+	return b
+}


Test patch:
diff --git a/day198/problem_test.go b/day198/problem_test.go
new file mode 100644
index 0000000..cdb3cd9
--- /dev/null
+++ b/day198/problem_test.go
@@ -0,0 +1,32 @@
+package day198
+
+import (
+	"reflect"
+	"testing"
+)
+
+var testcases = []struct {
+	nums, subset []int
+}{
+	{[]int{3, 5, 10, 20, 21}, []int{5, 10, 20}},
+	{[]int{1, 3, 6, 24}, []int{1, 3, 6, 24}},
+}
+
+func TestLargestSubsetPairs(t *testing.T) {
+	t.Parallel()
+	for _, tc := range testcases {
+		input := append([]int{}, tc.nums...)
+		if result := LargestSubsetPairs(input); !reflect.DeepEqual(result, tc.subset) {
+			t.Errorf("Expected %v, got %v", tc.subset, result)
+		}
+	}
+}
+
+func BenchmarkLargestSubsetPairs(b *testing.B) {
+	for i := 0; i < b.N; i++ {
+		for _, tc := range testcases {
+			input := append([]int{}, tc.nums...)
+			LargestSubsetPairs(input)
+		}
+	}
+}