easyDigit ProblemsJava

Find the Difference Between the Sums of Odd and Even Positioned Digits

Compute the difference between digits at odd positions and digits at even positions, counting from the right.

Quick Answer

Number the digit positions from the right starting at 1. Walk the digits with number % 10, tracking a position counter. Add digits at odd positions to one running sum and digits at even positions to another. Return sumOdd minus sumEven. This alternating sum is the core of the divisibility-by-11 test.

Problem Statement

Given a non-negative integer, number its digit positions from the right starting at 1. Sum the digits sitting at odd positions and, separately, the digits at even positions. Return sumOdd - sumEven. Do not convert the number to a String; use arithmetic only.

For example, in 12345 the odd-position digits (from the right) are 5, 3, 1 and the even-position digits are 4, 2, giving 9 - 6 = 3.

Input: A single non-negative integer n.

Output: The integer sumOdd - sumEven, where positions are counted from the right starting at 1.

Examples

Example 1
Input:  12345
Output: 3

Odd positions from the right hold 5, 3, 1 (sum 9); even positions hold 4, 2 (sum 6); 9 - 6 = 3.

Example 2
Input:  4813
Output: 6

Odd positions hold 3 and 8 (sum 11); even positions hold 1 and 4 (sum 5); 11 - 5 = 6.

Constraints

  • 0 <= n <= 2,147,483,647 (fits in a 32-bit int)
  • No String conversion; arithmetic only

Think Before You Code

Reveal the questions to ask yourself first
  • Which end do you start counting positions from, and how does that match modulo extraction?
  • How do you tell an odd position from an even one as you go?
  • What does this alternating difference have to do with divisibility by 11?

Hints

Open them one at a time — try after each before revealing the next.

Hint 1
Extracting with `% 10` gives the rightmost digit first, which is exactly position 1.
Hint 2
Keep a position counter starting at 1; use `position % 2` to decide which sum a digit joins.
Hint 3
Increment the position after each digit, then return `sumOdd - sumEven` at the end.

Approach

Reveal the step-by-step approach

Modulo extraction hands you the rightmost digit first, so the first digit you see is position 1 — the natural starting point.

  1. Set pos = 1, sumOdd = 0, sumEven = 0.
  2. While n is not 0:
    • digit = n % 10.
    • If pos is odd, add digit to sumOdd; otherwise add it to sumEven.
    • pos = pos + 1.
    • n = n / 10.
  3. Return sumOdd - sumEven.

This alternating sum is the quantity checked by the divisibility rule for 11.

Dry Run

Walk through the example step by step

Computing the difference for n = 12345:

step | n     | digit | pos | odd? | sumOdd | sumEven
-----+-------+-------+-----+------+--------+--------
1    | 12345 | 5     | 1   | yes  | 5      | 0
2    | 1234  | 4     | 2   | no   | 5      | 4
3    | 123   | 3     | 3   | yes  | 8      | 4
4    | 12    | 2     | 4   | no   | 8      | 6
5    | 1     | 1     | 5   | yes  | 9      | 6
end  | 0     | —     | —   | —    | 9      | 6   -> 9 - 6 = 3

Solution

Reveal the full Java solution
public class OddEvenPositionDiff {
    public static int difference(int n) {
        int pos = 1;
        int sumOdd = 0;
        int sumEven = 0;
        while (n != 0) {
            int digit = n % 10;
            if (pos % 2 == 1) {
                sumOdd += digit;
            } else {
                sumEven += digit;
            }
            pos++;
            n /= 10;
        }
        return sumOdd - sumEven;
    }

    public static void main(String[] args) {
        System.out.println(difference(12345)); // 3
        System.out.println(difference(4813));  // 6
    }
}

Because % 10 starts at the rightmost digit, the position counter aligns cleanly with the standard right-to-left numbering. The returned difference can be negative or zero, and a value divisible by 11 (including 0) signals the whole number is divisible by 11.

Time: O(d) where d is the number of digitsSpace: O(1)

Common Mistakes

  • Starting the position counter at 0, which flips the odd and even groups.
  • Taking the absolute value of the difference when the sign is part of the answer.

Edge Cases to Test

  • A single-digit number gives sumOdd = the digit and sumEven = 0.
  • n = 0 returns 0.
  • Numbers divisible by 11, like 121, produce a difference that is a multiple of 11.

Interview Follow-Ups

  • How would you use this result to test divisibility by 11 directly?
  • How would the answer change if positions were counted from the left instead?

Practising for Java interviews?

CodeBegun's Java Full Stack with AI program builds this problem-solving muscle with mentor review and mock interviews.

Explore the Java Full Stack program →
Chat with us