Lesson 3 of 12

Change a sequence without losing it

Update, copy, compare, reverse, and inspect neighbours using the correct bounds.

Change one cell or every matching cell

After validating its index, an array element behaves like a variable: a[index] = value changes one cell. A conditional scan can replace every negative value with zero or replace all occurrences of one target.

Replacement leaves the active length unchanged. Removing an element is a different operation because later elements must move and n must decrease. Avoid treating replacement with zero as deletion.

Copy and compare actual elements

Raw arrays do not support whole-array assignment with b = a. Copy corresponding cells with a loop. Later changes to either array do not affect the other copy.

Sequence equality requires equal active lengths and equal values at every corresponding index. Comparing raw array names is not a content comparison. Once a mismatch makes a same flag false, do not set it true again when a later pair matches.

Look at a neighbour only when it exists

To compare each element with its predecessor, start at i = 1. Accessing a[i − 1] at i = 0 would use index −1. To compare a[i] with a[i + 1], require i + 1 < n.

A nondecreasing sequence allows equal neighbours; a strictly increasing sequence does not. Checking existing order does not sort the array. Empty and one-element sequences contain no violating adjacent pair.

Reverse each mirrored pair once

The mirror of index left is n − 1 − left. Save one value before overwriting its cell, then exchange the pair. Assigning left from right followed by right from the new left would lose the original left value.

Stop after n / 2 swaps. Swapping all n pairs would undo the reversal. For odd n, the middle cell stays in place. This scan uses linear work and only one temporary value beyond the existing array.

C++23
#include <iostream>

int main() {
    int a[1000]{};
    int n = 0;
    std::cin >> n; // 0 <= n <= 1000.
    for (int i = 0; i < n; ++i) { std::cin >> a[i]; }
    for (int left = 0; left < n / 2; ++left) {
        int right = n - 1 - left;
        int temporary = a[left];
        a[left] = a[right];
        a[right] = temporary;
    }
    for (int i = 0; i < n; ++i) {
        if (i > 0) { std::cout << ' '; }
        std::cout << a[i];
    }
    std::cout << '\n';
    return 0;
}
Pause & predict

Why must an in-place reversal stop after n / 2 swaps?

Choose an answer
Try it yourself

Turn the idea into code

Count strict rises: elements larger than their immediate predecessor. For [2, 2, 5, 1, 4], print 2. Allow 0 ≤ n ≤ 1000.

Give me a hint

The first element has no predecessor. Start the scan at index 1.

Show the explained solution

Only 5 > 2 and 4 > 1 are rises. Equal neighbours do not count. Zero- and one-element lists naturally return zero.

C++23
#include <iostream>

int main() {
    int a[1000]{};
    int n = 0;
    std::cin >> n;
    for (int i = 0; i < n; ++i) { std::cin >> a[i]; }
    int rises = 0;
    for (int i = 1; i < n; ++i) {
        if (a[i] > a[i - 1]) { ++rises; }
    }
    std::cout << rises << '\n';
    return 0;
}

Your notes

Ready for the next idea?

Mark complete when you can explain it yourself.

From a problem to a programIdentify the input, output, and constraints, then translate a small plan into C++.Variables, types, and remembered valuesChoose a representation, initialize it, and understand what assignment really changes.Arithmetic without surprisesPredict division and remainder, widen before multiplication, and format decimal answers.Conditions and logical decisionsBuild exclusive categories, combine comparisons, and protect operations with short-circuit logic.Optional: other ways to chooseRecognize switch for exact choices and the conditional operator for choosing a value.For loops: make repetition preciseTrace initialization, condition, body, and update—and avoid an extra iteration.While, do-while, and stopping rulesChoose a loop from its stopping condition and read the exact input format.Break, continue, and early answersDistinguish leaving a loop from skipping one iteration, without losing required input.Five patterns worth understandingUse sums, counts, best-so-far candidates, products, and flags to process a stream.Nested loops: rows and columnsTranslate a shape into two loop roles and count the total work.Take a number apart, one digit at a timeUse remainder and division to process decimal digits, including zero.Multiple cases and a debugging mindsetCombine the patterns, isolate each case, and test the boundaries that can break your idea.Arrays: give every value a placeUnderstand indices, initialization, capacity, active length, and the first safe array loop.Scan, count, search, and compareDistinguish values from positions and preserve the exact tie rule a problem asks for.Change a sequence without losing itUpdate, copy, compare, reverse, and inspect neighbours using the correct bounds.Matrices: think in two coordinatesTraverse tables, total rows and columns, inspect diagonals, and protect grid neighbours.Characters become stringsSeparate characters from numbers, then safely inspect and edit managed text.Read the text the problem actually givesChoose words or full lines, preserve empty cases, and reset only the state that needs it.Scan text: count, filter, transformBuild a result from matching characters without losing leading zeros or skipping shifted text.Join, compare, and find textLearn content comparison, lexicographical order, substring counts, and the not-found sentinel.