Easy
You are given an integer array nums
.
The alternating sum of nums
is the value obtained by adding elements at even indices and subtracting elements at odd indices. That is, nums[0] - nums[1] + nums[2] - nums[3]...
Return an integer denoting the alternating sum of nums
.
Example 1:
Input: nums = [1,3,5,7]
Output: -4
Explanation:
nums[0] = 1
and nums[2] = 5
because 0 and 2 are even numbers.nums[1] = 3
and nums[3] = 7
because 1 and 3 are odd numbers.nums[0] - nums[1] + nums[2] - nums[3] = 1 - 3 + 5 - 7 = -4
.Example 2:
Input: nums = [100]
Output: 100
Explanation:
nums[0] = 100
because 0 is an even number.nums[0] = 100
.Constraints:
1 <= nums.length <= 100
1 <= nums[i] <= 100
class Solution {
fun alternatingSum(nums: IntArray): Int {
var sum = 0
for (i in nums.indices) {
val num = nums[i]
if (i % 2 == 0) {
sum += num
} else {
sum -= num
}
}
return sum
}
}