At the supermarket you only look at what you can afford. Write cheap_ones(prices, budget) to put every price no more than budget into a new vector, in the same order, and return it. None at all gives an empty one.
cheap_ones({3, 12, 5, 20}, 10)→[3, 5]cheap_ones({8, 9}, 5)→[]vector<int> cheap_ones(const vector<int> &prices, int budget)#include <vector>
using namespace std;
vector<int> cheap_ones(const vector<int> &prices, int budget) {
vector<int> picked;
for (int p : prices) {
// your code here: if p is no more than budget, put it in picked
}
return picked;
}
Run tries the examples. When they look right, Submit.