Introduction
In coding interviews and competitive programming, many problems test how well you can manipulate numbers and arrays together. One such beginner-friendly problem is LeetCode 2553 – Separate the Digits in an Array.
In this problem, we are given an integer array, and we need to separate every digit of every number while maintaining the original order.
This problem is excellent for practicing:
- Array traversal
- Digit extraction
- Reverse processing
- ArrayList usage in Java
- Thinking about order preservation
Problem Link
🔗 Problem
LeetCode 2553: Separate the Digits in an Array
Problem Statement
Given an array of positive integers nums, return an array containing all digits of each integer in the same order they appear.
Example
Input:
Output:
Intuition
The main challenge is:
- Extract digits from each number
- Preserve the original left-to-right order
Normally, extracting digits using % 10 gives digits in reverse order.
Example:
So we need a way to restore the correct order.
Approach 1 – Using String Conversion
Idea
Convert every number into a string and then traverse each character.
This is the simplest and most beginner-friendly approach.
Algorithm
- Traverse every number in the array.
- Convert the number into a string.
- Traverse each character of the string.
- Convert character back to integer.
- Store digits into ArrayList.
- Convert ArrayList to array.
Java Code – String Approach
Dry Run (String Approach)
Input:
Step 1
Digits added:
Step 2
Digits added:
Final Array:
Time Complexity & Space Complexity
Time Complexity
Where:
N= number of elementsD= number of digits
Space Complexity
For storing digits.
Approach 2 – Mathematical Digit Extraction (Optimal Without String)
This is the approach you implemented in your code.
Instead of converting numbers into strings, we extract digits mathematically using:
But digits come in reverse order.
To fix this:
- Traverse the original array from back to front
- Store extracted digits
- Reverse the final result
This avoids string conversion completely.
Intuition Behind Reverse Traversal
Suppose:
If we traverse from the end:
Stored list:
Now reverse the list:
Correct answer achieved.
Java Code – Mathematical Approach
Dry Run (Mathematical Approach)
Input:
Traverse from Back
83
Digits extracted:
List:
25
Digits extracted:
List:
13
Digits extracted:
List:
Reverse Final List
Correct answer.
Time Complexity Analysis & Space Complexity
Time Complexity
Because every digit is processed once.
Space Complexity
For storing final digits.
Which Approach is Better?
| Approach | Advantages | Disadvantages |
| String Conversion | Easy to understand | Uses extra string conversion |
| Mathematical Extraction | Better DSA practice | Slightly harder logic |
Interview Perspective
In interviews:
- Beginners should first explain the string approach.
- Then discuss optimization using mathematical extraction.
Interviewers like when candidates:
- Understand digit manipulation
- Think about order preservation
- Compare multiple approaches
Common Mistakes
1. Forgetting Reverse Order
Using % 10 extracts digits backward.
Example:
You must reverse later.
2. Not Handling Single Digit Numbers
Single digit numbers should directly be added.
3. Character Conversion Mistake
Wrong:
Correct:
Frequently Asked Questions (FAQs)
Q1. Why do digits come in reverse order?
Because % 10 always extracts the last digit first.
Example:
Q2. Can we solve this without ArrayList?
Yes, but ArrayList makes dynamic storage easier.
Q3. Which approach is more optimal?
Both have similar complexity.
Mathematical extraction avoids string conversion and is preferred in interviews.
Q4. Is this problem important for interviews?
Yes. It teaches:
- Number manipulation
- Order handling
- Array traversal
- Basic optimization thinking
Conclusion
LeetCode 2553 is a simple yet valuable beginner problem for understanding:
- Digit extraction
- Array handling
- Reverse traversal
- Order preservation
You learned two approaches:
- String Conversion Approach
- Mathematical Digit Extraction Approach
The mathematical solution is especially useful because it strengthens core DSA concepts and improves problem-solving skills for interviews.
If you're preparing for coding interviews in Java, this is a great problem to master before moving to harder digit manipulation questions.





