Talentd
FleetCode
Make and follow sheets.Sign in

Find the Sum of Encrypted Integers solution and explanation

EasyArrayMath6 min read74.7% acceptanceUpdated 9 Apr 2026
100262. Find the Sum of Encrypted Integers | Leetcode | Biweekly Contest 126 | Sol Code In Comment.Optimization · 0:17 · 703 viewsWatch on YouTube
9 more videos on this problem
  1. 100262. Find the Sum of Encrypted Integers | Leetcode | Biweekly Contest 126 | Sol Code In Comment.Optimization · 0:17 · 703 views
  2. Leetcode | 3079. Find the Sum of Encrypted Integers | Easy | Java SolutionDeveloper Docs · 5:21 · 649 views
  3. Leetcode 3079: Find the Sum of Encrypted IntegersAlgorithms Casts · 8:39 · 501 views
  4. 3079. Find the Sum of Encrypted Integers | BiWeekly contest 126 | Easy | C++Priyanshi Porwal · 5:23 · 457 views
  5. 3079. Find the Sum of Encrypted Integers | LEETCODE EASYcode Explainer · 4:07 · 377 views
  6. 3079. Find the Sum of Encrypted Integers (Leetcode Easy)Programming Live with Larry · 4:32 · 245 views
  7. LeetCode | 3079. Find the Sum of Encrypted Integers | Easy | Biweekly Contest 126Coding with Bobby · 2:34 · 180 views
  8. Leetcode easy problem 3079. Find the Sum of Encrypted Integers | devops python practiceADevOpsEngineer · 3:23 · 71 views
  9. Leetcode 3079 ~ Find the Sum of Encrypted Integers ~ Python Solutionarch_r45 · 11:00 · 60 views
  10. 🏁Biweekly Contest 126 is Here ! Problem 1-3079. Find the Sum of Encrypted IntegersAchyuth Chowdary · 5:17 · 40 views

The problem

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

Approach overview

Problem Overview: You receive an array of integers. Each number must be encrypted by replacing every digit with the maximum digit present in that number. For example, 523 becomes 555 because the largest digit is 5. After encrypting every value, return the total sum of the encrypted numbers.

The task is mostly digit manipulation. For each number, extract digits, determine the maximum digit, rebuild a number consisting only of that digit repeated for the same length, then accumulate the result. The problem relies on basic operations from array iteration and math digit processing.

Approach 1: Iterative Transformation (O(n · d) time, O(1) space)

Iterate through each number in the array and process its digits using modulo and division. During the scan, track two values: the maximum digit seen so far and the number of digits. Once all digits are processed, rebuild the encrypted number by repeating the maximum digit the same number of times (for example, length 3 and max digit 7 becomes 777). Add this value to the running sum. This approach performs digit extraction and reconstruction directly with simple loops, making it easy to implement in any language.

The key insight: the encrypted value depends only on two pieces of information—the maximum digit and the digit count. Every other detail of the original number becomes irrelevant. Since each digit is processed once, the work per number is proportional to its digit count d. Total complexity is O(n · d) with constant extra memory.

Approach 2: Precalculate Maximum Digit and Rebuild (O(n · d) time, O(1) space)

Separate the process into two explicit steps. First, iterate through the digits of the number to compute the maximum digit. Second, iterate again (or use the stored digit length) to construct the encrypted number by repeatedly multiplying the result by 10 and adding the maximum digit. This produces values like 999 or 4444 depending on the original length.

This approach makes the logic clearer: one pass determines the transformation rule (maximum digit), and the second pass performs reconstruction. It is especially useful when writing clean, readable implementations or when adapting the logic for similar digit-replacement problems in math-based questions.

Recommended for interviews: The iterative transformation approach is typically expected. It demonstrates that you can manipulate digits using modulo/division and reason about number reconstruction efficiently. The precalculation variant shows the same insight but with slightly clearer separation of steps. Both run in O(n · d) time and O(1) space, which is optimal for this problem.

Approach 1: Approach 1: Iterative Transformation

This approach involves iterating through the list of numbers, and for each number, finding the largest digit. We replace every digit of the number with this largest digit to form the encrypted number. The sum of these encrypted numbers is then computed.

def encrypt(x):
    max_digit = max(int(d) for d in str(x))
    return int(str(max_digit) * len(str(x)))

