implementing Greedy Algorithm