Unique Matrix Traversal and Perfect Square Detection in C++

Introduction

Hello, budding programmer! Are you ready to embark on a journey into the land of matrices? Today, we're in for a thrilling ride into the world of unique matrix traversal. We'll be navigating through the realm of 2D matrices following a distinctive order that oozes intrigue. Stay seated, and let's dive right in!

Task Statement

Suppose we have a matrix where each cell represents a distinct symbol or integer. Our task is to decode this matrix by reading the cells in a particular order.

The decoding begins from the top-left cell of the matrix. We move in a bottom-left downward diagonal direction until we hit the left boundary. Upon hitting the left boundary, we move one cell down (unless we're at the bottom-left corner already, in which case we move one cell to the right) and start moving in an upward diagonal direction towards the upper-right.

While moving diagonally up-right, if we hit the top boundary, we move one cell to the right and start moving in a downward diagonal direction towards the bottom-left. However, if we hit the right boundary while moving diagonally upwards, we move one cell down and start moving in a bottom-left direction. In other words, we keep zigzagging diagonally across the matrix until every cell in the matrix is visited.

Upon completing this zigzag traversal, we will have a list of traversed cell values. Next, we process this list to uncover the indices of the perfect square numbers. The function diagonal_traverse_and_squares(matrix) should implement this traversal and return a list containing the 0-indexed positions of perfect square numbers in the traversed sequence.

Take a 3x4 matrix for instance:

text
[
    {1, 2, 3, 4},
    {5, 6, 7, 8},
    {9, 10, 11, 12}
]

Upon completing the diagonal traversal, we'll get the list: [1, 5, 2, 3, 6, 9, 10, 7, 4, 8, 11, 12]. From this list, we see that 1, 9, and 4 are perfect squares and are located at the 0th, 5th, and 8th positions in the list. Thus, our function returns: [0, 5, 8].

Solution Building: Step 1

First, let's put on our C++ hats and scrutinize the dimensions of the 2D matrix. To map the landscape of our matrix, we'll use the size() method to determine the number of rows and columns. Next, we initialize two vectors: traversal and results. The traversal vector will be responsible for keeping the cell values that we will obtain from the matrix based on our unique diagonal zigzag traversal. The results vector will be populated later with the positions of perfect square numbers that can be found in the traversal vector.

C++
#include <iostream>
#include <vector>
#include <cmath>

std::vector<int> diagonal_traverse_and_squares(const std::vector<std::vector<int>>& matrix) {
    int rows = matrix.size(), cols = matrix[0].size();
    std::vector<int> traversal;
    std::vector<int> results;
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