def sum_of_encrypted_elements(nums):
    return sum(encrypt(num) for num in nums)

nums = [10, 21, 31]
print(sum_of_encrypted_elements(nums))
public class EncryptedSum {
    public static void main(String[] args) {
        int[] nums = {10, 21, 31};
        System.out.println(sumOfEncryptedElements(nums));
    }

    public static int encrypt(int x) {
        int maxDigit = 0;
        int num = x;
        while (num > 0) {
            maxDigit = Math.max(maxDigit, num % 10);
            num /= 10;
        }
        int encryptedNum = 0;
        while (x > 0) {
            encryptedNum = encryptedNum * 10 + maxDigit;
            x /= 10;
        }
        return encryptedNum;
    }

    public static int sumOfEncryptedElements(int[] nums) {
        int sum = 0;
        for (int num : nums) {
            sum += encrypt(num);
        }
        return sum;
    }
}
#include <iostream>
#include <vector>

int encrypt(int x) {
    int max_digit = 0, num = x;
    while (num) {
        max_digit = std::max(max_digit, num % 10);
        num /= 10;
    }
    int encrypted_num = 0;
    while (x) {
        encrypted_num = encrypted_num * 10 + max_digit;
        x /= 10;
    }
    return encrypted_num;
}

int sumOfEncryptedElements(const std::vector<int>& nums) {
    int sum = 0;
    for (int num : nums) {
        sum += encrypt(num);
    }
    return sum;
}

int main() {
    std::vector<int> nums = {10, 21, 31};
    std::cout << sumOfEncryptedElements(nums) << std::endl;
    return 0;
}
function encrypt(x) {
    const digits = x.toString().split('').map(Number);
    const maxDigit = Math.max(...digits);
    return parseInt(digits.map(() => maxDigit).join(''), 10);
}

function sumOfEncryptedElements(nums) {
    return nums.reduce((sum, num) => sum + encrypt(num), 0);
}

const nums = [10, 21, 31];
console.log(sumOfEncryptedElements(nums));
#include <stdio.h>

int encrypt(int x) {
    int max_digit = 0;
    int num = x;
    while (num > 0) {
        int digit = num % 10;
        if (digit > max_digit) {
            max_digit = digit;
        }
        num /= 10;
    }
    int encrypted_num = 0;
    while (x > 0) {
        encrypted_num = encrypted_num * 10 + max_digit;
        x /= 10;
    }
    return encrypted_num;
}

int sumOfEncryptedElements(int* nums, int numsSize) {
    int sum = 0;
    for (int i = 0; i < numsSize; i++) {
        sum += encrypt(nums[i]);
    }
    return sum;
}

int main() {
    int nums[] = {10, 21, 31};
    int size = sizeof(nums) / sizeof(nums[0]);
    printf("%d\n", sumOfEncryptedElements(nums, size));
    return 0;
}
using System;

class EncryptedSum {
    static void Main() {
        int[] nums = {10, 21, 31};
        Console.WriteLine(SumOfEncryptedElements(nums));
    }

    public static int Encrypt(int x) {
        int maxDigit = 0, num = x;
        while (num > 0) {
            maxDigit = Math.Max(maxDigit, num % 10);
            num /= 10;
        }
        int encryptedNum = 0;
        while (x > 0) {
            encryptedNum = encryptedNum * 10 + maxDigit;
            x /= 10;
        }
        return encryptedNum;
    }

    public static int SumOfEncryptedElements(int[] nums) {
        int sum = 0;
        foreach (int num in nums) {
            sum += Encrypt(num);
        }
        return sum;
    }
}

The Python solution uses string manipulation and a generator expression to find the maximum digit and construct the encrypted number. The use of sum function simplifies the accumulation of results.

Complexity

Time Complexity: O(n * m), where n is the number of elements in the array and m is the number of digits in the largest number.
Space Complexity: O(1).

Try this approach in the editor

Approach 2: Approach 2: Precalculate Maximum Digit and Rebuild

This approach involves precalculating the maximum digit for each number and then constructing the new number using a repetitive process. Though similar to Approach 1, this highlights the preprocessing step distinctly and showcases optimization possibilities in reconstruction.

def max_digit(x):
    return max(int(d) for d in str(x))

