Find the Sum of Encrypted Integers - 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.

Input & Output

Example 1 — Basic Encryption
$ Input: nums = [1,2,3]
Output: 6
💡 Note: encrypt(1) = 1, encrypt(2) = 2, encrypt(3) = 3. Sum = 1 + 2 + 3 = 6
Example 2 — Multi-digit Numbers
$ Input: nums = [523,213]
Output: 888
💡 Note: encrypt(523) = 555 (max digit 5), encrypt(213) = 333 (max digit 3). Sum = 555 + 333 = 888
Example 3 — Same Digits
$ Input: nums = [999]
Output: 999
💡 Note: encrypt(999) = 999 (all digits are 9). Sum = 999

Constraints

  • 1 ≤ nums.length ≤ 50
  • 1 ≤ nums[i] ≤ 1000

Visualization

Tap to expand
Sum of Encrypted Integers INPUT nums = [1, 2, 3] 1 idx 0 2 idx 1 3 idx 2 encrypt(x) Function: Replace every digit with the largest digit in x encrypt(523) = 555 For single digits: max digit = number itself encrypt(1)=1, encrypt(2)=2 encrypt(3)=3 ALGORITHM STEPS 1 Convert to String num --> string for digit access 2 Find Max Digit Scan string, track maximum 3 Build Encrypted Repeat max digit (length) 4 Sum All Encrypted Add all encrypt(nums[i]) Processing Each Element: 1 --> "1" --> max='1' --> 1 2 --> "2" --> max='2' --> 2 3 --> "3" --> max='3' --> 3 Sum: 1 + 2 + 3 = 6 FINAL RESULT Output: 6 OK - Correct! Breakdown: encrypt(1) = 1 encrypt(2) = 2 encrypt(3) = 3 1+2+3 = 6 Sum of encrypted Key Insight: String-based approach allows easy digit iteration. For each number, convert to string, find the maximum character, then create new string with max digit repeated. For single-digit numbers like [1,2,3], each encrypts to itself since they have only one digit! TutorialsPoint - Find the Sum of Encrypted Integers | String-Based Encryption Approach
Asked in
Google 15 Amazon 12
12.5K Views
Medium Frequency
~10 min Avg. Time
340 Likes
Ln 1, Col 1
Smart Actions
💡 Explanation
AI Ready
💡 Suggestion Tab to accept Esc to dismiss
// Output will appear here after running code
Code Editor Closed
Click the red button to reopen