After seeing the solutions of others, I noticed that their time complexity is O(n**2). I think my solution is O(nlog(n)) since sorting an array takes that time and then a linear scan will check the result.
Here's my brainfuck of a solution. Apparently I wanted to use the Chinese Remainder Theorem for modulo 10 thus giving us a simplified us something easier to compute such as (6a_1 + 5a_2) % 10 where a_1 is the remainder divided by 5 and a_2 is the remainder when divided by 2. The logic is kinda nuts and a lot of repetetive code. I'll improve on this next time.
My solution is longer than most but I think the idea is simply prime factorization.
This is just the travelling salesman problem. An NP-Hard problem with no polynomial time solution.
Probably the fastest and the simplest at the same time!
After seeing the solutions of others, I noticed that their time complexity is O(n**2). I think my solution is O(nlog(n)) since sorting an array takes that time and then a linear scan will check the result.
Here's my brainfuck of a solution. Apparently I wanted to use the Chinese Remainder Theorem for modulo 10 thus giving us a simplified us something easier to compute such as (6a_1 + 5a_2) % 10 where a_1 is the remainder divided by 5 and a_2 is the remainder when divided by 2. The logic is kinda nuts and a lot of repetetive code. I'll improve on this next time.