Embercode
✕
ArraysQuick quiz

Rotation and differences

7 questions. Covers Difference arrays.

Questions 1 to 5 of 7

  1. 1. Predict the output of this program. ``cpp #include <algorithm> #include <iostream> #include <vector> using namespace std; void rotateRight(vector<int>& a, int k) { int n = static_cast<int>(a.size()); if (n == 0) { return; } k %= n; reverse(a.begin(), a.end()); reverse(a.begin(), a.begin() + k); reverse(a.begin() + k, a.end()); } int main() { vector<int> a = {2, 4, 6, 8, 10, 12}; rotateRight(a, 8); for (int x : a) { cout << x << " "; } cout << "\n"; } `` What does it print?
  2. 2. Predict the output of this program. It applies the updates to a difference array and then runs the total. ``cpp #include <iostream> #include <vector> using namespace std; int main() { vector<int> diff(6, 0); diff[0] += 3; diff[3] -= 3; diff[2] += 1; diff[5] -= 1; int run = 0; for (int i = 0; i < 5; ++i) { run += diff[i]; cout << run << " "; } cout << "\n"; } `` What does it print?
  3. 3. Reverse the whole array 1 2 3 4 5 with reverse(a.begin(), a.end()). Write the five values in the order they read afterwards, separated by spaces.

    Type your answer.

  4. 4. To add v to every index from l to r in a difference array, the second write is diff[____] -= v. Write the index as an expression in r.

    Type your answer.

  5. 5. What does the line k %= n do at the start of rotateRight? ``cpp int n = static_cast<int>(a.size()); if (n == 0) { return; } k %= n; ``

5 question(s) left on this page.

Typed answers ignore extra spaces and, for most questions, capital letters.