Solved. Should be done by recursion

Logic: @srinidhij