Amazon interview question

Summation problem. Medium level leetcode