Taro Logo

Find the Sum of Encrypted Integers

Easy
Asked by:
Larsen & Toubro
0 views
Topics:
Arrays

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

Solution


Clarifying Questions

When you get asked this question in a real-life environment, it will often be ambiguous (especially at FAANG). Make sure to ask these questions in that case:

  1. Can the input array contain negative numbers, or are all integers guaranteed to be non-negative?
  2. What is the expected behavior if the input array is empty?
  3. How should single-digit numbers be encrypted (e.g., does a number like '7' remain '7')?
  4. Could the encrypted numbers exceed standard 32-bit integer limits, requiring us to consider potential integer overflow?
  5. Are all elements in the `nums` array guaranteed to be valid integers, or could we encounter null or floating-point values?

Brute Force Solution

Approach

To find the sum of all encrypted integers, the brute force approach goes through every single number one by one. For each number, it transforms it according to the encryption rule and then adds all those transformed results together to get the final total.

Here's how the algorithm would work step-by-step:

  1. Look at the first number in the collection of numbers.
  2. Find the biggest digit in that number.
  3. Replace every single digit in that number with that biggest digit.
  4. Write down this newly created encrypted number.
  5. Repeat this exact process for the second number, the third number, and so on, until every single number has been looked at and encrypted.
  6. Add all of the newly created encrypted numbers together to get one final grand total.
  7. Share that grand total as the final answer.

Code Implementation

def sum_of_encrypted_integers(numbers):
    encrypted_total_sum = 0

    # We iterate through each individual number to apply the required digit transformation rule
    for current_number in numbers:
        number_string = str(current_number)
        maximum_digit = max(number_string)

        # Replacing every single digit with the maximum found digit creates the new encrypted value
        encrypted_string = maximum_digit * len(number_string)
        encrypted_number = int(encrypted_string)

        encrypted_total_sum += encrypted_number

    # Returning the accumulated grand total fulfills the final requirement of the encryption process
    return encrypted_total_sum

Big(O) Analysis

Time Complexity
O(n * d) – For each of the n numbers in the input collection, we must process its digits to find the maximum value and construct the encrypted integer. Letting d represent the maximum number of digits in any single number, finding the max digit and replacing all digits takes proportional to d operations per number. Repeating this encryption process for all n numbers results in approximately n * d total operations. Since the number of digits d is typically bounded by a small constant (such as up to 5 digits for standard integer constraints), this simplifies to O(n) linear time complexity with respect to the number of elements.
Space Complexity
O(1) – To encrypt each number and compute the final total, the algorithm only uses a few primitive variables to track the current number, its maximum digit, the running total, and digit extraction math. These constant variables require a fixed amount of extra memory regardless of the input size N. Therefore, the auxiliary space complexity remains constant because no dynamic data structures or recursion stacks grow with the number of elements.

Optimal Solution

Approach

To find the sum of encrypted integers, we need to transform every number in our collection by replacing all of its digits with its largest digit. Once every number is converted this way, we simply add all the transformed numbers together to get our final answer.

Here's how the algorithm would work step-by-step:

  1. Look at the collection of given numbers one by one.
  2. For any chosen number, examine its individual digits to find the one with the highest value.
  3. Replace every digit in that number with the highest digit you just found, effectively creating a brand new encrypted version of the number.
  4. Add this newly encrypted number to a running total.
  5. Repeat this transformation and addition process for every number in the collection.
  6. Once all numbers have been processed and added together, output the final grand total as the answer.

Code Implementation

def sum_of_encrypted_int(input_numbers_list):
    total_encrypted_sum = 0

    # We transform each integer to ensure every digit matches the maximum digit found in it.
    for current_number in input_numbers_list:
        number_as_string = str(current_number)
        highest_digit_character = max(number_as_string)
        encrypted_number = int(highest_digit_character * len(number_as_string))

        total_encrypted_sum += encrypted_number

    # Accumulating all encrypted numbers yields the final grand total required.
    return total_encrypted_sum

Big(O) Analysis

Time Complexity
O(n * d) – For each of the n integers in the given collection, we must process its individual digits to find the maximum value and construct the encrypted number. If we let d represent the maximum number of digits across the integers, finding the max digit and replacing all digits takes proportional to d operations per integer. Multiplying this per-integer cost across all n integers gives approximately n * d operations, which simplifies to O(n * d) time complexity.
Space Complexity
O(1) – To calculate the encrypted sum, the algorithm only needs a few primitive variables to maintain the running total and process the digits of each number in the collection. These variables use a constant amount of extra memory regardless of the input size N. No dynamic data structures, temporary lists, or recursive calls are created to store intermediate results. Therefore, the auxiliary space complexity remains strictly constant.

Edge Cases

Empty input array nums
How to Handle:
Return zero immediately since there are no integers to encrypt and sum.
Single-element array
How to Handle:
Process the single number by finding its max digit, encrypting it, and returning that exact value.
Numbers with all identical digits such as 222
How to Handle:
The max digit function correctly identifies the digit and keeps the number unchanged during encryption.
Numbers with a single digit like 5
How to Handle:
The algorithm treats the single digit as the maximum and returns the number itself without modification.
Integers containing the digit zero
How to Handle:
Zeros are ignored when determining the maximum digit unless all digits are zero, ensuring non-zero replacement.
Very large integers or maximum constraint sizes
How to Handle:
Convert numbers to strings or use arithmetic loops to find the max digit safely within standard integer limits without overflow.
Negative numbers
How to Handle:
Assuming standard constraints where inputs are positive integers, but if negatives occur, the sign must be preserved while evaluating absolute digit magnitudes.
Maximum-sized array with high-value elements
How to Handle:
Iterate through the array in linear time O(n * d) where d is the number of digits, ensuring efficient scaling for FAANG performance standards.