Given a numeric string s, determine whether the sum of digits at odd positions is equal to the sum of digits at even positions. Positions are counted from 1 starting from the leftmost digit.
Examples:Â
Input: s = "132"
Output: true
Explanation: The sum of digits at odd places is 1 + 2 = 3. Similarly the sum of digits at even places is 3. Since they are equal, the answer is 1.Input: s = "123"
Output: false
Explanation: The sum of digits at odd places is 1 + 3 = 4. The sum of digits at even places is 2. Since, the sums are not equal, Thus answer is 0.
Using Single Traversal with Running Sums - O(n) Time and O(1) Space
The idea is to traverse the string maintaining separate sums of digits at odd and even positions. Since the parity of each position can be determined from its index, we update the corresponding sum during traversal. Finally, compare the two sums and return the result.
Working of Approach:
- Initialize two variables to store the sums of odd and even positions.
- Traverse the string exactly once.
- Use the index parity (i % 2) to identify odd and even positions.
- Update the corresponding running sum for each digit.
- Compare the two sums and return the result.
Let us understand with an example:
Input: s = "132"
- Initialize oddSum = 0 and evenSum = 0.
- At index 0 (odd position), digit 1 is added to oddSum -> oddSum = 1.
- At index 1 (even position), digit 3 is added to evenSum -> evenSum = 3.
- At index 2 (odd position), digit 2 is added to oddSum -> oddSum = 3.
- Since oddSum == evenSum (3 == 3), the function returns true.
#include <iostream>
#include <string>
using namespace std;
bool checkDigitSums(string s)
{
int oddSum = 0, evenSum = 0;
// Traverse the string and compute sums for odd and even positions
for (int i = 0; i < (int)s.size(); i++)
{
int digit = s[i] - '0';
// Add digit to oddSum if position is odd (1-based index)
if (i % 2 == 0)
oddSum += digit;
else
evenSum += digit;
}
// Return true if both sums are equal
return oddSum == evenSum;
}
int main()
{
string s = "132";
cout << (checkDigitSums(s) ? "true" : "false");
return 0;
}
#include <stdio.h>
#include <string.h>
int checkDigitSums(char *s)
{
int oddSum = 0, evenSum = 0;
// Traverse the string and compute sums for odd and even positions
for (int i = 0; i < strlen(s); i++)
{
int digit = s[i] - '0';
// Add digit to oddSum if position is odd (1-based index)
if (i % 2 == 0)
oddSum += digit;
else
evenSum += digit;
}
// Return true if both sums are equal
return oddSum == evenSum;
}
int main()
{
char s[] = "132";
printf(checkDigitSums(s)? "true" : "false");
return 0;
}
public class Main {
public static boolean checkDigitSums(String s) {
int oddSum = 0, evenSum = 0;
// Traverse the string and compute sums for odd and even positions
for (int i = 0; i < s.length(); i++) {
int digit = s.charAt(i) - '0';
// Add digit to oddSum if position is odd (1-based index)
if (i % 2 == 0)
oddSum += digit;
else
evenSum += digit;
}
// Return true if both sums are equal
return oddSum == evenSum;
}
public static void main(String[] args) {
String s = "132";
System.out.println(checkDigitSums(s)? "true" : "false");
}
}
def checkDigitSums(s):
oddSum = 0
evenSum = 0
# Traverse the string and compute sums for odd and even positions
for i in range(len(s)):
digit = int(s[i])
# Add digit to oddSum if position is odd (1-based index)
if i % 2 == 0:
oddSum += digit
else:
evenSum += digit
# Return true if both sums are equal
return oddSum == evenSum
s = "132"
print("true" if checkDigitSums(s) else "false")
using System;
class Program
{
static bool checkDigitSums(string s)
{
int oddSum = 0, evenSum = 0;
// Traverse the string and compute sums for odd and even positions
for (int i = 0; i < s.Length; i++)
{
int digit = s[i] - '0';
// Add digit to oddSum if position is odd (1-based index)
if (i % 2 == 0)
oddSum += digit;
else
evenSum += digit;
}
// Return true if both sums are equal
return oddSum == evenSum;
}
static void Main()
{
string s = "132";
Console.WriteLine(checkDigitSums(s)? "true" : "false");
}
}
function checkDigitSums(s) {
let oddSum = 0, evenSum = 0;
// Traverse the string and compute sums for odd and even positions
for (let i = 0; i < s.length; i++) {
let digit = parseInt(s[i]);
// Add digit to oddSum if position is odd (1-based index)
if (i % 2 === 0)
oddSum += digit;
else
evenSum += digit;
}
// Return true if both sums are equal
return oddSum === evenSum;
}
let s = "132";
console.log(checkDigitSums(s)? "true" : "false");
Output
true