olved it using a O(n) algorithm