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 thesum of encrypted elements.

Examples

Example 1

1
2
3
4
5
6
7

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

1
2
3
4
5
6
7

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

Method 1 – Digit Replacement and Summation

Intuition

For each number, find its largest digit and replace every digit with this value to form the encrypted number. The sum of all such encrypted numbers is the answer.

Approach

  1. For each number in nums:
    • Convert the number to a string or process its digits to find the largest digit.
    • Replace every digit with the largest digit to form the encrypted number.
  2. Sum all encrypted numbers and return the result.

Code

 1
 2
 3
 4
 5
 6
 7
 8
 9
10
11
12
13
14
class Solution {
public:
    int sumOfEncryptedInt(vector<int>& nums) {
        int ans = 0;
        for (int x : nums) {
            int mx = 0, y = x, len = 0;
            while (y) { mx = max(mx, y % 10); y /= 10; len++; }
            int enc = 0;
            for (int i = 0; i < len; ++i) enc = enc * 10 + mx;
            ans += enc;
        }
        return ans;
    }
};
 1
 2
 3
 4
 5
 6
 7
 8
 9
10
11
12
13
14
15
func sumOfEncryptedInt(nums []int) int {
    ans := 0
    for _, x := range nums {
        mx, y, len := 0, x, 0
        for y > 0 {
            if y%10 > mx { mx = y % 10 }
            y /= 10
            len++
        }
        enc := 0
        for i := 0; i < len; i++ { enc = enc*10 + mx }
        ans += enc
    }
    return ans
}
 1
 2
 3
 4
 5
 6
 7
 8
 9
10
11
12
13
14
15
16
17
class Solution {
    public int sumOfEncryptedInt(int[] nums) {
        int ans = 0;
        for (int x : nums) {
            int mx = 0, y = x, len = 0;
            while (y > 0) {
                mx = Math.max(mx, y % 10);
                y /= 10;
                len++;
            }
            int enc = 0;
            for (int i = 0; i < len; ++i) enc = enc * 10 + mx;
            ans += enc;
        }
        return ans;
    }
}
 1
 2
 3
 4
 5
 6
 7
 8
 9
10
11
12
13
14
15
16
17
18
19
class Solution {
    fun sumOfEncryptedInt(nums: IntArray): Int {
        var ans = 0
        for (x in nums) {
            var mx = 0
            var y = x
            var len = 0
            while (y > 0) {
                mx = maxOf(mx, y % 10)
                y /= 10
                len++
            }
            var enc = 0
            repeat(len) { enc = enc * 10 + mx }
            ans += enc
        }
        return ans
    }
}
1
2
3
4
5
6
7
8
9
class Solution:
    def sumOfEncryptedInt(self, nums: list[int]) -> int:
        ans = 0
        for x in nums:
            s = str(x)
            mx = max(s)
            enc = int(mx * len(s))
            ans += enc
        return ans
 1
 2
 3
 4
 5
 6
 7
 8
 9
10
11
12
13
14
15
16
17
18
19
impl Solution {
    pub fn sum_of_encrypted_int(nums: Vec<i32>) -> i32 {
        let mut ans = 0;
        for x in nums {
            let mut y = x;
            let mut mx = 0;
            let mut len = 0;
            while y > 0 {
                mx = mx.max(y % 10);
                y /= 10;
                len += 1;
            }
            let mut enc = 0;
            for _ in 0..len { enc = enc * 10 + mx; }
            ans += enc;
        }
        ans
    }
}
 1
 2
 3
 4
 5
 6
 7
 8
 9
10
11
12
class Solution {
    sumOfEncryptedInt(nums: number[]): number {
        let ans = 0;
        for (const x of nums) {
            const s = x.toString();
            const mx = Math.max(...s.split('').map(Number));
            const enc = Number(Array(s.length).fill(mx).join(''));
            ans += enc;
        }
        return ans;
    }
}

Complexity

  • ⏰ Time complexity: O(n * d) — n = number of elements, d = max number of digits per number.
  • 🧺 Space complexity: O(1) — Only a few variables for computation.