Kodewick
Start learning →
Basic←→0 / 60

Within budget

#024BasicvectorLoops4 min

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.

Examples
cheap_ones({3, 12, 5, 20}, 10)→[3, 5]
cheap_ones({8, 9}, 5)→[]
The functionvector<int> cheap_ones(const vector<int> &prices, int budget)
main.cpp
#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;
}
Ctrl + Enter runs too
Result

Run tries the examples. When they look right, Submit.