alternate solution

Time complexity efficient solution