From 45d5c2b32432fd004ccb2278750a8a434eb53b40 Mon Sep 17 00:00:00 2001 From: kritebh Date: Sun, 26 Dec 2021 23:16:41 +0530 Subject: [PATCH] leetcode 1281 subtract the product and sum of digits of an integar added --- README.md | 1 + ...actTheProductAndSumOfDigitsOfAnInteger.cpp | 52 +++++++++++++++++++ 2 files changed, 53 insertions(+) create mode 100644 algorithms/cpp/subtractTheProductAndSumOfDigitsOfAnInteger/subtractTheProductAndSumOfDigitsOfAnInteger.cpp diff --git a/README.md b/README.md index 0ab766d0..c62a5618 100644 --- a/README.md +++ b/README.md @@ -149,6 +149,7 @@ LeetCode |1375|[Bulb Switcher III](https://leetcode.com/problems/bulb-switcher-iii) | [C++](./algorithms/cpp/bulbSwitcher/BulbSwitcher.III.cpp)|Medium| |1353|[Maximum Number of Events That Can Be Attended](https://leetcode.com/problems/maximum-number-of-events-that-can-be-attended/) | [C++](./algorithms/cpp/maximumNumberOfEventsThatCanBeAttended/MaximumNumberOfEventsThatCanBeAttended.cpp)|Medium| |1333|[Filter Restaurants by Vegan-Friendly, Price and Distance](https://leetcode.com/problems/filter-restaurants-by-vegan-friendly-price-and-distance/) | [C++](./algorithms/cpp/filterRestaurantsByVeganFriendlyPriceAndDistance/FilterRestaurantsByVeganFriendlyPriceAndDistance.cpp)|Medium| +|1281|[ Subtract the Product and Sum of Digits of an Integer](https://leetcode.com/problems/subtract-the-product-and-sum-of-digits-of-an-integer/) | [C++](./algorithms/cpp/subtractTheProductAndSumOfDigitsOfAnInteger/subtractTheProductAndSumOfDigitsOfAnInteger.cpp)|Easy| |1207|[Unique Number of Occurrences](https://leetcode.com/problems/unique-number-of-occurrences/) | [C++](./algorithms/cpp/uniqueNumberOfOccurrences/Unique-Number-of-Occurrences.cpp)|Easy| |1170|[Compare Strings by Frequency of the Smallest Character](https://leetcode.com/problems/compare-strings-by-frequency-of-the-smallest-character/) | [C++](./algorithms/cpp/compareStringsByFrequencyOfTheSmallestCharacter/CompareStringsByFrequencyOfTheSmallestCharacter.cpp)|Easy| |1071|[Greatest Common Divisor of Strings](https://leetcode.com/problems/greatest-common-divisor-of-strings/) | [C++](./algorithms/cpp/greatestCommonDivisorOfStrings/GreatestCommonDivisorOfStrings.cpp)|Easy| diff --git a/algorithms/cpp/subtractTheProductAndSumOfDigitsOfAnInteger/subtractTheProductAndSumOfDigitsOfAnInteger.cpp b/algorithms/cpp/subtractTheProductAndSumOfDigitsOfAnInteger/subtractTheProductAndSumOfDigitsOfAnInteger.cpp new file mode 100644 index 00000000..93a01c7e --- /dev/null +++ b/algorithms/cpp/subtractTheProductAndSumOfDigitsOfAnInteger/subtractTheProductAndSumOfDigitsOfAnInteger.cpp @@ -0,0 +1,52 @@ +// Source : https://leetcode.com/problems/subtract-the-product-and-sum-of-digits-of-an-integer/ +// Author : Kritebh +// Date : 2021-12-26 + +/*************************************************************************************** + +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 + +We are going to apply simple approach as said in question + +First extract the digit from given number then find product and sum + +Finally return product -sum in integer + +******************************************************************************************/ + +class Solution { +public: + int subtractProductAndSum(int n) { + + long long int product=1; + long long int sum =0; + long long int digit; + while(n){ + digit = n%10; + product*=digit; + sum+=digit; + n/=10; + } + + return (int)product - sum; + } +}; \ No newline at end of file