xyz Interview Question
Software AnalystsCountry: United States
Interview Type: Written Test
#include <iostream>
#include <vector>
#include <limits>
using namespace std;
vector<pair<int, int>> findMaxMinimumPositions(vector<vector<int>>& grid) {
int n = grid.size();
int maxMinimum = numeric_limits<int>::min();
vector<pair<int, int>> positions;
// Iterate over each block in the grid
for (int i = 0; i < n; i++) {
for (int j = 0; j < n; j++) {
int minimum = numeric_limits<int>::max();
// Calculate the minimum amount considering all neighboring blocks
for (int dx = -1; dx <= 1; dx++) {
for (int dy = -1; dy <= 1; dy++) {
int ni = i + dx;
int nj = j + dy;
if (ni >= 0 && ni < n && nj >= 0 && nj < n) {
minimum = min(minimum, grid[ni][nj]);
}
}
}
// Update the maximum minimum amount and positions
if (minimum > maxMinimum) {
maxMinimum = minimum;
positions.clear();
positions.push_back({i + 1, j + 1});
} else if (minimum == maxMinimum) {
positions.push_back({i + 1, j + 1});
}
}
}
return positions;
}
int main() {
int N;
cin >> N;
vector<vector<int>> grid(N, vector<int>(N));
// Read the grid description
for (int i = 0; i < N; i++) {
for (int j = 0; j < N; j++) {
char delimiter;
cin >> grid[i][j];
if (j < N - 1) {
cin >> delimiter;
}
}
}
// Find the positions with the maximum minimum amount
vector<pair<int, int>> positions = findMaxMinimumPositions(grid);
// Print the positions
for (const auto& pos : positions) {
cout << pos.first << "#" << pos.second << endl;
}
return 0;
}
#include <iostream>
- Vijith Pramod G July 05, 2023#include <vector>
#include <limits>
using namespace std;
vector<pair<int, int>> findMaxMinimumPositions(vector<vector<int>>& grid) {
int n = grid.size();
int maxMinimum = numeric_limits<int>::min();
vector<pair<int, int>> positions;
// Iterate over each block in the grid
for (int i = 0; i < n; i++) {
for (int j = 0; j < n; j++) {
int minimum = numeric_limits<int>::max();
// Calculate the minimum amount considering all neighboring blocks
for (int dx = -1; dx <= 1; dx++) {
for (int dy = -1; dy <= 1; dy++) {
int ni = i + dx;
int nj = j + dy;
if (ni >= 0 && ni < n && nj >= 0 && nj < n) {
minimum = min(minimum, grid[ni][nj]);
}
}
}
// Update the maximum minimum amount and positions
if (minimum > maxMinimum) {
maxMinimum = minimum;
positions.clear();
positions.push_back({i + 1, j + 1});
} else if (minimum == maxMinimum) {
positions.push_back({i + 1, j + 1});
}
}
}
return positions;
}
int main() {
int N;
cin >> N;
vector<vector<int>> grid(N, vector<int>(N));
// Read the grid description
for (int i = 0; i < N; i++) {
for (int j = 0; j < N; j++) {
char delimiter;
cin >> grid[i][j];
if (j < N - 1) {
cin >> delimiter;
}
}
}
// Find the positions with the maximum minimum amount
vector<pair<int, int>> positions = findMaxMinimumPositions(grid);
// Print the positions
for (const auto& pos : positions) {
cout << pos.first << "#" << pos.second << endl;
}
return 0;
}