3079. Find the Sum of Encrypted Integers LeetCode Solution
In this guide, you will get 3079. Find the Sum of Encrypted Integers LeetCode Solution with the best time and space complexity. The solution to Find the Sum of Encrypted Integers problem is provided in various programming languages like C++, Java, and Python. This will be helpful for you if you are preparing for placements, hackathons, interviews, or practice purposes. The solutions provided here are very easy to follow and include detailed explanations.
Table of Contents
- Problem Statement
- Complexity Analysis
- Find the Sum of Encrypted Integers solution in C++
- Find the Sum of Encrypted Integers solution in Java
- Find the Sum of Encrypted Integers solution in Python
- Additional Resources
Problem Statement of Find the Sum of Encrypted Integers
You are given an integer array nums containing positive integers. We define a function encrypt such that encrypt(x) replaces every digit in x with the largest digit in x. For example, encrypt(523) = 555 and encrypt(213) = 333.
Return the sum of encrypted elements.
Example 1:
Input: nums = [1,2,3]
Output: 6
Explanation: The encrypted elements are [1,2,3]. The sum of encrypted elements is 1 + 2 + 3 == 6.
Example 2:
Input: nums = [10,21,31]
Output: 66
Explanation: The encrypted elements are [11,22,33]. The sum of encrypted elements is 11 + 22 + 33 == 66.
Constraints:
1 <= nums.length <= 50
1 <= nums[i] <= 1000
Complexity Analysis
- Time Complexity: O(n)
- Space Complexity: O(1)
3079. Find the Sum of Encrypted Integers LeetCode Solution in C++
class Solution {
public:
int sumOfEncryptedInt(vector<int>& nums) {
int ans = 0;
for (const int num : nums) {
int maxDigit = 0;
int base = 0;
for (int x = num; x > 0; x /= 10) {
maxDigit = max(maxDigit, x % 10);
base = base * 10 + 1;
}
ans += base * maxDigit;
}
return ans;
}
};
/* code provided by PROGIEZ */
3079. Find the Sum of Encrypted Integers LeetCode Solution in Java
class Solution {
public int sumOfEncryptedInt(int[] nums) {
int ans = 0;
for (final int num : nums) {
int maxDigit = 0;
int base = 0;
for (int x = num; x > 0; x /= 10) {
maxDigit = Math.max(maxDigit, x % 10);
base = base * 10 + 1;
}
ans += base * maxDigit;
}
return ans;
}
}
// code provided by PROGIEZ
3079. Find the Sum of Encrypted Integers LeetCode Solution in Python
class Solution:
def sumOfEncryptedInt(self, nums: list[int]) -> int:
def getEncrypted(num: int) -> int:
maxDigit = 0
base = 0
while num > 0:
maxDigit = max(maxDigit, num % 10)
base = base * 10 + 1
num //= 10
return base * maxDigit
return sum(getEncrypted(num) for num in nums)
# code by PROGIEZ
Additional Resources
- Explore all LeetCode problem solutions at Progiez here
- Explore all problems on LeetCode website here
Happy Coding! Keep following PROGIEZ for more updates and solutions.