What could be the more efficient way of solving the 'sequence land' problem?

algorithm
graph

#1

I have been trying to solve the problem SEQUENCE LAND and I have got ac but the complexity of My Solution is O(N^2).
I would like to know what would be the more efficient way of solving this problem. Constraints in this problem were fairly small, my solution would have not worked otherwise.