Write the following program using a greedy algorithm using C/C++ (or both):
Question 2:
Suppose you were to drive from A to B, which is D miles away, along a straight road. Your gas tank, when full, holds enough gas to travel m miles, and you have a map that gives distances between gas stations along the route. Let d1 < d2 < ... < dn be the locations of all the gas stations along the route where di is the distance from St. Louis to the gas station. You can assume that the distance between neighboring gas stations is at most m miles. Your goal is to make as few gas stops as possible along the way. Give the most efficient algorithm you can to determine at which gas stations you should stop.
Write a code to solve this problem using a greedy algorithm. Keep the time complexity of your code O(n).
Sample input: D m n d1 d2 ... dn
20 10 8 2 4 5 8 12 14 16 19
Sample output:
Stop at gas station 4 (8 miles)
Stop at gas station 7 (16 miles)
25 10 5 2 4 5 8 12