Back to blog
Jun 05, 2025
3 min read

Find the Sum of Encrypted Integers

Calculate the sum of integers where each digit is replaced by the maximum digit found in that integer.

Difficulty: Easy | Acceptance: 74.90% | Paid: No Topics: Array, Math

You are given an integer array nums. The encryption of an integer is performed as follows:

Replace each digit x of the integer with the maximum digit of the integer.

Return the sum of the elements of nums after encryption.

Examples

Example 1:

Input: nums = [1,2,3]
Output: 6
Explanation: 1 is encrypted as 1, 2 is encrypted as 2, 3 is encrypted as 3. 1 + 2 + 3 = 6.

Example 2:

Input: nums = [10,21,31]
Output: 66
Explanation: 10 is encrypted as 11, 21 is encrypted as 22, 31 is encrypted as 33. 11 + 22 + 33 = 66.

Constraints

1 <= nums.length <= 50
1 <= nums[i] <= 1000

String Manipulation

Intuition Convert each number to a string to easily iterate over its digits, find the maximum digit, and construct the encrypted number by repeating that maximum digit.

Steps

  • Initialize a variable total to 0.
  • Iterate through each number in the input array nums.
  • Convert the current number to a string.
  • Find the maximum character (digit) in the string.
  • Create a new string consisting of this maximum character repeated for the length of the original string.
  • Convert this new string back to an integer and add it to total.
  • Return total.
python
class Solution:
    def sumOfEncryptedInt(self, nums: list[int]) -&gt; int:
        total = 0
        for num in nums:
            s = str(num)
            max_digit = max(s)
            encrypted = int(max_digit * len(s))
            total += encrypted
        return total

Complexity

  • Time: O(N * L), where N is the number of elements and L is the maximum number of digits in an element.
  • Space: O(L) to store the string representation of the number.
  • Notes: This approach is very readable and easy to implement but involves string allocation overhead.

Mathematical Calculation

Intuition Process each number mathematically by extracting digits to find the maximum value and the total number of digits. The encrypted number can be calculated using the geometric series sum formula: max_digit * (10^len - 1) / 9.

Steps

  • Initialize total to 0.
  • Iterate through each number in nums.
  • Initialize max_digit to 0 and length to 0.
  • Use a loop to extract digits from the number using modulo 10 and division by 10.
  • Update max_digit with the maximum value found and increment length.
  • Calculate the encrypted value using the formula max_digit * (10^length - 1) / 9.
  • Add the encrypted value to total.
  • Return total.
python
class Solution:
    def sumOfEncryptedInt(self, nums: list[int]) -&gt; int:
        total = 0
        for num in nums:
            max_digit = 0
            length = 0
            temp = num
            while temp &gt; 0:
                max_digit = max(max_digit, temp % 10)
                length += 1
                temp //= 10
            encrypted = max_digit * (10**length - 1) // 9
            total += encrypted
        return total

Complexity

  • Time: O(N * L), where N is the number of elements and L is the maximum number of digits.
  • Space: O(1), as we only use a few variables for calculation.
  • Notes: This approach avoids string allocation and is generally more performant in environments where string operations are expensive.