Center-Out Vector Traversal in C++

Introduction

Greetings! Welcome to our lesson today where we'll unravel a fascinating aspect of vector manipulation. Here's the question: How would you traverse a vector not from the beginning to the end, or vice versa, but from the center outward in either direction? Today's lesson is all about exploring this concept with C++. Brace yourself for a captivating learning journey.

Task Statement

Our task is to produce a new vector, given a vector of integers, that starts from the center of the original vector and alternates direction towards both ends. That is, the first element of our new vector will be the middle element of the original one. After defining the starting point, we will alternate between elements to the left and to the right of this center until all elements have been included. If the length of the initial array is even, we first take the the element to the left of the center, then the one to the right of the center, then do the alternation as described above.

For example, for numbers = {1, 2, 3, 4, 5}, the output would be {3, 2, 4, 1, 5}.

We break down this seemingly complex task into manageable pieces to progressively build our C++ function. Keep in mind an additional condition: the length of the vector — represented as n — can range from 1 to 100,000, inclusive.

Solution Building: Step 1

First, let's establish the midpoint of our vector. Our task requires us to expand our vector from the center to the ends, so we divide its length by 2 using integer division in C++. If we find that the vector's length is odd, we include the middle element in the newOrder vector, given it has no counterpart. If the vector's length is even, newOrder initially remains empty.

Here is how it looks in C++:

std::vector<int> iterateMiddleToEnd(std::vector<int> &numbers) {
    int mid = numbers.size() / 2;  // index of the left middle element
    std::vector<int> newOrder;  // vector to store new order

    if(numbers.size() % 2 == 1) {
        newOrder.push_back(numbers[mid]);  // Adding middle element to resulting vector if length is odd
    }
    // newOrder remains empty for now if length is even

Solution Building: Step 2

Successfully solving our task requires two pointers: left and right. These pointers are initialized to point to the elements immediately to the left and right of the middle element, respectively.

Here is the C++ function with the added initialization of these pointers:

std::vector<int> iterateMiddleToEnd(std::vector<int>& numbers) {
    int mid = numbers.size() / 2; // index of the left middle element
    int left, right;
    std::vector<int> newOrder;  // vector to store new order

    if (numbers.size() % 2 == 1) {
        left = mid - 1; // Pointing to the left to the middle element
        right = mid + 1; // Pointing to the right of the middle element
        newOrder.push_back(numbers[mid]); // Adding the middle element to the resulting vector
    }
    else {
        left = mid - 1; // Pointing to the left middle element
        right = mid; // Pointing to the right middle element
    }
Sign up

Join the 1M+ learners on CodeSignal

Be a part of our community of 1M+ users who develop and demonstrate their skills on CodeSignal