Amazon Interview Question

Summation problem. Medium level leetcode