I\'ve come up with a solution for this question above, but I wanted to see if there were any better algorithms that can be used to accomplish the same task more efficiently