3701. Compute Alternating Sum Easy

@problem@discussion
#Array#Simulation



1/**
2 * [3701] Compute Alternating Sum
3 *
4 * You are given an integer array nums.
5 * 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]...
6 * Return an integer denoting the alternating sum of nums.
7 *  
8 * <strong class="example">Example 1:
9 * <div class="example-block">
10 * Input: <span class="example-io">nums = [1,3,5,7]</span>
11 * Output: <span class="example-io">-4</span>
12 * Explanation:
13 * 
14 * 	Elements at even indices are nums[0] = 1 and nums[2] = 5 because 0 and 2 are even numbers.
15 * 	Elements at odd indices are nums[1] = 3 and nums[3] = 7 because 1 and 3 are odd numbers.
16 * 	The alternating sum is nums[0] - nums[1] + nums[2] - nums[3] = 1 - 3 + 5 - 7 = -4.
17 * </div>
18 * <strong class="example">Example 2:
19 * <div class="example-block">
20 * Input: <span class="example-io">nums = [100]</span>
21 * Output: <span class="example-io">100</span>
22 * Explanation:
23 * 
24 * 	The only element at even indices is nums[0] = 100 because 0 is an even number.
25 * 	There are no elements on odd indices.
26 * 	The alternating sum is nums[0] = 100.
27 * </div>
28 *  
29 * Constraints:
30 * 
31 * 	1 <= nums.length <= 100
32 * 	1 <= nums[i] <= 100
33 * 
34 */
35pub struct Solution {}
36
37// problem: https://leetcode.com/problems/compute-alternating-sum/
38// discuss: https://leetcode.com/problems/compute-alternating-sum/discuss/?currentPage=1&orderBy=most_votes&query=
39
40// submission codes start here
41
42impl Solution {
43    pub fn alternating_sum(nums: Vec<i32>) -> i32 {
44        0
45    }
46}
47
48// submission codes end
49
50#[cfg(test)]
51mod tests {
52    use super::*;
53
54    #[test]
55    fn test_3701() {
56    }
57}
58

Back
© 2026 bowen.ge All Rights Reserved.