Sunday, February 23, 2020

1281. Subtract the Product and Sum of Digits of an Integer (easy)

Q:

Given an integer number n, return the difference between the product of its digits and the sum of its digits.

Example 1:
Input: n = 234
Output: 15 
Explanation: 
Product of digits = 2 * 3 * 4 = 24 
Sum of digits = 2 + 3 + 4 = 9 
Result = 24 - 9 = 15
Example 2:
Input: n = 4421
Output: 21
Explanation: 
Product of digits = 4 * 4 * 2 * 1 = 32 
Sum of digits = 4 + 4 + 2 + 1 = 11 
Result = 32 - 11 = 21

Constraints:
  • 1 <= n <= 10^5
A:

class Solution {
public:
    int subtractProductAndSum(int n) {
        int pro=1, sum =0;
        while(n)
        {
            int v = n%10;
            n /= 10;
            pro *= v;
            sum += v;
        }
        return pro - sum;        
    }
};

1 comment:

  1. This way my associate Wesley Virgin's adventure starts with this SHOCKING and controversial video.

    You see, Wesley was in the military-and shortly after leaving-he unveiled hidden, "SELF MIND CONTROL" tactics that the government and others used to get whatever they want.

    THESE are the EXACT same SECRETS tons of celebrities (notably those who "come out of nothing") and elite business people used to become wealthy and successful.

    You've heard that you only use 10% of your brain.

    Really, that's because most of your brain's power is UNCONSCIOUS.

    Perhaps this expression has even occurred INSIDE your own mind... as it did in my good friend Wesley Virgin's mind seven years back, while riding an unregistered, garbage bucket of a vehicle without a license and with $3.20 in his pocket.

    "I'm absolutely frustrated with living check to check! When will I finally succeed?"

    You've been a part of those those questions, ain't it right?

    Your success story is going to start. Go and take a leap of faith in YOURSELF.

    Take Action Now!

    ReplyDelete