Compute Alternating Sum - Solution & Explanation
Problem Statement
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:
- Elements at even indices are
nums[0] = 1andnums[2] = 5because 0 and 2 are even numbers. - Elements at odd indices are
nums[1] = 3andnums[3] = 7because 1 and 3 are odd numbers. - The alternating sum is
nums[0] - nums[1] + nums[2] - nums[3] = 1 - 3 + 5 - 7 = -4.
Example 2:
Input: nums = [100]
Output: 100
Explanation:
- The only element at even indices is
nums[0] = 100because 0 is an even number. - There are no elements on odd indices.
- The alternating sum is
nums[0] = 100.
Constraints:
1 <= nums.length <= 1001 <= nums[i] <= 100
Approach Overview
Problem Overview: You are given an array of integers and need to compute its alternating sum. The pattern is simple: add the first element, subtract the second, add the third, subtract the fourth, and continue this pattern until the end of the array.
Approach 1: Index Parity Simulation (O(n) time, O(1) space)
The most direct way is to iterate through the array once and decide whether to add or subtract each value based on its index. If the index is even, add the value to the running total; if the index is odd, subtract it. This works because the alternating pattern directly maps to index parity. The algorithm performs a single pass over the array and uses only one accumulator variable, making it both time and memory efficient. This approach fits naturally with problems involving arrays and straightforward simulation.
Approach 2: Sign Toggle Simulation (O(n) time, O(1) space)
Another clean implementation uses a sign variable that flips after each element. Start with sign = 1 and multiply the current element by the sign before adding it to the result. After processing an element, flip the sign using sign *= -1. This avoids checking index parity and keeps the loop logic minimal. The method still processes each element exactly once, so the time complexity remains O(n) with constant O(1) space.
Recommended for interviews: Interviewers expect the linear simulation solution. Showing the index-parity version demonstrates that you recognize the alternating pattern quickly. The sign-toggle variation shows cleaner reasoning and often results in simpler code. Both run in O(n) time with O(1) space, which is optimal for this problem.
Solution
We can directly traverse the array nums. For each index i, if i is even, we add nums[i] to the answer; otherwise, we subtract nums[i] from the answer.
Finally, we return the answer.
The time complexity is O(n), where n is the length of array nums. The space complexity is O(1).
Code
Python
Java
C++
Go
TypeScript
Detailed Complexity Analysis
| Approach | Time | Space | When to Use |
|---|---|---|---|
| Index Parity Simulation | O(n) | O(1) | Best when implementing directly from the definition of alternating addition and subtraction using index parity. |
| Sign Toggle Simulation | O(n) | O(1) | Cleaner implementation when you want minimal conditional checks inside the loop. |
Video Solution
3701. Compute Alternating Sum (Leetcode Easy) • Programming Live with Larry • 359 views views
Watch 9 more video solutions →Frequently Asked Questions
Is Compute Alternating Sum easy or hard?
Compute Alternating Sum Python/Java solution
How to solve Compute Alternating Sum in O(n)?
What is the best approach for Compute Alternating Sum?
Is Compute Alternating Sum asked at Google/Amazon/Meta?
What data structure is used in Compute Alternating Sum?
What is the time complexity of Compute Alternating Sum?
Ready to solve this problem?
Practice Compute Alternating Sum with our built-in code editor and test cases.
Practice on FleetCodeProblem Info
Table of Contents
Practice this problem
Open in Editor