4

I have been trying to figure out this question from LeetCoode. This is the problem: Given an integer number n, return the difference between the product of its digits and the sum of its digits.

This is my code so far:

/**
 * @param {number} n
 * @return {number}
 */
//n=234
var subtractProductAndSum = function(n) {
    var z= n.toString().length;
    var g= n.toString()
    for(var i=0; i<z; i++){
      var p= g[i]+g[i+1]+g[i+2];
    var y=g[i]*g[i+1]*g[i+2];
        var d= y-p
    }
    return d;
};
Mureinik
  • 297,002
  • 52
  • 306
  • 350

6 Answers6

1

Your solution assumes the number has three digits, which is probably not what the problem intended.

I'd run over the number and extract the digits one by one, and sum and multiply them as I go. Then, just subtract the two:

var subtractProductAndSum = function(n) {
    // Initialize the sum and the product with neutral values
    var sum = 0;
    var product = 1;

    while (n > 0) {
        var digit = n % 10;
        sum += digit;
        product *= digit;

        n = Math.floor(n / 10);
    }

    return product - sum;
};
Mureinik
  • 297,002
  • 52
  • 306
  • 350
1

You can convert the integer into a string and then split it. Then using the array of digit strings, find the product of the digits and the sum of the digits by using the reduce function like so:

const subtractProductAndSum = function(n) {
  const digitArray = n.toString().split('')
  const digitProduct = digitArray.reduce((previousValue, currentValue) => previousValue * parseInt(currentValue), 1)
  const digitSum = digitArray.reduce((previousValue, currentValue) => previousValue + parseInt(currentValue), 0)
  return digitProduct - digitSum
}
Christopher Bradshaw
  • 2,615
  • 4
  • 24
  • 38
1

You can try

/**
 * @param {number} n
 * @return {number}
 */
//n=234
var subtractProductAndSum = function(n) {
let product = (n+"").split("").reduce((total,curr)=>total * +curr,1);
let sum = (n+"").split("").reduce((total,curr)=>total + +curr,0);

return product - sum;
};

or

/**
 * @param {number} n
 * @return {number}
 */
//n=234
var subtractProductAndSum = function(n) {
let  product = 1;
let sum = 0;
n = n+"";

for(let  i = 0; i < n.length; i++){
product *= +n[i];
sum += +n[i];
}

return product - sum;
};

0

See the following :

/**
 * @param {number} n
 * @return {number}
 */
//n=234
var subtractProductAndSum = function(n) {
    var digits = n.split('');
    //console.log(digits);
    var prod = digits.reduce((a, b) => a * parseInt(b), 1);
    //console.log(prod);
    var sum = digits.reduce((a, b) => a + parseInt(b), 0);
    //console.log(sum);
    //console.log(prod - sum);
    return prod - sum;
};

subtractProductAndSum('234');
Mohamed23gharbi
  • 1,710
  • 23
  • 28
0
//it's working on IntelliJ
public class SubofProSum {
    public static void main(String[] args) {
        Scanner in = new Scanner(System.in);
        int sum,product,n,value,sub;
        sum = 0;
        product = 1;
        n= in.nextInt();

        while(n>0){
            value=n%10;
            sum = sum+value;
            product = product * value;
            n = n /10;
        }
        System.out.println("sum is: "+sum+" \nMultiple is: "+product);
        sub=product-sum;
        System.out.println("sub is:"+sub);
    }
}
  • Your answer could be improved with additional supporting information. Please [edit] to add further details, such as citations or documentation, so that others can confirm that your answer is correct. You can find more information on how to write good answers [in the help center](/help/how-to-answer). – Community Jan 30 '22 at 03:39
0

I have solved the same question in Java. I used the modulus operator (%) to compute the last digit, and the code snippet is given below.

public int subtractProductAndSum(int n) {

   int productOfDigits = 1;
    int sumOfDigits = 0;
    while (n > 0) {
        int lastDigit = n % 10;
        productOfDigits *= lastDigit;
        sumOfDigits += lastDigit;
        n /= 10;
    }
    return productOfDigits - sumOfDigits;
}
Lee Taylor
  • 7,761
  • 16
  • 33
  • 49