CJCoding With Joseph
15free / daySign inCreate account

Keep Only the Last N

Write a function deque<int> keepLast(const vector<int>& v, int n) that returns a deque holding only the last n elements of v, in their original order. If v has n or fewer elements, all of them are kept. You may assume n >= 1.

The tests print the returned deque separated by spaces. For v = {1, 2, 3, 4, 5} and n = 3 the output is exactly what is shown in Expected Output.

For v = {1, 2} and n = 5 the output is:
1 2

Do NOT write a main function.

Expected Output:

3 4 5
1 2

= newline. The output ends with a newline — print one after the last line.

Hints4 available
Topics:
STL ContainersQueues
1
Tab to indent (Esc then Tab to move on) · Ctrl+Enter to run · Shift+Alt+F to format · Ctrl+Space to expand shortcuts (cout, fori)

Your Output

Run your code to see the output here...

Test Cases

Run your code to see test case results.