Module 2 · vector: The Container You Reach for First
Problems: vector
In this lesson
- Read any input into a vector in the shape every starter uses, and print it with single spaces and
'\n'. - Choose
intorlong longfor every value, sum and count from the statement's constraints. - Count the work of your first idea at the largest input, and replace an erase or a sum inside a loop with one pass.
Ten problems, graded against hidden tests. Each one practises an idea from lessons 01 to 03 or the CP pack of this module. Problems 1, 2 and 5 are free. The other seven open with Learn Pro or with the track.
Bob reads the sample, writes a loop and submits. Zara first runs n = 1, a list of equal values and the case where nothing is left. In this set, Zara's habit earns the marks.
Every starter has the same shape
Each starter does four things in order. It turns on fast input and output, reads the input into a vector, leaves a gap for your code, and returns 0. The two fast lines are ios::sync_with_stdio(false); and cin.tie(nullptr);, from Module 1. The largest tests in this set are about 1 MB of text, so reading speed matters.
The vector is named for the story: marks, scores, steps. When the count comes first, the starter makes vector<int> scores(n) and reads into scores[i]. When no count is given, it starts empty and calls push_back for every value, until cin >> x fails at the end of the input.
The judge never looks inside your vector. It runs your whole program on a hidden input and compares what it prints, line by line. Spaces at the end of a line are ignored. Words such as empty and none are printed exactly as written, in lower case. End every line with '\n'.
The constraints choose int or long long
An int holds up to 2147483647, a little over 2 x 109. Every single value in this set fits, because none is bigger than 109. Sums and counts are another matter. Multiply the largest value by the largest count before you choose a type.
| Problem | What can grow | Largest size | Type |
|---|---|---|---|
frequency-table | one mark's count | 200000 | int |
prefix-range-sums | a total of up to 200000 days | 2 x 1014 | long long |
grid-row-col-sums | a row or column of 500 values | 5 x 1011 | long long |
pair-sum-count | t minus one card | 3 x 109 | long long |
pair-sum-count | the number of pairs | 19999900000 | long long |
The fourth row surprises people. Two cards add up to at most 2000000000, which just fits. But the partner a card needs is t minus that card, and 2 x 109 minus -109 is 3 x 109. Reading t as a long long and adding as long long costs nothing and removes the question.
The first idea, at the largest input
Every problem allows 1 second per test, for the whole program, reading included. Before you submit, count your loop's steps at the largest input. A walk over 200000 values is nothing. A loop that moves the rest of the vector at every step is 200000 times 200000, and that is far too much.
Two vector operations hide such a loop. erase at index i moves every value after i one place left, and insert moves them one place right. One call is cheap enough. A call inside a loop over the vector is not. The same goes for a sum written inside a loop over questions.
The table shows two pairs from this set. Each program made its own data and timed only the work, with chrono::steady_clock. Each time is one run on Compiler Explorer, GCC 12.2, with g++ -O2 -std=c++17, the Playground's flags. Compiler Explorer stops a program after about 20 seconds.
| Problem | Way | Input | Time |
|---|---|---|---|
drop-negatives | erase each negative where it stands | n = 200000, every reading negative | stopped after 20503 ms |
drop-negatives | erase each negative where it stands | n = 200000, about half negative | stopped after 20263 ms |
drop-negatives | erase-remove, one pass | n = 200000, every reading negative | 0.2 ms |
drop-negatives | erase each negative where it stands | n = 20000, every reading negative | 297 ms |
prefix-range-sums | add up each question's days | n = q = 200000, random l and r | 10750 ms |
prefix-range-sums | add up each question's days | n = q = 200000, every question days 1 to n | stopped after 20786 ms |
prefix-range-sums | a prefix vector, filled once | n = q = 200000, random l and r | 1.8 ms |
That is why drop-negatives allows only 20000 readings. A careful erase loop passes there, in 297 ms. At ten times the size it needs a hundred times the work. Every slow version gives correct answers. Each one misses the second by far.
The forms these ten problems need
vector<int> v; empty; grows with push_back
while (cin >> x) { v.push_back(x); } read until the input ends
vector<int> v(n); n zeros; then cin >> v[i]
v.size() v.empty() v[i] count, "is it empty?", index i
v.back() v.pop_back() the last value; remove it (never on empty)
v.insert(v.begin() + i, x); x lands at index i; the rest move right
v.erase(remove_if(v.begin(), v.end(), f), v.end()); drop every value f says yes to
vector<int> count(101, 0); one counter for each value 0 to 100
vector<long long> prefix(n + 1, 0); prefix[k]: the total of the first k values
vector<vector<int>> g(r, vector<int>(c)); a grid; g[i][j] is row i, column j
sort(v.begin(), v.end()); smallest first
v.erase(unique(v.begin(), v.end()), v.end()); one copy of each run of equal neighbours
- Every starter below reads the input into the vector the statement names. Keep those lines, and write your code where the comment says.
remove_if,sortanduniquelive in<algorithm>. Add that include when you use one.- Test the edges before the sample: n = 1, all values equal, all values negative, nothing left, and the largest n.
David counts the visitors to his shop each day, but he never says how many days. Print the number of days, the total, and the counts in their order.
#include <iostream>
#include <vector>
using namespace std;
int main()
{
ios::sync_with_stdio(false);
cin.tie(nullptr);
vector<int> visitors;
int x = 0;
while (cin >> x) {
visitors.push_back(x);
}
// Many days of big counts can pass an int, so the total is long long.
long long total = 0;
for (int v : visitors) {
total += v;
}
cout << visitors.size() << " days, " << total << " visitors\n";
for (size_t i = 0; i < visitors.size(); i++) {
if (i > 0) {
cout << ' ';
}
cout << visitors[i];
}
cout << '\n';
return 0;
}
5 days, 653 visitors
120 95 140 210 88
That output is for the input 120 95 on one line and 140 210 88 on the next. cin >> x skips spaces and line breaks alike, so the split does not matter.
The range-for adds every count without an index. The second loop needs the index, because the space goes before every value except the first. i is a size_t, the type size() returns, so the comparison mixes no signed and unsigned types.
Zara logs one temperature a day. Which days were warmer than the day before? Print their indexes, counting from 0, or none.
#include <iostream>
#include <vector>
using namespace std;
int main()
{
ios::sync_with_stdio(false);
cin.tie(nullptr);
int n = 0;
cin >> n;
vector<int> temps(n);
for (int i = 0; i < n; i++) {
cin >> temps[i];
}
// Day 0 has no day before it, so the walk starts at day 1.
vector<int> warmer;
for (int i = 1; i < n; i++) {
if (temps[i] > temps[i - 1]) {
warmer.push_back(i);
}
}
if (warmer.empty()) {
cout << "none\n";
return 0;
}
// int, like the loop above: one index style in one program.
for (int j = 0; j < (int)warmer.size(); j++) {
if (j > 0) {
cout << ' ';
}
cout << warmer[j];
}
cout << '\n';
return 0;
}
1 4 5
That output is for the input 6 and 30 32 31 31 35 36. Day 1 beats day 0, and days 4 and 5 each beat the day before. Day 3 only ties.
The loop starts at 1, so temps[i - 1] never reads before the vector. It stops at n - 1, the last index, so the last day is checked too. For n = 1 the loop never runs, warmer stays empty, and the program prints none. That is the case Zara runs first.
Kenji serves a queue at his coffee stand. Each customer needs some minutes, and each one waits for everyone in front. What is the total waiting time? Kenji serves the front customer and erases them, the way a real queue moves.
#include <iostream>
#include <vector>
using namespace std;
int main()
{
ios::sync_with_stdio(false);
cin.tie(nullptr);
int n = 0;
cin >> n;
vector<int> minutes(n);
for (int i = 0; i < n; i++) {
cin >> minutes[i];
}
long long clock = 0;
long long total_wait = 0;
while (!minutes.empty()) {
total_wait += clock;
clock += minutes.front();
minutes.erase(minutes.begin());
}
cout << total_wait << '\n';
return 0;
}
24
That output is for the input 5 and 3 1 4 1 5. The five customers wait 0, 3, 4, 8 and 9 minutes, which make 24.
The answer is right, and the erase is the problem. Each erase(minutes.begin()) moves every customer still waiting one place left. That is exactly the first row of the table above: 200000 erases at the front, stopped after 20503 ms. The cure is to leave the vector alone and walk it once: for (int m : minutes) { total_wait += clock; clock += m; }. Nothing needs erasing, because nobody reads the served customers again.
Where this is used
- The Unix
uniqcommand. It keeps one copy of each run of equal neighbouring lines, sosort | uniqisunique-sortedfor lines of text.uniq -calso prints each run's length, the job offrequency-table. - PostgreSQL window functions.
SUM(amount) OVER (ORDER BY day)returns a running total for every row. That column is a prefix sum, the vectorprefix-range-sumsfills once. - LeetCode problem 167, "Two Sum II". It gives a sorted array and asks for two values that add up to a target. The intended answer is the two-pointer walk of
pair-sum-count, without the counting. - Microsoft Excel's AutoSum. Select a table with an empty row below it and an empty column to its right, then press AutoSum. Excel fills in every column total and every row total, which is
grid-row-col-sumsin one click.
Common mistakes
1. Moving on after an erase.
for (size_t i = 0; i < steps.size(); i++) {
if (steps[i] < 0) {
steps.erase(steps.begin() + i);
}
}
No message at any command line. After an erase, the next reading slides into index i, and i++ jumps over it. For the sample 3 -1 4 -1 -5 9 2, GCC 12 on Compiler Explorer printed 3 4 -5 9 2: the -5 followed another negative and was never checked. Move on only when you keep the value, or use erase-remove.
2. A backward walk with a size_t index.
for (size_t i = marks.size() - 1; i >= 0; i--) {
The Playground's command line (-O2 -std=c++17, no -Wall) says nothing. With -Wall -Wextra, GCC 12 warns: warning: comparison of unsigned expression in '>= 0' is always true [-Wtype-limits]. A size_t below 0 wraps round to 18446744073709551615, so the loop never ends. On Compiler Explorer the line began 48 91 62 85 70 0 49 0 0 and ran on through memory before the vector, until the run was killed. Walk backwards with an int: for (int i = (int)marks.size() - 1; i >= 0; i--).
3. pop_back on an empty vector.
} else if (op == "pop") {
strokes.pop_back();
}
No message at any command line, because the vector is only empty at run time. It is undefined behaviour. On the Playground, pop_back() on an empty vector ran to Success, and size() then printed 18446744073709551615. In the push-pop-print sample, one run on Compiler Explorer printed 5 8, then 5 8 0 0 0 0 98497 and thousands of zeros, until it was killed. Ask strokes.empty() first.
4. A vector<int> where the totals need long long.
vector<int> prefix(n + 1, 0);
No message at any command line, and the sample passes, because its totals are small. For three days of 1000000000 and the question 1 3, one run on Compiler Explorer printed -1294967296. That is the true total minus 232: signed overflow, which is undefined behaviour. The values fit an int, but the totals reach 2 x 1014.
Amara typed in the marks from a pile of exam papers, in the order they came in. She never counted them. Print the marks from the last paper to the first. The starter reads them into vector<int> marks with push_back.
Input. One or more integers, separated by spaces or line breaks. No count comes first: read until the input ends.
Output. One line with the marks in reverse order, separated by single spaces.
Constraints. 1 to 200000 marks, each between -1000000000 and 1000000000.
Sample. Input 70 85 62 91 48 gives 48 91 62 85 70.
#include <iostream>
#include <vector>
using namespace std;
int main()
{
ios::sync_with_stdio(false);
cin.tie(nullptr);
vector<int> marks;
int x = 0;
while (cin >> x) {
marks.push_back(x);
}
// Print the marks from the last one back to the first,
// on one line, separated by single spaces.
return 0;
}
Run in Compiler
Hint 1
The marks sit at indexes 0 to marks.size() - 1. Which index holds the last mark read?
Hint 2
Walk an int index down from (int)marks.size() - 1 to 0, both included. Print a space between two marks, never after the last one.
Solution
The loop is for (int i = (int)marks.size() - 1; i >= 0; i--). The last mark comes out first and marks[0] comes out last. The index is an int so it can reach -1, and the loop stops after index 0. A space goes after every mark except the one at index 0. For a single mark the loop runs once.
A size_t index never goes below 0, so i >= 0 is always true, as mistake 2 shows. Starting at marks.size() reads one box past the end. The hidden tests include one mark, marks spread over many lines, a last line with no line break, and 200000 marks. while (cin >> x) handles them all.
Kenji wrote down his score in each of n rounds, and some are negative. Find his best score and the round where he first reached it, counting rounds from 0.
Input. The first line holds n. The second holds the n scores.
Output. One line: the largest score, a space, and the index of its first occurrence.
Constraints. 1 <= n <= 200000, and each score is between -1000000000 and 1000000000.
Sample. Input 6 and 4 9 7 9 2 -3 gives 9 1.
#include <iostream>
#include <vector>
using namespace std;
int main()
{
ios::sync_with_stdio(false);
cin.tie(nullptr);
int n = 0;
cin >> n;
vector<int> scores(n);
for (int i = 0; i < n; i++) {
cin >> scores[i];
}
// Print the largest score and the index of its first occurrence,
// counting from 0, separated by a space.
return 0;
}
Run in Compiler
Hint 1
Every score can be negative. Work out -5 -2 -9 by hand. What must the best score start at?
Hint 2
Start the best at scores[0] and its index at 0, then walk from index 1. Replace both only when a score is strictly greater.
Solution
The best starts at scores[0], a real score, so no guess about the smallest possible score is needed. A later score replaces it only with >, so a second copy of the best never moves the index. For n = 1 the loop never runs, and the answer is scores[0] and 0.
A best that starts at 0 prints 0 0 when every score is negative. >= moves to the last copy and prints 9 3 for the sample. The hidden tests put the best first, last and three times over, and include all equal scores and all -1000000000.
Bob's step counter logs a negative number when its sensor fails. Remove every negative reading from vector<int> steps and print the rest in order. A reading of 0 is real and stays.
Input. The first line holds n. The second holds the n readings.
Output. One line with the readings that are 0 or more, separated by single spaces, or the word empty if none is left.
Constraints. 1 <= n <= 20000, and each reading is between -1000000000 and 1000000000.
Sample. Input 7 and 3 -1 4 -1 -5 9 2 gives 3 4 9 2.
#include <iostream>
#include <vector>
using namespace std;
int main()
{
ios::sync_with_stdio(false);
cin.tie(nullptr);
int n = 0;
cin >> n;
vector<int> steps(n);
for (int i = 0; i < n; i++) {
cin >> steps[i];
}
// Remove every negative reading from steps, keeping the order of the rest.
// Then print what is left on one line, or the word empty if nothing is.
return 0;
}
Run in Compiler
Hint 1
Erase one value on paper and look at the index it left. Which reading sits there now?
Hint 2
Either move the index on only when you keep a reading, or use Amara's erase-remove. Write bool is_broken(int reading), which returns reading < 0, and pass it to remove_if. Print empty when steps.empty().
Alice's reading list holds n book numbers. She adds book x so that it becomes the k-th book, counting from 1. So k = 1 puts it first, and k = n + 1 puts it last.
Input. n, then the n book numbers, then k and x.
Output. One line with the n + 1 book numbers after the insert, separated by single spaces.
Constraints. 1 <= n <= 200000, 1 <= k <= n + 1, and every number is between -1000000000 and 1000000000.
Sample. Input 5, 10 20 30 40 50 and 3 25 gives 10 20 25 30 40 50.
#include <iostream>
#include <vector>
using namespace std;
int main()
{
ios::sync_with_stdio(false);
cin.tie(nullptr);
int n = 0;
cin >> n;
vector<int> books(n);
for (int i = 0; i < n; i++) {
cin >> books[i];
}
int k = 0;
int x = 0;
cin >> k >> x;
// Insert x so that it becomes the k-th book, counting from 1.
// Then print all n + 1 books on one line, separated by single spaces.
return 0;
}
Run in Compiler
Hint 1
Positions count from 1 and indexes from 0. After the insert, which index must x have?
Hint 2
books.insert(books.begin() + (k - 1), x); puts x in front of index k - 1. Check k = 1 and k = n + 1 on paper before you submit.
Maria marked n papers, each from 0 to 100. For every mark that appears, print how many students got it.
Input. The first line holds n. The second holds the n marks.
Output. One line for every mark that appears, from the smallest to the largest: the mark, a space, its count.
Constraints. 1 <= n <= 200000, and each mark is between 0 and 100.
Sample. Input 8 and 3 7 3 0 100 7 3 5 gives five lines: 0 1, 3 3, 5 1, 7 2 and 100 1.
#include <iostream>
#include <vector>
using namespace std;
int main()
{
ios::sync_with_stdio(false);
cin.tie(nullptr);
int n = 0;
cin >> n;
vector<int> marks(n);
for (int i = 0; i < n; i++) {
cin >> marks[i];
}
// For every mark that appears, from the smallest to the largest,
// print one line: the mark, a space, and how many students got it.
return 0;
}
Run in Compiler
Hint 1
Marks run from 0 to 100. How many different marks is that, both ends included?
Hint 2
Make vector<int> count(101, 0) and add 1 to count[m] for every mark m. Then walk m from 0 to 100 and print the counts above 0.
Solution
The mark itself is the index, so count[m] counts mark m. 101 boxes cover 0 to 100, both included. Walking the boxes upward prints the marks in increasing order, with no sort. No count can pass 200000, so int is enough.
count(100) has no box for 100, so count[100]++ writes past the end. That is undefined behaviour, and GCC 12 says nothing, because the index is only known at run time. Printing every box prints the zeros too. The hidden tests hold only 0 and 100, every mark once, and 200000 copies of one mark.
David has the sales of n days, and a day can be negative after refunds. He asks q questions: what were the total sales from day l to day r?
Input. n and q, then the n daily sales, then q lines, each with l and r.
Output. One line per question with the total of days l to r, both included.
Constraints. 1 <= n, q <= 200000, each day's sales are between -1000000000 and 1000000000, and 1 <= l <= r <= n, with days numbered from 1.
Sample. Input 5 3, 4 -2 7 1 3, 1 3, 2 5 and 4 4 gives 9, 9 and 1 on three lines.
#include <iostream>
#include <vector>
using namespace std;
int main()
{
ios::sync_with_stdio(false);
cin.tie(nullptr);
int n = 0;
int q = 0;
cin >> n >> q;
vector<int> sales(n);
for (int i = 0; i < n; i++) {
cin >> sales[i];
}
// Anything you want to prepare before the questions goes here.
for (int k = 0; k < q; k++) {
int l = 0;
int r = 0;
cin >> l >> r;
// Print the total sales of days l to r, both included.
// Days are numbered from 1.
}
return 0;
}
Run in Compiler
Hint 1
Count the additions when n = q = 200000 and every question asks for all the days. Then look at the table above.
Hint 2
Before the questions, fill vector<long long> prefix(n + 1, 0) with prefix[i] = prefix[i - 1] + sales[i - 1]. Days l to r are the first r days without the first l - 1.
Kenji's tournament table is a grid: a row per player, a column per round, and a cell holds that round's points, negative for a penalty. Print every row's total and every column's total.
Input. r and c, then r lines of c integers.
Output. Line 1 holds the r row sums, and line 2 the c column sums, each separated by single spaces.
Constraints. 1 <= r, c <= 500, and each integer is between -1000000000 and 1000000000.
Sample. Input 2 3, 1 2 3 and 4 5 6 gives 6 15 and 5 7 9 on two lines.
#include <iostream>
#include <vector>
using namespace std;
int main()
{
ios::sync_with_stdio(false);
cin.tie(nullptr);
int r = 0;
int c = 0;
cin >> r >> c;
vector<vector<int>> points(r, vector<int>(c));
for (int i = 0; i < r; i++) {
for (int j = 0; j < c; j++) {
cin >> points[i][j];
}
}
// Line 1: the sum of each row, from the first row to the last.
// Line 2: the sum of each column, from the first column to the last.
return 0;
}
Run in Compiler
Hint 1
Work out the largest possible sum of one row. Does it fit an int?
Hint 2
Make vector<long long> row_sum(r, 0) and col_sum(c, 0). One walk over the grid adds points[i][j] to row_sum[i] and to col_sum[j].
Amara tests the undo list of a drawing app with a script. push x adds stroke x at the end. pop removes the last stroke, or does nothing on an empty list. print prints the list.
Input. n, then n lines, each one operation.
Output. For every print, one line: the strokes separated by single spaces, or the word empty.
Constraints. 1 <= n <= 200000, 1 <= x <= 1000, and all the prints together print at most 100000 strokes.
Sample. Input 7, push 5, push 8, print, pop, pop, pop and print gives 5 8 and empty on two lines.
#include <iostream>
#include <string>
#include <vector>
using namespace std;
int main()
{
ios::sync_with_stdio(false);
cin.tie(nullptr);
int n = 0;
cin >> n;
vector<int> strokes;
for (int i = 0; i < n; i++) {
string op;
cin >> op;
if (op == "push") {
int x = 0;
cin >> x;
// Add x at the end of strokes.
} else if (op == "pop") {
// Remove the last stroke, or do nothing if there is none.
} else {
// op is "print": print the strokes on one line,
// or the word empty if there are none.
}
}
return 0;
}
Run in Compiler
Hint 1
The sample pops three times after two pushes. What must the third pop do, and what does pop_back() promise on an empty vector?
Hint 2
push is push_back(x). pop calls pop_back() only when !strokes.empty(). print prints empty or the strokes with spaces between them.
Zara sorted her thermometer's n readings, and many repeat. Print how many different readings there are, and the readings themselves, each once.
Input. n, then n integers in non-decreasing order.
Output. Line 1 holds k, the number of distinct readings. Line 2 holds them in increasing order, separated by single spaces.
Constraints. 1 <= n <= 200000, and each reading is between -1000000000 and 1000000000.
Sample. Input 8 and 1 1 2 3 3 3 7 7 gives 4 and 1 2 3 7 on two lines.
#include <iostream>
#include <vector>
using namespace std;
int main()
{
ios::sync_with_stdio(false);
cin.tie(nullptr);
int n = 0;
cin >> n;
vector<int> readings(n);
for (int i = 0; i < n; i++) {
cin >> readings[i];
}
// The readings are in non-decreasing order.
// Line 1: how many distinct readings there are.
// Line 2: the distinct readings in order, separated by single spaces.
return 0;
}
Run in Compiler
Hint 1
The input is sorted. Where does every copy of a value sit, compared with the other copies?
Hint 2
A reading is new when it is the first one or differs from the one before it. Or let the library do it: readings.erase(unique(readings.begin(), readings.end()), readings.end());, with <algorithm> included.
Maria deals n numbered cards in a row, and some numbers are negative. Two cards win when their numbers add up to exactly t. Count the winning pairs of positions.
Input. n and t, then the n card numbers, in any order.
Output. One line with the number of pairs of positions i < j whose numbers add up to t.
Constraints. 1 <= n <= 200000, each number is between -1000000000 and 1000000000, and -2000000000 <= t <= 2000000000.
Sample. Input 6 10 and 3 7 5 5 2 8 gives 3.
#include <iostream>
#include <vector>
using namespace std;
int main()
{
ios::sync_with_stdio(false);
cin.tie(nullptr);
int n = 0;
long long t = 0;
cin >> n >> t;
vector<int> cards(n);
for (int i = 0; i < n; i++) {
cin >> cards[i];
}
// Print how many pairs of positions i < j have
// cards[i] + cards[j] equal to t.
return 0;
}
Run in Compiler
Hint 1
Checking every pair is about 2 x 1010 checks at n = 200000. Sort the cards first. What does the smallest card plus the largest card tell you?
Hint 2
Put lo at 0 and hi at n - 1. A sum below t moves lo up, and a sum above t moves hi down. On a match, count the run of equal cards at each end, add their product, and step past both runs.
Common doubts
The judge sees only the output. Must I use the vector the starter names?
The judge cannot tell, so no rule forces you.
largest-and-positioncould even be solved while reading. But this set trains vectors, and the hints and solutions talk about the starter's vector. A stored vector also lets you walk the values twice.Why
'\n'and notendl?endlwrites a line break and then flushescout, which sends its buffer out at once. A program that prints 200000 lines would flush 200000 times.'\n'only writes the line break, and the buffer goes out when it is full or the program ends.My program passes the sample. Why does a hidden test fail?
The sample is one small case, chosen to explain the statement. The hidden tests add n = 1, all equal values, all negative values, the empty answer and the largest n. Zara runs those before she submits, and so should you.
Should my loop index be an int or a size_t?
size_tmatchessize(), so a forward loop compares like with like. A backward loop needs anint, because asize_tcan never go below 0. Pick one per program and castsize()once if you need to.
Key takeaways
- Every starter reads into a named vector after the two fast lines; you print with single spaces and
'\n'. - Values up to 109 fit an
int; sums, pair sums and pair counts needlong long. - Count the steps at the largest input: an
eraseor a sum inside a loop over 200000 values misses the second. - Erase-remove, a prefix vector, a count vector and unique-erase each turn a loop inside a loop into one pass.
- Test n = 1, all equal, all negative and the empty answer before the sample.
- Go deeper: CP and Interview Pack, vector patterns and the bug gallery (Pro).
Next comes the cheat sheet, the whole of vector on one page, and then the module test.
End of lesson 7
Get every problem accepted, and the lesson is done.
0 of 3 free problems accepted
Next: Cheat Sheet: vector on One Page