Subtract the Product and Sum of Digits of an Integer

Easy
177.

Subtract the Product and Sum of Digits of an Integer

Subtract the Product and Sum of Digits of an Integer is an easy coding problem on math, asked in Amazon, Infosys and Wipro interviews.

Given an integer n, return the product of its digits minus the sum of its digits.

Example 1

Input
n = 234
Output
15
Explanation
Product = 2 × 3 × 4 = 24, sum = 2 + 3 + 4 = 9, and 24 - 9 = 15.

Example 2

Input
n = 4421
Output
21
Explanation
Product = 32, sum = 11.

Example 3

Input
n = 1
Output
0

Constraints

  • 1 <= n <= 100000
Write your solution here. Running it against the tests needs a free account — what you type comes with you.Sign in to run
/**
 * @param {number} n
 * @return {number}
 */
var subtractProductAndSum = function(n) {
    // Write your code here
    
};

Sign in to run your own test cases

Sign in

Subtract the Product and Sum of Digits of an Integer Solution

Subtract the Product and Sum of Digits of an Integer solution explained: step-by-step approach, complexity and code in Python, Java, C++ and JavaScript.

Subtract the Product and Sum of Digits of an Integer is an easy coding problem on math, asked in Amazon, Infosys and Wipro interviews.

  • Difficulty: Easy
  • Topics: Math
  • Asked at: Amazon, Infosys, Wipro
  • Time limit: 2 s
  • Memory limit: 256 MB
  • Languages: JavaScript, TypeScript, Python, Java, C++, C, C#, Go, Kotlin, Swift, Rust, PHP and Ruby

Problem statement

Given an integer n, return the product of its digits minus the sum of its digits.

Example 1

Input: n = 234
Output: 15
Explanation: Product = 2 × 3 × 4 = 24, sum = 2 + 3 + 4 = 9, and 24 - 9 = 15.

Example 2

Input: n = 4421
Output: 21
Explanation: Product = 32, sum = 11.

Example 3

Input: n = 1
Output: 0

Constraints

  • 1 <= n <= 100000

Hints for Subtract the Product and Sum of Digits of an Integer

  1. Hint 1

    One pass over the digits can maintain both accumulators at once.

  2. Hint 2

    Seed the product at 1 and the sum at 0.

  3. Hint 3

    A zero digit legitimately zeroes the product — do not skip it.

How to solve Subtract the Product and Sum of Digits of an Integer

One digit-peeling loop maintains both accumulators — the product seeded at 1 and the sum at 0 — then return their difference.

Approach

  1. Set prod = 1, total = 0.
  2. While n > 0: d = n % 10; multiply prod by d, add d to total, n //= 10.
  3. Return prod - total.

Why it works

Both quantities are simple folds over the digit sequence and can share the traversal.

Complexity

  • Time — O(digits)
  • Space — O(1)

Pitfalls

  • A zero digit legitimately zeroes the product; do not skip it.
  • Seeding the product at 0 makes every answer -sum.

Reference solution

Python

def subtractProductAndSum(n: int) -> int:
    prod, total = 1, 0
    while n > 0:
        d = n % 10
        prod *= d
        total += d
        n //= 10
    return prod - total

Java

public static int subtractProductAndSum(int n) {
    int prod = 1, total = 0;
    while (n > 0) {
        int d = n % 10;
        prod *= d;
        total += d;
        n /= 10;
    }
    return prod - total;
}

C++

int subtractProductAndSum(int n) {
    int prod = 1, total = 0;
    while (n > 0) {
        int d = n % 10;
        prod *= d;
        total += d;
        n /= 10;
    }
    return prod - total;
}

JavaScript

var subtractProductAndSum = function(n) {
    let prod = 1, total = 0;
    while (n > 0) {
        const d = n % 10;
        prod *= d;
        total += d;
        n = Math.floor(n / 10);
    }
    return prod - total;
};

Also on the editorial tab: C, C#, Go, Kotlin, PHP, Ruby, Rust, Swift, TypeScript.

All 337 math problems · the whole catalogue