You are given an integer 'n'. Calculate the sum of even and odd digits of 'n', represented in decimals.
Even and odd do not refer to the position of the digit but to the polarity of the digit.
Return the answer in the form of an array of size 2, such that the 0th index represents the even sum and the 1st index represents the odd sum.
Input: 'n' = 1986.
Output: [14, 10]
Explanation: Even digits are 8 & 6, and odd digits are 1 & 9. The sum of even digits = 14, and the sum of odd digits = 10.
The first and only line of input contains the integer 'n'.
Output format :
The output consists of a single line containing two space-separated integers denoting the sum of even digits and the sum of odd digits, respectively.
Note:
You do not need to print anything, it has already been taken care of. Just implement the given function.
1234
6 4
For the given input, the even digits are 2 and 4, and if we take the sum of these digits, we get 6 (2 + 4), and similarly, if we look at the odd digits, they are 1 and 3, which form a sum of 4 (1 + 3).
552245
8 15
For the given input, the even digits are 2, 2, and 4, and if we take the sum of these digits, we get 8 (2 + 2 + 4), and similarly, if we look at the odd digits, they are, 5, 5 and 5 which form a sum of 15(5 + 5 + 5).
The expected time complexity is O(log10(n)).
0 <= 'n' <= 10 ^ 9
Time Limit: 1 sec.
Try using basic mathematical operations.
Use the modulo operation to find the digits of numbers.
O(log10(n)), where ‘n' is the input integer.
Since we are iterating through all digits of the given input.
Therefore, the overall time complexity will be O(log10(n)).
O(1)
This approach uses constant space.
Therefore, the overall space complexity will be O(1).