Separate the Digits in an Array - Problem

Given an array of positive integers nums, return an array answer that consists of the digits of each integer in nums after separating them in the same order they appear in nums.

To separate the digits of an integer is to get all the digits it has in the same order.

For example, for the integer 10921, the separation of its digits is [1,0,9,2,1].

Input & Output

Example 1 — Basic Case
$ Input: nums = [13,25,83]
Output: [1,3,2,5,8,3]
💡 Note: Separate digits: 13 → [1,3], 25 → [2,5], 83 → [8,3]. Combine: [1,3,2,5,8,3]
Example 2 — Single Digits
$ Input: nums = [7,1,3,9]
Output: [7,1,3,9]
💡 Note: Single digit numbers remain unchanged: [7,1,3,9]
Example 3 — Larger Numbers
$ Input: nums = [1000,23]
Output: [1,0,0,0,2,3]
💡 Note: 1000 → [1,0,0,0], 23 → [2,3]. Combined: [1,0,0,0,2,3]

Constraints

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

Visualization

Tap to expand
Separate the Digits in an Array Mathematical Digit Extraction Approach INPUT nums = [13, 25, 83] 13 index 0 25 index 1 83 index 2 Each number has digits: 1 3 2 5 8 3 3 positive integers to be separated ALGORITHM STEPS 1 Initialize Result Create empty answer array 2 For Each Number Extract digits using % 10 3 Reverse Digits Maintain original order 4 Append to Result Add digits to answer[] Example: num = 13 13 % 10 = 3 (last digit) 13 / 10 = 1 1 % 10 = 1 (first digit) Reverse: [3,1] --> [1,3] FINAL RESULT Separation Process: 13 --> 1 3 25 --> 2 5 83 --> 8 3 Output Array: [1,3,2,5,8,3] 1 3 2 5 8 3 OK - 6 digits extracted Key Insight: Use modulo (% 10) to extract the last digit and integer division (/ 10) to remove it. Since digits are extracted in reverse order (right to left), reverse them before adding to result. Time: O(n * d) where d = avg digits per number | Space: O(total digits) TutorialsPoint - Separate the Digits in an Array | Mathematical Digit Extraction
Asked in
Amazon 15 Google 12
12.0K Views
Medium Frequency
~10 min Avg. Time
450 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