You've been hired to work on a planet exploration rover's data analysis system. One of your tasks is to process the rover's camera data, represented as a 2D square array of integers matrix, to help map the terrain for navigation.
Your task is to flatten the elements of the given
matrix into a single list by traversing it in a spiral order, starting from the top-left corner and moving clockwise. Once the matrix is flattened, calculate the sum of the elements located at indices divisible by 3
(0-based) in this flattened list and return this sum.
Example
• For
matrix = [
[1, 2, 31,
[4, 5, 6],
17, 8, 9]
]
the output should be solution(matrix) = 14
The matrix is traversed in a spiral order: [1, 2, 3, 6, 9. 8, 7, 4, 5] . The elements at indices divisible by 3 are [1, 6, 7] with sum 14 .
For
matrix = [ ]
the output should be solution(matrix) = 0.
Answer this in c++ and ensure that the end user is able to run the test cases
Assistant
Certainly! Below is a C++ program that solves the problem you described. The program includes the solution function that processes the matrix in spiral order, sums the elements at indices divisible by 3, and returns the sum. I've also included a main function with test cases so you can run and verify the output.
cpp
#include <iostream>
#include <vector>
int solution(const std::vector<std::vector<int>>& matrix) {
std::vector<int> spiral_order;
int n = matrix.size();
if (n == 0) return 0; // Return 0 for empty matrix
int top = 0, bottom = n - 1;
int left = 0, right = n - 1;
while (top <= bottom && left <= right) {
// Traverse from Left to Right
for (int col = left; col <= right; ++col)
spiral_order.push_back(matrix[top][col]);
++top;
// Traverse Downwards
for (int row = top; row <= bottom; ++row)
spiral_order.push_back(matrix[row][right]);
--right;
// Make sure we are now on a different row
if (top <= bottom) {
// Traverse from Right to Left
for (int col = right; col >= left; --col)
spiral_order.push_back(matrix[bottom][col]);
--bottom;
}
// Make sure we are now in a different column
if (left <= right) {
// Traverse Upwards
for (int row = bottom; row >= top; --row)
spiral_order.push_back(matrix[row][left]);
++left;
}
}
// Sum elements at indices divisible by 3
int sum = 0;
for (size_t i = 0; i < spiral_order.size(); ++i) {
if (i % 3 == 0)
sum += spiral_order[i];
}
return sum;
}
int main() {
// Test case 1
std::vector<std::vector<int>> matrix1 = {
{1, 2, 3},
{4, 5, 6},
{7, 8, 9}
};
int result1 = solution(matrix1);
std::cout << "Test Case 1 Output: " << result1 << std::endl; // Expected output: 14
// Test case 2 (Empty matrix)
std::vector<std::vector<int>> matrix2 = {};
int result2 = solution(matrix2);
std::cout << "Test Case 2 Output: " << result2 << std::endl; // Expected output: 0
// Additional test case
std::vector<std::vector<int>> matrix3 = {
{1, 2, 3, 4},
{5, 6, 7, 8},
{9,10,11,12},
{13,14,15,16}
};
int result3 = solution(matrix3);
std::cout << "Test Case 3 Output: " << result3 << std::endl;
return 0;
}
Explanation:
Spiral Traversal:
We use four variables top, bottom, left, and right to keep track of the current edges of the matrix that we need to traverse.
In each iteration of the while loop, we traverse:
From left to right across the top row.
Downwards on the rightmost column.
From right to left across the bottom row (if top <= bottom).
Upwards on the leftmost column (if left <= right).
After traversing each edge, we adjust the variables to move towards the inner layers of the matrix.
Summation:
After flattening the matrix into spiral_order, we iterate over it.
We check if the index is divisible by 3 (i % 3 == 0) and add the element to sum if it is.
How to Run the Program:
Save the code to a file named, for example, spiral_matrix_sum.cpp.
Compile the program using a C++ compiler. If you're using GCC, you can compile it with the following command: