Repository: vaskoz/dailycodingproblem-go (go)

Issue:
Day 322
Good morning! Here's your coding interview problem for today.

This problem was asked by Flipkart.

Starting from `0` on a number line, you would like to make a series of jumps that lead to the integer `N`.

On the `i-th` jump, you may move exactly `i` places to the left or right.

Find a path with the fewest number of jumps required to get from `0` to `N`.

Pull request description:
Day322
Resolves #654.

Patch:
diff --git a/README.md b/README.md
index ff0bd53..b955c47 100644
--- a/README.md
+++ b/README.md
@@ -325,3 +325,4 @@ problems from
 * [Day 317](https://github.com/vaskoz/dailycodingproblem-go/issues/645)
 * [Day 320](https://github.com/vaskoz/dailycodingproblem-go/issues/650)
 * [Day 321](https://github.com/vaskoz/dailycodingproblem-go/issues/651)
+* [Day 322](https://github.com/vaskoz/dailycodingproblem-go/issues/654)
diff --git a/day322/problem.go b/day322/problem.go
new file mode 100644
index 0000000..ffb32d1
--- /dev/null
+++ b/day322/problem.go
@@ -0,0 +1,22 @@
+package day322
+
+// NumberOfJumps returns the number of jumps to reach
+// N from 0.
+// Each jump length is of length "i".
+// Runs in O(answer) time and O(1) space.
+func NumberOfJumps(n int) int {
+	if n < 0 {
+		n = -n
+	}
+	var result int
+	for arithmeticSum(result) < n || (arithmeticSum(result)-n)%2 == 1 {
+		result++
+	}
+	return result
+}
+
+// arithmeticSum is:
+// https://en.wikipedia.org/wiki/Arithmetic_progression#Sum
+func arithmeticSum(n int) int {
+	return (n * (n + 1)) / 2
+}


Test patch:
diff --git a/day322/problem_test.go b/day322/problem_test.go
new file mode 100644
index 0000000..5c6238e
--- /dev/null
+++ b/day322/problem_test.go
@@ -0,0 +1,35 @@
+package day322
+
+import "testing"
+
+// nolint
+var testcases = []struct {
+	n, jumps int
+}{
+	{9, 5},
+	{1, 1},
+	{0, 0},
+	{2, 3},
+	{3, 2},
+	{-3, 2},
+	{-2, 3},
+	{-1, 1},
+	{-9, 5},
+}
+
+func TestNumberOfJumps(t *testing.T) {
+	t.Parallel()
+	for _, tc := range testcases {
+		if jumps := NumberOfJumps(tc.n); jumps != tc.jumps {
+			t.Errorf("Given %v, expected %v, got %v", tc.n, tc.jumps, jumps)
+		}
+	}
+}
+
+func BenchmarkNumberOfJumps(b *testing.B) {
+	for i := 0; i < b.N; i++ {
+		for _, tc := range testcases {
+			NumberOfJumps(tc.n)
+		}
+	}
+}