Currently tackling a Codewars challenge.
The task involves finding the maximum possible result after performing a rotation on a given number.
This rotation is unique in that 'n' number of digits will remain fixed after each rotation, with 'n' increasing as more rotations are performed.
For instance, when starting with the number 56789:
67895
(6 remains static, while 7 is rotated to the end)
68957
(6 and 8 remain stable, while 9 is moved to the back)
68579
(6, 8, and 5 stay unchanged, while 7 is shifted to the end)
68597
(6, 8, 5, and 9 do not move - no further rotations can occur)
The goal is to return the highest value achieved from these rotations - 68957
.
Below is my current implementation:
function maxRot(n) {
let listOfNums = [];
let array = Array.from(n.toString());
let num = 0;
while (num < array.length -1) {
let number = array.splice(num, 1);
array.push(Number(number));
listOfNums.push(Number(array.join("")));
num++;
}
listOfNums.sort((a, b) => b - a);
return listOfNums[0];
}
console.log(maxRot(56789));
However, this solution is failing roughly half of the tests on Codewars platform.
The approach involves removing one digit at a time and adding it to the end of the array, then storing the updated array in a listOfNums
array. Finally, sorting this array in descending order and returning the first element.
I am unsure about what else could be attempted to improve the results.
Once again, the link to the Codewars challenge can be found here.