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 Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out /  Change )

Twitter picture

You are commenting using your Twitter account. Log Out /  Change )

Facebook photo

You are commenting using your Facebook account. Log Out /  Change )

Connecting to %s