O(n) solution for flags