Most of this kind of question can be deal in O(n)