Skip to main content

Separate the Digits in an Array - Solution & Explanation

EasyArraySimulation14 min readAsked at: Microsoft, Meta, Google
Practice this problem

Problem Statement

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 <= 1000
  • 1 <= nums[i] <= 105

Approach Overview

Problem Overview: You receive an array of positive integers. For every number, split it into its individual digits and append those digits to a result array while preserving the original order. For example, [13, 25] becomes [1, 3, 2, 5]. The challenge is simply extracting digits efficiently while keeping the sequence correct.

This problem falls under basic Array traversal and Simulation. You process each number one by one and simulate digit extraction.

Approach 1: String Conversion Method (O(n·k) time, O(n·k) space)

The simplest implementation converts each number into a string, then iterates over its characters. Each character represents a digit, so convert it back to an integer and append it to the result list. If n is the number of integers and k is the average number of digits per integer, the algorithm performs roughly n × k operations.

This approach is popular because it is extremely readable and avoids manual digit manipulation. Most high-level languages provide fast string iteration, so the performance is usually more than sufficient for interview constraints. The tradeoff is extra temporary memory used by the string representation, which makes the space complexity O(n·k) including the output.

Approach 2: Mathematical Division Method (O(n·k) time, O(n·k) space)

This method extracts digits using arithmetic instead of string operations. For each number, repeatedly apply num % 10 to obtain the last digit and num / 10 to remove it. Because this produces digits in reverse order, store them temporarily (for example in a stack or small list) and then append them back to the result in reverse.

The algorithm still processes every digit exactly once, so the time complexity remains O(n·k). Space complexity is also O(n·k) due to the output plus a small temporary buffer per number. This approach is useful when you want tighter control over numeric operations or when avoiding string conversions is preferred.

Recommended for interviews: Both approaches are acceptable because the complexity is identical. The string conversion solution demonstrates clean thinking and quick implementation, which interviewers appreciate for an easy problem. The mathematical division method shows deeper understanding of digit manipulation and is sometimes preferred in lower-level environments where avoiding string conversion matters.

Approach 1: String Conversion Method

This approach involves converting each integer in the array to a string, then iterating over each character in the string to convert it back to an integer. This allows us to access each digit in its original order effortlessly.

This C solution uses a character array to convert each number into a string. We iterate through each character of the string, convert it to an integer, and append it to the resulting array. Clarification of the logic is managed by utilizing snprintf for conversion and indexing through the string to build the desired output.

Code

C

C++

Java

Python

C#

JavaScript

Complexity

Time Complexity: O(n * k) where n is the number of elements and k is the number of digits in each number (up to 5).
Space Complexity: O(n * k) mostly due to storing digits in the output array.

Try this approach in the editor →

Approach 2: Mathematical Division Method

This approach involves using mathematical operations to extract digits from each number without converting it to a string. The core idea retains the remainder of number by using modulo operation followed by integer division to remove processed digits from the right.

This solution first reverses each number by iteratively extracting digits via modulo and shifting them via integer division. The reversal happens twice to maintain sequence, ensuring digits return in the same order as input.

Code

C

C++

Java

Python

C#

JavaScript

Complexity

Time Complexity: O(n * k) since each digit is processed twice.
Space Complexity: O(n * k) though original number storage is minimized.

Try this approach in the editor →

Approach 3: Simulation

Split each number in the array into digits, then put the split numbers into the answer array in order.

The time complexity is O(n times log_{10} M), and the space complexity is O(n times log_{10} M). Where n is the length of the array nums, and M is the maximum value in the array nums.

Code

Python

Java

C++

Go

TypeScript

Rust

C

Try this approach in the editor →

Approach 4: Default Approach

Code

Rust

Try this approach in the editor →

Complexity Comparison

ApproachComplexity
String Conversion Method

Time Complexity: O(n * k) where n is the number of elements and k is the number of digits in each number (up to 5).
Space Complexity: O(n * k) mostly due to storing digits in the output array.

Mathematical Division Method

Time Complexity: O(n * k) since each digit is processed twice.
Space Complexity: O(n * k) though original number storage is minimized.

Simulation
Default Approach

Detailed Complexity Analysis

ApproachTimeSpaceWhen to Use
String Conversion MethodO(n·k)O(n·k)Best for readability and fast implementation in interviews
Mathematical Division MethodO(n·k)O(n·k)When avoiding string conversion or demonstrating numeric digit extraction

Video Solution

Separate the Digits in an Array || LeetCode Biweekly Contest 97 || Explanation with SolutionLearn to Code1,210 views views

Watch 9 more video solutions →

Frequently Asked Questions

Is Separate the Digits in an Array easy or hard?
Separate the Digits in an Array is classified as an Easy problem. It mainly tests basic array traversal and simple digit extraction logic rather than complex data structures or algorithms.
Separate the Digits in an Array Python/Java solution
In Python or Java, the most common solution converts each number to a string and iterates through the characters to append digits to a result list. The same logic works in C++, JavaScript, and C#. The complexity remains O(n·k) time and O(n·k) space.
How to solve Separate the Digits in an Array in O(n)?
Strictly speaking, the complexity depends on the number of digits processed. The practical optimal solution is O(n·k), which behaves close to linear relative to total digits. Iterate through the array and extract digits using either string iteration or repeated modulo and division operations.
What is the best approach for Separate the Digits in an Array?
The string conversion approach is usually the best for interviews because it is short and easy to reason about. Convert each integer to a string, iterate through its characters, and append each digit to the result array. The algorithm runs in O(n·k) time where k is the number of digits per number.
Is Separate the Digits in an Array asked at Google/Amazon/Meta?
This problem represents an easy-level array simulation pattern commonly used in screening rounds and coding practice platforms. Variations involving digit manipulation or array transformations can appear in interviews at companies like Amazon or Google as warm-up questions.
What data structure is used in Separate the Digits in an Array?
The primary data structure is an array or dynamic list used to store the resulting digits. Some implementations also use a temporary stack or list when extracting digits mathematically to restore the correct order.
What is the time complexity of Separate the Digits in an Array?
The time complexity is O(n·k), where n is the number of elements in the input array and k is the number of digits in each number. Every digit is processed exactly once when extracting digits from each integer.

Ready to solve this problem?

Practice Separate the Digits in an Array with our built-in code editor and test cases.

Practice on FleetCode