def encrypt_rebuild(x, max_d):
    return int(str(max_d) * len(str(x)))

def sum_of_encrypted_elements(nums):
    return sum(encrypt_rebuild(num, max_digit(num)) for num in nums)

nums = [10, 21, 31]
print(sum_of_encrypted_elements(nums))
public class EncryptedSum {
    public static void main(String[] args) {
        int[] nums = {10, 21, 31};
        System.out.println(sumOfEncryptedElements(nums));
    }

    public static int maxDigit(int x) {
        int maxDigit = 0;
        while (x > 0) {
            maxDigit = Math.max(maxDigit, x % 10);
            x /= 10;
        }
        return maxDigit;
    }

    public static int encryptRebuild(int x, int maxDigit) {
        int encryptedNum = 0;
        while (x > 0) {
            encryptedNum = encryptedNum * 10 + maxDigit;
            x /= 10;
        }
        return encryptedNum;
    }

    public static int sumOfEncryptedElements(int[] nums) {
        int sum = 0;
        for (int num : nums) {
            int maxDigit = maxDigit(num);
            sum += encryptRebuild(num, maxDigit);
        }
        return sum;
    }
}
#include <iostream>
#include <vector>

int maxDigit(int x) {
    int max_digit = 0;
    while (x > 0) {
        max_digit = std::max(max_digit, x % 10);
        x /= 10;
    }
    return max_digit;
}

int encryptRebuild(int x, int max_digit) {
    int encrypted_num = 0;
    while (x > 0) {
        encrypted_num = encrypted_num * 10 + max_digit;
        x /= 10;
    }
    return encrypted_num;
}

int sumOfEncryptedElements(const std::vector<int>& nums) {
    int sum = 0;
    for (int num : nums) {
        int max_digit = maxDigit(num);
        sum += encryptRebuild(num, max_digit);
    }
    return sum;
}

int main() {
    std::vector<int> nums = {10, 21, 31};
    std::cout << sumOfEncryptedElements(nums) << std::endl;
    return 0;
}
function maxDigit(x) {
    return Math.max(...x.toString().split('').map(Number));
}

function encryptRebuild(x, maxDigit) {
    return parseInt(x.toString().replace(/./g, maxDigit), 10);
}

function sumOfEncryptedElements(nums) {
    return nums.reduce((sum, num) => sum + encryptRebuild(num, maxDigit(num)), 0);
}

const nums = [10, 21, 31];
console.log(sumOfEncryptedElements(nums));
#include <stdio.h>

int maxDigit(int x) {
    int max_digit = 0;
    while (x > 0) {
        int digit = x % 10;
        if (digit > max_digit) {
            max_digit = digit;
        }
        x /= 10;
    }
    return max_digit;
}

int encryptRebuild(int x, int max_digit) {
    int encrypted_num = 0;
    while (x > 0) {
        encrypted_num = encrypted_num * 10 + max_digit;
        x /= 10;
    }
    return encrypted_num;
}

int sumOfEncryptedElements(int* nums, int numsSize) {
    int sum = 0;
    for (int i = 0; i < numsSize; i++) {
        int max_digit = maxDigit(nums[i]);
        sum += encryptRebuild(nums[i], max_digit);
    }
    return sum;
}

int main() {
    int nums[] = {10, 21, 31};
    int size = sizeof(nums) / sizeof(nums[0]);
    printf("%d\n", sumOfEncryptedElements(nums, size));
    return 0;
}
using System;

class EncryptedSum {
    static void Main() {
        int[] nums = {10, 21, 31};
        Console.WriteLine(SumOfEncryptedElements(nums));
    }

    public static int MaxDigit(int x) {
        int maxDigit = 0;
        while (x > 0) {
            maxDigit = Math.Max(maxDigit, x % 10);
            x /= 10;
        }
        return maxDigit;
    }

    public static int EncryptRebuild(int x, int maxDigit) {
        int encryptedNum = 0;
        while (x > 0) {
            encryptedNum = encryptedNum * 10 + maxDigit;
            x /= 10;
        }
        return encryptedNum;
    }

    public static int SumOfEncryptedElements(int[] nums) {
        int sum = 0;
        foreach (int num in nums) {
            int maxDigit = MaxDigit(num);
            sum += EncryptRebuild(num, maxDigit);
        }
        return sum;
    }
}

