Unknown's avatar

1281. Subtract the Product and Sum of Digits of an Integer

1281. Subtract the Product and Sum of Digits of an Integer

class Solution {
    public int subtractProductAndSum(int n) {
    
        ArrayList<Integer> list = new ArrayList<Integer>();
        while(n != 0){
        list.add(n%10);
        n = n/10;
    }
    int product = 1;
    int sum = 0;
    for(int i = 0; i < list.size(); i++){
        product *= list.get(i);
        sum+= list.get(i);
    }
    return product - sum;
}
}

Leave a comment