Separate the Digits in an Array - Problem
Given an array of positive integers, your task is to separate each number into its individual digits and return a new array containing all these digits in the same order they appear.

Think of it like taking apart a stack of multi-digit numbers and laying out each digit individually. For example, if you have the number 1234, you would separate it into [1, 2, 3, 4].

Goal: Transform an array of integers into an array of their constituent digits.
Input: An array of positive integers
Output: An array of digits maintaining the original order

Example: [13, 25, 83, 77] becomes [1, 3, 2, 5, 8, 3, 7, 7]

Input & Output

example_1.py โ€” Basic Case
$ Input: nums = [13, 25, 83, 77]
โ€บ Output: [1, 3, 2, 5, 8, 3, 7, 7]
๐Ÿ’ก Note: Each number is broken into its digits: 13โ†’[1,3], 25โ†’[2,5], 83โ†’[8,3], 77โ†’[7,7]
example_2.py โ€” Single Digits
$ Input: nums = [7, 1, 3, 9]
โ€บ Output: [7, 1, 3, 9]
๐Ÿ’ก Note: Single-digit numbers remain unchanged as they have only one digit each
example_3.py โ€” Mixed Lengths
$ Input: nums = [1, 234, 56, 7890]
โ€บ Output: [1, 2, 3, 4, 5, 6, 7, 8, 9, 0]
๐Ÿ’ก Note: Numbers of different lengths: 1โ†’[1], 234โ†’[2,3,4], 56โ†’[5,6], 7890โ†’[7,8,9,0]

Constraints

  • 1 โ‰ค nums.length โ‰ค 1000
  • 1 โ‰ค nums[i] โ‰ค 105
  • All integers are positive

Visualization

Tap to expand
Digit Separation ProcessInput: [13, 25, 83, 77] โ†’ Output: [1, 3, 2, 5, 8, 3, 7, 7]Step 1: Input Array13258377Step 2: String Conversion"13""25""83""77"Step 3: Digit Extraction13258377Final Result[1, 3, 2, 5, 8, 3, 7, 7]
Understanding the Visualization
1
Input Processing
Take each number from the input array
2
String Conversion
Convert the number to string format for easy digit access
3
Digit Extraction
Extract each character and convert back to integer
4
Result Building
Add each digit to the result array in order
Key Takeaway
๐ŸŽฏ Key Insight: String conversion provides the simplest way to access individual digits, making the code readable and maintainable while achieving optimal time complexity.
Asked in
Amazon 25 Google 18 Microsoft 12 Meta 8
23.6K Views
Medium Frequency
~15 min Avg. Time
892 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