814. Find Sum of AP Series

Given an Arithmetic Progression (A.P.), find the sum of its first n terms.

You are given:

  • a → the first term of the A.P.
  • d → the common difference between terms.
  • n → the number of terms.

Example 1:

Input: n = 4, a = 2, d = 2

Output: 20

Explanation:

Series = 2 + 4 + 6 + 8 → Sum = 20

Example 2:

Input: n = 8, a = -2, d = 5

Output: 124

Explanation:

Series = -2 + 3 + 8 + 13 + 18 + 23 + 28 + 33 → Sum = 124

Now Your Turn!

Pick the correct output for the given input

Input: n = 1, a = 100, d = 0

Still unsure what the problem is asking ?

Let’s go through a few more examples, step by step, to make it clearer.

Constraints:

  • āˆ’10⁓ ≤ a, d ≤ 10⁓
  • 1 ≤ n ≤ 10⁓
  • The final sum will not exceed 10⁹.
0
class Solution {
public:
int sumOfAP(int n, int a, int d) {
// Your code goes here
}
};
Test Case

Input:

A
D
N