Python's implementation uses a helper function to find the maximum digit and a reconstructive function to form the encrypted number. The separate steps allow for clean logic flow.

Complexity

Time Complexity: O(n * m).
Space Complexity: O(1).

Try this approach in the editor

Approach 3: Simulation

We directly simulate the encryption process by defining a function encrypt(x), which replaces each digit in an integer x with the maximum digit in x. The implementation of the function is as follows:

We can obtain each digit of x by continuously taking the modulus and integer division of x by 10, and find the maximum digit, denoted as mx. During the loop, we can also use a variable p to record the base number of mx, i.e., p = 1, 11, 111, cdots. Finally, return mx times p.

The time complexity is O(n times log M), where n is the length of the array, and M is the maximum value in the array. The space complexity is O(1).

function sumOfEncryptedInt(nums: number[]): number {
    const encrypt = (x: number): number => {
        let [mx, p] = [0, 0];
        for (; x > 0; x = Math.floor(x / 10)) {
            mx = Math.max(mx, x % 10);
            p = p * 10 + 1;
        }
        return mx * p;
    };
    return nums.reduce((acc, x) => acc + encrypt(x), 0);
}
func sumOfEncryptedInt(nums []int) (ans int) {
	encrypt := func(x int) int {
		mx, p := 0, 0
		for ; x > 0; x /= 10 {
			mx = max(mx, x%10)
			p = p*10 + 1
		}
		return mx * p
	}
	for _, x := range nums {
		ans += encrypt(x)
	}
	return
}
Try this approach in the editor

Complexity comparison

ApproachComplexity
Approach 1: Iterative Transformation

Time Complexity: O(n * m), where n is the number of elements in the array and m is the number of digits in the largest number.
Space Complexity: O(1).

Approach 2: Precalculate Maximum Digit and Rebuild

Time Complexity: O(n * m).
Space Complexity: O(1).

Simulation–

Complexity analysis

ApproachTimeSpaceWhen to Use
Iterative TransformationO(n · d)O(1)Best general solution. Processes digits once per number and rebuilds the encrypted value directly.
Precalculate Maximum Digit and RebuildO(n · d)O(1)Good when separating logic for clarity—first compute max digit, then construct the encrypted number.

Frequently asked questions

Is Find the Sum of Encrypted Integers easy or hard?

Find the Sum of Encrypted Integers is classified as an Easy problem on LeetCode with a high acceptance rate around 74%. It focuses on basic array traversal and digit manipulation rather than advanced data structures or algorithms.

Find the Sum of Encrypted Integers Python/Java solution

In Python or Java, loop through each number, compute the maximum digit using a while loop with modulo operations, count the digits, and build the encrypted value by repeatedly multiplying by 10 and adding the max digit. Sum these encrypted numbers to produce the final result.

How to solve Find the Sum of Encrypted Integers in O(n)?

Treat the digit length as a small constant. For each array element, extract digits using modulo and division to determine the maximum digit and length, then construct the encrypted number like 777 or 9999. The array is scanned once, giving O(n · d) time which behaves like O(n) for typical integer sizes.

What is the best approach for Find the Sum of Encrypted Integers?

The best approach iterates through each number, finds the maximum digit, counts its digits, and rebuilds a number consisting only of that digit repeated. This iterative digit transformation runs in O(n · d) time where d is the number of digits per number, and uses O(1) extra space.

Is Find the Sum of Encrypted Integers asked at Google/Amazon/Meta?

The problem itself is a LeetCode Easy question and is more common in screening rounds or practice sets rather than direct Google or Meta interview questions. However, the underlying skill—digit manipulation and number reconstruction—appears in many coding interviews.

What data structure is used in Find the Sum of Encrypted Integers?

The main structure is an array that stores the input numbers. The algorithm relies on basic math operations such as modulo (%) and integer division to extract digits rather than additional data structures.

What is the time complexity of Find the Sum of Encrypted Integers?

The time complexity is O(n · d). Each of the n integers is processed digit by digit, and a number with d digits requires at most d operations to find the maximum digit and rebuild the encrypted value. Space complexity remains O(1).

Comments

Now write it yourselfClose this page and solve Find the Sum of Encrypted Integers in the editor, without looking.
Practise Find the Sum of Encrypted Integers