LeetCode 2553: Separate the Digits in an Array – Java Solution Explained (2 Easy Approaches)
LeetCode 2553: Separate the Digits in an Array – Java Solution Explained (2 Easy Approaches)

LeetCode 2553: Separate the Digits in an Array – Java Solution Explained (2 Easy Approaches)

Learn how to solve LeetCode 2553 Separate the Digits in an Array using Java with 2 easy approaches. Includes intuition, dry run, time complexity, interview tips, common mistakes, and optimized mathematical solution.

3 views
0
0
Listen to articleAudio version
brillicode ad banner

Introduction

In coding interviews and competitive programming, many problems test how well you can manipulate numbers and arrays together. One such beginner-friendly problem is LeetCode 2553 – Separate the Digits in an Array.

In this problem, we are given an integer array, and we need to separate every digit of every number while maintaining the original order.

This problem is excellent for practicing:

  1. Array traversal
  2. Digit extraction
  3. Reverse processing
  4. ArrayList usage in Java
  5. Thinking about order preservation

Problem Link

🔗 Problem

LeetCode 2553: Separate the Digits in an Array

Problem Statement

Given an array of positive integers nums, return an array containing all digits of each integer in the same order they appear.

Example

Input:

nums = [13,25,83,77]

Output:

[1,3,2,5,8,3,7,7]

Intuition

The main challenge is:

  1. Extract digits from each number
  2. Preserve the original left-to-right order

Normally, extracting digits using % 10 gives digits in reverse order.

Example:

83 → 3 → 8

So we need a way to restore the correct order.

Approach 1 – Using String Conversion

Idea

Convert every number into a string and then traverse each character.

This is the simplest and most beginner-friendly approach.

Algorithm

  1. Traverse every number in the array.
  2. Convert the number into a string.
  3. Traverse each character of the string.
  4. Convert character back to integer.
  5. Store digits into ArrayList.
  6. Convert ArrayList to array.

Java Code – String Approach

class Solution {
public int[] separateDigits(int[] nums) {

ArrayList<Integer> list = new ArrayList<>();

for (int num : nums) {

String str = String.valueOf(num);

for (char ch : str.toCharArray()) {
list.add(ch - '0');
}
}

int[] ans = new int[list.size()];

for (int i = 0; i < list.size(); i++) {
ans[i] = list.get(i);
}

return ans;
}
}

Dry Run (String Approach)

Input:

nums = [13,25]

Step 1

13 → "13"

Digits added:

1, 3

Step 2

25 → "25"

Digits added:

2, 5

Final Array:

[1,3,2,5]

Time Complexity & Space Complexity

Time Complexity

O(N × D)

Where:

  1. N = number of elements
  2. D = number of digits

Space Complexity

O(N × D)

For storing digits.

Approach 2 – Mathematical Digit Extraction (Optimal Without String)

This is the approach you implemented in your code.

Instead of converting numbers into strings, we extract digits mathematically using:

digit = num % 10
num = num / 10

But digits come in reverse order.

To fix this:

  1. Traverse the original array from back to front
  2. Store extracted digits
  3. Reverse the final result

This avoids string conversion completely.

Intuition Behind Reverse Traversal

Suppose:

nums = [13,25]

If we traverse from the end:

25 → 5,2
13 → 3,1

Stored list:

[5,2,3,1]

Now reverse the list:

[1,3,2,5]

Correct answer achieved.

Java Code – Mathematical Approach

class Solution {

public int[] separateDigits(int[] nums) {

ArrayList<Integer> list = new ArrayList<>();

for (int i = nums.length - 1; i >= 0; i--) {

if (nums[i] < 10) {
list.add(nums[i]);
}
else {

int val = nums[i];

while (val != 0) {

int digit = val % 10;
val = val / 10;

list.add(digit);
}
}
}

int[] ans = new int[list.size()];

int k = 0;

for (int i = list.size() - 1; i >= 0; i--) {
ans[k++] = list.get(i);
}

return ans;
}
}

Dry Run (Mathematical Approach)

Input:

nums = [13,25,83]

Traverse from Back

83

Digits extracted:

3, 8

List:

[3,8]

25

Digits extracted:

5,2

List:

[3,8,5,2]

13

Digits extracted:

3,1

List:

[3,8,5,2,3,1]

Reverse Final List

[1,3,2,5,8,3]

Correct answer.

Time Complexity Analysis & Space Complexity

Time Complexity

O(N × D)

Because every digit is processed once.

Space Complexity

O(N × D)

For storing final digits.

Which Approach is Better?

ApproachAdvantagesDisadvantages
String ConversionEasy to understandUses extra string conversion
Mathematical ExtractionBetter DSA practiceSlightly harder logic

Interview Perspective

In interviews:

  1. Beginners should first explain the string approach.
  2. Then discuss optimization using mathematical extraction.

Interviewers like when candidates:

  1. Understand digit manipulation
  2. Think about order preservation
  3. Compare multiple approaches

Common Mistakes

1. Forgetting Reverse Order

Using % 10 extracts digits backward.

Example:

123 → 3,2,1

You must reverse later.

2. Not Handling Single Digit Numbers

Single digit numbers should directly be added.

3. Character Conversion Mistake

Wrong:

list.add(ch);

Correct:

list.add(ch - '0');

Frequently Asked Questions (FAQs)

Q1. Why do digits come in reverse order?

Because % 10 always extracts the last digit first.

Example:

123 % 10 = 3

Q2. Can we solve this without ArrayList?

Yes, but ArrayList makes dynamic storage easier.

Q3. Which approach is more optimal?

Both have similar complexity.

Mathematical extraction avoids string conversion and is preferred in interviews.

Q4. Is this problem important for interviews?

Yes. It teaches:

  1. Number manipulation
  2. Order handling
  3. Array traversal
  4. Basic optimization thinking

Conclusion

LeetCode 2553 is a simple yet valuable beginner problem for understanding:

  1. Digit extraction
  2. Array handling
  3. Reverse traversal
  4. Order preservation

You learned two approaches:

  1. String Conversion Approach
  2. Mathematical Digit Extraction Approach

The mathematical solution is especially useful because it strengthens core DSA concepts and improves problem-solving skills for interviews.

If you're preparing for coding interviews in Java, this is a great problem to master before moving to harder digit manipulation questions.

Ai Assistant Kas