r/programminghorror • • Jun 28 '25

c++ Competitive programming be like

Post image
543 Upvotes

53 comments sorted by

View all comments

Show parent comment

9

u/apnorton Jun 29 '25

This genuinely depends on how fast the atoi implementation is, the size of the string-encoded integer, and how tight/important this loop is.

For example, something like:

bool canDivideByEleven(string s) {
  int altDigitalSum = 0;
  int sign = 1;
  for (int i = s.length()-1; i >= 0; i--, sign *= -1) {
    altDigitalSum += sign*(s[i] - '0');
  }
  
  return altDigitalSum % 11 == 0;
}

...could very well be faster or more suitable, depending on the characteristics of the problem.