BackmediumSorting

Root Sum Sort Solution

Problem Statement

Sort an array of integers in ascending order based on the digital root, which is the value obtained by recursive sum of its digits until only a single digit remains

Example 1
Input
[13, 25, 67, 89, 12]
Output
[12,13,67,25,89]

Explanation: The digital root of 12 is 3 (1+2=3), of 13 is 4 (1+3=4), of 25 is 7 (2+5=7), of 67 is 13 (6+7=13, 1+3=4), and of 89 is 17 (8+9=17, 1+7=8). So, the array is sorted in ascending order based on the digital roots.

Example 2
Input
[100, 9, 200, 38]
Output
[100,200,38,9]

Explanation: The digital root of 9 is 9, of 100 is 1 (1+0+0=1), of 200 is 2 (2+0+0=2), and of 38 is 2 (3+8=11, 1+1=2). So, the array is sorted in ascending order based on the digital roots.

Constraints

  • 1 <= array length <= 10^5
  • 0 <= array elements <= 10^6
Live Compiler
Loading...
Test Cases & Output
🔒 Sign up to run your code

🚀 Practice this problem

Run code, get AI hints & track streak

Sign Up Free