Separate the Digits in an Array
EasyPrompt
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].
Example 1:
Input: nums = [13,25,83,77]
Output: [1,3,2,5,8,3,7,7]
Explanation:
- The separation of 13 is [1,3].
- The separation of 25 is [2,5].
- The separation of 83 is [8,3].
- The separation of 77 is [7,7].
answer = [1,3,2,5,8,3,7,7]. Note that answer contains the separations in the same order.Example 2:
Input: nums = [7,1,3,9]
Output: [7,1,3,9]
Explanation: The separation of each integer in nums is itself.
answer = [7,1,3,9].
Constraints:
1 <= nums.length <= 10001 <= nums[i] <= 105
Approaches
3 approaches with complexity analysis and trade-offs.
This approach uses mathematical operations (modulo and division) to extract digits from each number. Since this process extracts digits from right to left (least significant to most significant), a temporary list is used to store these digits, which is then reversed to get the correct order before being added to the final result.
Algorithm
- Initialize an empty list of integers,
answerList. - Iterate through each integer
numin the input arraynums. - Create a temporary list,
tempDigits. - While
numis greater than 0:- Extract the last digit using
num % 10. - Add the digit to
tempDigits. - Update the number by integer division:
num = num / 10.
- Extract the last digit using
- Reverse the
tempDigitslist to get the digits in the correct order. - Add all digits from the reversed
tempDigitslist toanswerList. - After processing all numbers, convert
answerListinto an array and return it.
Walkthrough
The core idea is to process each number individually. For a given number, we can easily extract its digits in reverse order by repeatedly taking the number modulo 10 (to get the last digit) and then dividing by 10 (to remove the last digit). We store these reversed digits in a temporary list. Once all digits of a number are extracted, we reverse this temporary list to restore the original order and then append these correctly ordered digits to our main result list. This process is repeated for every number in the input array.
import java.util.ArrayList;import java.util.Collections;import java.util.List; class Solution { public int[] separateDigits(int[] nums) { List<Integer> answerList = new ArrayList<>(); for (int num : nums) { // Per the constraints, nums[i] >= 1, so no need to handle num == 0. List<Integer> tempDigits = new ArrayList<>(); int currentNum = num; while (currentNum > 0) { tempDigits.add(currentNum % 10); currentNum /= 10; } Collections.reverse(tempDigits); answerList.addAll(tempDigits); } int[] result = new int[answerList.size()]; for (int i = 0; i < answerList.size(); i++) { result[i] = answerList.get(i); } return result; }}Complexity
Time
O(N * K), where `N` is the number of elements in `nums` and `K` is the maximum number of digits in an element. For each number, we perform O(K) operations to extract digits and O(K) to reverse the temporary list.
Space
O(N * K), where `N` is the number of elements in `nums` and `K` is the maximum number of digits in an element. This is for the final result list. Additionally, O(K) space is used for the temporary list for each number.
Trade-offs
Pros
Avoids string conversions, which can be beneficial in performance-critical environments that penalize type casting.
The logic for extracting digits (modulo and division) is a fundamental and efficient arithmetic technique.
Cons
Requires creating a new temporary list for each number in the input array.
The reversal step for each number's digits adds computational overhead.
The overall logic is more complex compared to more direct approaches.
Solutions
Solution
class Solution {public int[] separateDigits(int[] nums) { List<Integer> res = new ArrayList<>(); for (int x : nums) { List<Integer> t = new ArrayList<>(); for (; x > 0; x /= 10) { t.add(x % 10); } Collections.reverse(t); res.addAll(t); } int[] ans = new int[res.size()]; for (int i = 0; i < ans.length; ++i) { ans[i] = res.get(i); } return ans; }}Video walkthrough
Newsletter
One sharp idea, every week
System design and interview prep — short enough to finish.
No spam. Unsubscribe anytime.
Practice
Same difficulty — related problems to reinforce the pattern.