Description
Solutions
Difference Between Sums of Positions
🤘 INTERN

note - see the Problem Source below for the original problem description ;)

Imagine you’re given a list of numbers, and your task is to calculate the difference between two specific sums. First, you’ll sum up all the numbers at even positions (remember, the list is 0-based) that fall between -100 and 100, inclusive. Then, you’ll do the same for numbers at odd positions that also lie within this range. Finally, you’ll subtract the odd-positioned sum from the even-positioned sum to get your answer. Just make sure your solution is efficient, ideally not taking more than O(n) time, though a slightly slower approach will also work fine.

Example 1:

Input:  numbers = [101, 3, 4, 359, 2, 5]
Output: -2
Explanation:
Let’s break it down: you want to find the sums of certain elements from your list based on their positions and values. For the even-positioned elements, you only consider those at positions 2 and 4 because their values are within the range of [-100, 100]. In this case, numbers[2] is 4 and numbers[4] is 2, giving you a total of 6. numbers[0] = 101 is excluded because it’s outside the range. Next, for the odd-positioned elements, you check numbers[1] and numbers[5], which are 3 and 5, respectively, totaling 8. Here, numbers[3] = 359 is filtered out because it doesn’t meet the criteria. Finally, you subtract the sum of the odd-positioned elements (8) from the sum of the even-positioned elements (6), resulting in 6 - 8 = -2. Thus, the expected output is -2.

Example 2:

Input:  numbers = [-2, 234, 100, 99, 540, -1]
Output: 0
Explanation:
For the even-positioned elements, you check the values at positions 0 and 2. Here, numbers[0] = -2 and numbers[2] = 100, which adds up to 98. numbers[4] = 540 is excluded since it exceeds the range of [-100, 100]. Next, for the odd-positioned elements, you look at numbers[1] and numbers[5]. In this case, numbers[3] = 99 and numbers[5] = -1, giving a total of 98 as well. numbers[1] = 234 is filtered out because it is outside the specified range. Finally, you subtract the sum of the odd-positioned elements (98) from the sum of the even-positioned elements (98), resulting in 98 - 98 = 0. Therefore, the expected output is 0.

Example 3:

Input:  numbers = [-9]
Output: -9
Explanation:
In your list, the only even-positioned element is at position 0, which is -9. This value is within the range of [-100, 100], so it contributes to the sum, making the total for even positions -9. For the odd-positioned elements, there are none present, resulting in a sum of 0. Now, when you subtract the sum of the odd-positioned elements (0) from the sum of the even-positioned elements (-9), you get -9 - 0 = -9. Thus, the expected output is -9.
Constraints:
    • 1 ≤ numbers.length ≤ 10^3
    • -10^3 ≤ numbers[i] ≤ 10^3
Thumbnail 0
Thumbnail 1
Testcase

Result
Case 1

input:

output: