Logo
IconChallenge
Icon
IconProblem
IconSolutions
IconSubmissions

Compute Alternating Sum

XPChallenge Points: 10
levelLevel: Easy

You are given an array of integers nums.
The alternating sum of the array is calculated by adding the elements at even indices and subtracting the elements at odd indices.
Formally,
Alternating Sum=nums[0]−nums[1]+nums[2]−nums[3]+...
Your task is to return the integer result of this alternating sum.

Example 1:

Input: nums = [15, 3, 9]

Output: 21

Example 2:

Input: nums = [2, 4, 6, 8, 10]

Output: 6

to Continue
like
dislike

Accepted:

Submission:

IconReport an issue
Icon
IconCode
IconYou need toto run or submitYou need toto run or submit
IconTest Case
IconTest Result