DEV Community

Cover image for Solving LeetCode 2553: Separate the Digits in an Array using Java
Jerin
Jerin

Posted on

Solving LeetCode 2553: Separate the Digits in an Array using Java

Difficulty: Easy
Topics: Array, Simulation
Platform: Leetcode

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].

Problem Statement Simplified

Deconstruct each digits in nums array and return inorder in answer array.
Mistakes and Learning
Added the modules value to final array list without reversing it - Learned: Created another arraylist and added the values to it and then reversed and added to the main arraylist

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.
Enter fullscreen mode Exit fullscreen mode

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].
Key Insight
Loop through each value in the array.
Take the modules of each digit and add them to an arraylist.
Reverse it and add it to the arraylist.
Enter fullscreen mode Exit fullscreen mode

Algorithm

  1. Initialize 2 arraylists.
  2. Loop through each digits of nums array.
  3. Initialize an integer to store the deconstructed values.
  4. While the nums[i]>0.
  5. Assign 10 modules of nums[i] to integer value.
  6. Add that value to an arraylist. 
  7. Divide the nums[i] by 10.
  8. End while.
  9. Reverse the arraylist that stored the value.
  10. Add the reversed arraylist to another arraylist.
  11. Clear the reversed arraylist.
  12. Convert arraylist to int[].
  13. return answer.

Algorithm in simple words

Initialize 2 arraylists one to save the array and another to save the modules value of element having more than single digit and reverse it. 
Loop through nums and initialize an integer to assign the modules value. While the nums[i] is greater than 0 so that when the nums[i] is < or = 0 it will go for another for loop iteration. The value becomes nums[i]%10. and add this value to the 2nd arraylist. Then nums[i]=nums[i]/10 - this will remove the last digit of the element. 
After the while loop ends we will reverse the arraylist and add it to the 1st arraylist, and clear the 2nd arraylist for next element.

Java code

class Solution {
    public int[] separateDigits(int[] nums) {
        ArrayList <Integer> answers = new ArrayList<>();
         ArrayList <Integer> answers2 = new ArrayList<>();
        for (int i=0;i<nums.length;i++){
            int value=0;
            while(nums[i]>0){
                value=nums[i]%10;
                answers2.add(value);
                nums[i]=nums[i]/10;
            }
            Collections.reverse(answers2);
            answers.addAll(answers2);
            answers2.clear();
        }
        int[] answer = answers.stream()
                         .mapToInt(i -> i)
                         .toArray();
        return answer;
    }
}
Enter fullscreen mode Exit fullscreen mode

Time & Space Complexity

Time Complexity: O(nm)
Space Complexity: O(nm)

Top comments (0)