Repository: vaskoz/dailycodingproblem-go (go)

Issue:
Day 338
Good morning! Here's your coding interview problem for today.

This problem was asked by Facebook.

Given an integer `n`, find the next biggest integer with the same number of 1-bits on. For example, given the number `6` (`0110` in binary), return `9` (`1001`).

Pull request description:
Day338
Resolves #675 

Patch:
diff --git a/README.md b/README.md
index ceba8f4..2ab56a5 100644
--- a/README.md
+++ b/README.md
@@ -329,5 +329,6 @@ problems from
 * [Day 323](https://github.com/vaskoz/dailycodingproblem-go/issues/656)
 * [Day 324](https://github.com/vaskoz/dailycodingproblem-go/issues/658)
 * [Day 325](https://github.com/vaskoz/dailycodingproblem-go/issues/660)
+* [Day 338](https://github.com/vaskoz/dailycodingproblem-go/issues/675)
 * [Day 339](https://github.com/vaskoz/dailycodingproblem-go/issues/676)
 * [Day 340](https://github.com/vaskoz/dailycodingproblem-go/issues/679)
diff --git a/day338/problem.go b/day338/problem.go
new file mode 100644
index 0000000..19d743d
--- /dev/null
+++ b/day338/problem.go
@@ -0,0 +1,16 @@
+package day338
+
+// NextIntSameSetBits returns the next greater
+// integer with the same set bits.
+func NextIntSameSetBits(n int) int {
+	res := 0
+	if n != 0 {
+		rightMostBitInt := n & -n
+		nextHigherSetBitInt := n + rightMostBitInt
+		d := n ^ nextHigherSetBitInt
+		d /= rightMostBitInt
+		d >>= 2
+		res = nextHigherSetBitInt | d
+	}
+	return res
+}


Test patch:
diff --git a/day338/problem_test.go b/day338/problem_test.go
new file mode 100644
index 0000000..043a99c
--- /dev/null
+++ b/day338/problem_test.go
@@ -0,0 +1,28 @@
+package day338
+
+import "testing"
+
+// nolint
+var testcases = []struct {
+	n, next int
+}{
+	{6, 9},
+	{156, 163},
+}
+
+func TestNextIntSameSetBits(t *testing.T) {
+	t.Parallel()
+	for _, tc := range testcases {
+		if next := NextIntSameSetBits(tc.n); next != tc.next {
+			t.Errorf("Expected %v, got %v", tc.next, next)
+		}
+	}
+}
+
+func BenchmarkNextIntSameSetBits(b *testing.B) {
+	for i := 0; i < b.N; i++ {
+		for _, tc := range testcases {
+			NextIntSameSetBits(tc.n)
+		}
+	}
+}