table of contents

greedy algorithm

2023-01-18

the method of greediness proposes that in each step in an algorithm, where we are presented with multiple choices, the choice with the biggest instantaneous gain is picked using this method we can write algorithm that, while relatively simple, arent usually optimal

input: an array A of positive numbers output: a subarray (non-consecutive) whose sum is maximal which doesnt contain two consecutive elements

the algorithm picks the biggest number in the input array, adds it to the total sum, and discards the two neighboring elements

but is this algorithm correct?