Hey all, I’m back at it with another algorithm breakdown. This is a format I’ve done before so if you like it you can check out a similar article I published here, leave a comment, or connect on LinkedIn. I have a lot of fun figuring out and breaking down problems so it’s always a pleasure to hear other perspectives. Anyway, enough on that, let’s get to it.
In this problem, we have to write an algorithm that can check if the integer input is a narcissistic number. Narcissistic numbers are whole numbers, or integers, whose digits each to the nth power sum to equal the whole number or integer itself, where nth is the number of digits.
You can find a list of Narcissistic numbers here.
Lastly, after our loop breaks, we can compare our input with comparisonNum and conditional return a message telling us whether or not the input is a narcissistic number.
Then we can clean it up a bit to make it presentable.
To test this solution I ran the following tests:
And the time complexity of our solution is O(n), linear time, where n is the length of the array we make from the digits of our input because we iterate over it once to find the solution.
Our space complexity is also O(n), linear space, where n is digitArray which, in our solution, is relative to the number of digits in the input integer.
Thanks for taking the time to read this, I hope you found it useful or perhaps took away some useful methods. If you can come up with a less complex solution I would love to hear about it.