 Separate the Digits in an Array | 🚀 Beginner-Friendly | 💻 Java, JavaScript, C, C++, Python
IntuitionThe problem requires extracting individual digits from each number in the given array while maintaining their order. The simplest approach is to iterat
# Intuition
<!-- Describe your first thoughts on how to solve this problem. -->
The problem requires extracting individual digits from each number in the given array while maintaining their order. The simplest approach is to iterate through the array, break each number into its digits, and append them to the result while ensuring the order remains correct.

# Approach
**1)Iterate through the given array nums.
2)For each number:**

    1)Extract its digits using modulo (% 10) and division (/ 10).
    2)Store the digits in a temporary list to preserve their order.
    3)Reverse the extracted digits to maintain the original sequence.
    4)Append them to the final result list.
**3)Return the final list or array.**
<!-- Describe your approach to solving the problem. -->


# Complexity
- Time complexity:
- Each number is broken down into its digits, which takes O(d) time, where d is the number of digits.
- Since we process each number in nums, the overall complexity is O(N * D), where N is the number of elements and D is the average number of digits per number.
- In the worst case, D = log₁₀(max(nums)), so we can approximate this as O(N log M) where M is the largest number.
<!-- Add your time complexity here, e.g. $$O(n)$$ -->

- Space complexity:
- The extra space is used to store the digits before appending to the final list.
- If we ignore the output space, additional space is O(D) for each number, leading to an overall complexity of O(N * D).
- The final result array requires O(K) space, where K is the total number of digits in all numbers.
<!-- Add your space complexity here, e.g. $$O(n)$$ -->

# Code
```java []
class Solution {
    public int[] separateDigits(int[] nums) {
        List<Integer> temp = new ArrayList<>();
        
        for(int i = nums.length - 1; i >= 0; i--) {
           
            while(nums[i] > 0) {
                temp.add(nums[i] % 10);
                nums[i] /= 10;
            }
        }
        
        int[] res = new int[temp.size()];
        
        for(int i = 0; i < res.length; i++) {
            res[i] = temp.get(res.length - i - 1);
        }
        return res;
    }
}
```
```javascript []
class Solution {
    separateDigits(nums) {
        let result = [];
        
        for (let num of nums) {
            let digits = Array.from(String(num), Number);
            result.push(...digits);
        }
        
        return result;
    }
}

```
```python []
class Solution:
    def separateDigits(self, nums: list[int]) -> list[int]:
        result = []
        
        for num in nums:
            digits = list(map(int, str(num)))
            result.extend(digits)
        
        return result

```
```c []
#include <stdio.h>
#include <stdlib.h>

int* separateDigits(int* nums, int numsSize, int* returnSize) {
    int* temp = (int*)malloc(numsSize * 10 * sizeof(int));
    int count = 0;

    for (int i = 0; i < numsSize; i++) {
        int num = nums[i], digits[10], dSize = 0;

        while (num > 0) {
            digits[dSize++] = num % 10;
            num /= 10;
        }

        for (int j = dSize - 1; j >= 0; j--) {
            temp[count++] = digits[j];
        }
    }

    *returnSize = count;
    return temp;
}

```
```C++ []
#include <vector>

class Solution {
public:
    std::vector<int> separateDigits(std::vector<int>& nums) {
        std::vector<int> result;
        
        for (int num : nums) {
            std::vector<int> digits;
            
            while (num > 0) {
                digits.push_back(num % 10);
                num /= 10;
            }
            
            reverse(digits.begin(), digits.end());
            result.insert(result.end(), digits.begin(), digits.end());
        }
        
        return result;
    }
};



```
**If you found this solution helpful, don't forget to give it an upvote! 👍 Your support keeps me motivated to create more beginner-friendly and efficient solutions. Happy coding! 🚀**

![image.png](https://assets.leetcode.com/users/images/5f37000c-f6f9-4aaa-b598-2686c7248a1c_1741073600.842762.png)

['C', 'Python', 'C++', 'Java', 'JavaScript']