Off-by-one on range boundaries
Wrong move: Loop endpoints miss first/last candidate.
Usually fails on: Fails on minimal arrays and exact-boundary answers.
Fix: Re-derive loops from inclusive/exclusive ranges before coding.
Build confidence with an intuition-first walkthrough focused on array fundamentals.
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 <= 501 <= nums[i] <= 1000Problem summary: 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.
Start with the most direct exhaustive search. That gives a correctness anchor before optimizing.
Pattern signal: Array · Math
[1,2,3]
[10,21,31]
encrypt-and-decrypt-strings)Source-backed implementations are provided below for direct study and interview prep.
// Accepted solution for LeetCode #3079: Find the Sum of Encrypted Integers
class Solution {
public int sumOfEncryptedInt(int[] nums) {
int ans = 0;
for (int x : nums) {
ans += encrypt(x);
}
return ans;
}
private int encrypt(int x) {
int mx = 0, p = 0;
for (; x > 0; x /= 10) {
mx = Math.max(mx, x % 10);
p = p * 10 + 1;
}
return mx * p;
}
}
// Accepted solution for LeetCode #3079: Find the Sum of Encrypted Integers
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
}
# Accepted solution for LeetCode #3079: Find the Sum of Encrypted Integers
class Solution:
def sumOfEncryptedInt(self, nums: List[int]) -> int:
def encrypt(x: int) -> int:
mx = p = 0
while x:
x, v = divmod(x, 10)
mx = max(mx, v)
p = p * 10 + 1
return mx * p
return sum(encrypt(x) for x in nums)
// Accepted solution for LeetCode #3079: Find the Sum of Encrypted Integers
fn sum_of_encrypted_int(nums: Vec<i32>) -> i32 {
fn encrypt(num: i32) -> i32 {
let mut largest = 0;
let mut n = num;
while n > 0 {
largest = std::cmp::max(largest, n % 10);
n /= 10;
}
let mut ret = 0;
n = num;
let mut base = 1;
while n > 0 {
ret += base * (largest % 10);
base *= 10;
n /= 10;
}
ret
}
nums.into_iter().map(encrypt).sum()
}
fn main() {
let nums = vec![10, 21, 31];
let ret = sum_of_encrypted_int(nums);
println!("ret={ret}");
}
#[test]
fn test() {
{
let nums = vec![1, 2, 3];
let ret = sum_of_encrypted_int(nums);
assert_eq!(ret, 6);
}
{
let nums = vec![10, 21, 31];
let ret = sum_of_encrypted_int(nums);
assert_eq!(ret, 66);
}
}
// Accepted solution for LeetCode #3079: Find the Sum of Encrypted Integers
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);
}
Use this to step through a reusable interview workflow for this problem.
Two nested loops check every pair or subarray. The outer loop fixes a starting point, the inner loop extends or searches. For n elements this gives up to n²/2 operations. No extra space, but the quadratic time is prohibitive for large inputs.
Most array problems have an O(n²) brute force (nested loops) and an O(n) optimal (single pass with clever state tracking). The key is identifying what information to maintain as you scan: a running max, a prefix sum, a hash map of seen values, or two pointers.
Review these before coding to avoid predictable interview regressions.
Wrong move: Loop endpoints miss first/last candidate.
Usually fails on: Fails on minimal arrays and exact-boundary answers.
Fix: Re-derive loops from inclusive/exclusive ranges before coding.
Wrong move: Temporary multiplications exceed integer bounds.
Usually fails on: Large inputs wrap around unexpectedly.
Fix: Use wider types, modular arithmetic, or rearranged operations.