DDSA Solutions

2553. Separate the Digits in an Array

Time: O(n * digits)
Space: O(n * digits)

Problem Overview

Separate digits of each number and insert in order.

Intuition

Separate digits of each number and insert in order. For each number, expand its digits.

Algorithm

  1. 1For each num: extract digits (from most significant). Insert all digits in order.

Common Pitfalls

  • Maintain relative order of original elements, and digit order within each element.
2553.cs
C#
// Approach: For each number extract digits in order and append to result list.
// Time: O(n * digits) Space: O(n * digits)

public class Solution
{
    public int[] SeparateDigits(int[] nums)
    {
        List<int> ans = new List<int>();

        foreach (var num in nums)
        {
            foreach (var c in num.ToString())
                ans.Add(c - '0');
        }

        return ans.ToArray();
    }
}
Was this solution helpful?

Related Problems