Optimize to O(k)