# Teaching Kids Programming – Algorithms to Separate the Digits of a Number Array

Teaching Kids Programming – Algorithms to Separate the Digits of a Number Array | ninjasquad

Teaching Kids Programming: Videos on Data Structures and Algorithms

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.

Constraints:
1 <= nums.length <= 1000
1 <= nums[i] <= 10^5

Hints:
Convert each number into a list and append that list to the answer.
You can convert the integer into a string to do that easily.

### Algorithms to Separate the Digits of a Number Array

We can convert each numbers to string, turn them to list of digit characters, then, add them separately in the form of numbers to the answer list. The time complexity is O(N) where N is the number of total digits. The space complexity is O(D) where D is the maximum length of digits per number.

 ```1 2 3 4 5 6 ``` ```class Solution:     def separateDigits(self, nums: List[int]) -> List[int]:         ans = []         for i in nums:             ans.extend(map(int, list(str(i))))         return ans```
```class Solution:
def separateDigits(self, nums: List[int]) -> List[int]:
ans = []
for i in nums:
ans.extend(map(int, list(str(i))))
return ans```

We can extract each digit in the reverse order by math operator % mod and // integer division. Then we keep the digits in a temp array, and append the reversed version of it. Same time/space complexity as the first approach.

 ```1 2 3 4 5 6 7 8 9 10 ``` ```class Solution:     def separateDigits(self, nums: List[int]) -> List[int]:         ans = []         for i in nums:             cur = []             while i:                 cur.append(i % 10)                 i //= 10             ans.extend(cur[::-1])         return ans```
```class Solution:
def separateDigits(self, nums: List[int]) -> List[int]:
ans = []
for i in nums:
cur = []
while i:
cur.append(i % 10)
i //= 10
ans.extend(cur[::-1])
return ans```

We can iterate the numbers in the reverse order, append the reverse version of each number, and then reverse the answer. THe time complexity is O(N) and space complexity is O(1). We can reverse an array using Two Pointer Algorithm.

 ```1 2 3 4 5 6 7 8 ``` ```class Solution:     def separateDigits(self, nums: List[int]) -> List[int]:         ans = []         for i in nums[::-1]:             while i:                 ans.append(i % 10)                 i //= 10         return ans[::-1]```
```class Solution:
def separateDigits(self, nums: List[int]) -> List[int]:
ans = []
for i in nums[::-1]:
while i:
ans.append(i % 10)
i //= 10
return ans[::-1]```

And here is the one liner e.g. iterating each number, and iterating the digits and append to the answer (same idea as the first approach).

 ```1 2 3 ``` ```class Solution:     def separateDigits(self, nums: List[int]) -> List[int]:         return [int(c) for a in nums for c in str(a)]```
```class Solution:
def separateDigits(self, nums: List[int]) -> List[int]:
return [int(c) for a in nums for c in str(a)]```

GD Star Rating