EasyFoundational
Calculate the Sum of Digits at Even and Odd Positions
- Time limit
- 1 s
- Memory limit
- 128 MB
- Estimated
- 15 min
Description
Given a non-negative integer n, calculate two sums based on the positions of its digits.
Read the decimal digits from left to right. The leftmost digit is at position 1. Add digits at odd-numbered positions to the first sum, and digits at even-numbered positions to the second sum.
Return an integer array containing exactly two values: [oddPositionSum, evenPositionSum].
The number 0 has one digit at position 1, so its result is [0, 0]. Leading zeroes are not part of the standard decimal representation.
Examples:
n = 12345returns[9, 6]because positions 1, 3, and 5 contain1 + 3 + 5 = 9, while positions 2 and 4 contain2 + 4 = 6.n = 908returns[17, 0].n = 0returns[0, 0].
Implement sumDigitsByPosition(n).
Constraints
0 <= n <= 9007199254740991 Return an array containing exactly two integers.
Sample cases
3Sample 1
Input
n = 12345
Expected output
[9,6]
Sample 2
Input
n = 908
Expected output
[17,0]
Sample 3
Skills and languages
Concepts
- Number Bases and Digit Manipulation
Languages
- Python 3
Solve this problem
Create a free account to write, run and submit your solution.
- Write Python or Java in the browser, with your draft saved as you type
- Run your code on the examples as often as you like
- Submit to a sandboxed judge and see exactly which case failed
- Ask the AI mentor for a hint without being handed the answer