Skip to main content

Command Palette

Search for a command to run...

Largest Local Values in a Matrix

Published
2 min read
Largest Local Values in a Matrix
G

As a Systems Engineer at Tata Consultancy Services, I deliver exceptional software products for mobile and web platforms, using agile methodologies and robust quality maintenance. I am experienced in performance testing, automation testing, API testing, and manual testing, with various tools and technologies such as Jmeter, Azure LoadTest, Selenium, Java, OOPS, Maven, TestNG, and Postman.

I have successfully developed and executed detailed test plans, test cases, and scripts for Android and web applications, ensuring high-quality standards and user satisfaction. I have also demonstrated my proficiency in manual REST API testing with Postman, as well as in end-to-end performance and automation testing using Jmeter and selenium with Java, TestNG and Maven. Additionally, I have utilized Azure DevOps for bug tracking and issue management.

You are given an n x n integer matrix grid.

Generate an integer matrix maxLocal of size (n - 2) x (n - 2) such that:

  • maxLocal[i][j] is equal to the largest value of the 3 x 3 matrix in grid centered around row i + 1 and column j + 1.

In other words, we want to find the largest value in every contiguous 3 x 3 matrix in grid.

Return the generated matrix.

LeetCode Problem - 2373

class Solution {

    // Method to find the largest local values in a given grid
    public int[][] largestLocal(int[][] grid) {
        // The length of the new array will be 2 less than the length of the grid
        int outerLoopIteration = grid.length - 2;

        // Initialize a new array to store the largest local values
        int[][] newArray = new int[outerLoopIteration][outerLoopIteration];

        // Iterate over each element of the new array
        for(int i = 0; i < newArray.length; i++){
            for(int j = 0; j < newArray.length; j++){
                // Find the largest element in the 3x3 subgrid and assign it to the new array
                newArray[i][j] = findLargerElement(grid, i, j);
            }
        }
        // Return the new array containing the largest local values
        return newArray;
    }

    // Helper method to find the largest element in a 3x3 subgrid
    public int findLargerElement(int[][] grid, int row, int col){
        // Initialize the maximum value to the smallest possible integer
        int maxValue = Integer.MIN_VALUE;

        // Iterate over each element in the 3x3 subgrid
        for (int i = row; i < row + 3; i++){
            for (int j = col; j < col + 3; j++){
                // Update the maximum value if a larger element is found
                if (grid[i][j] > maxValue){
                    maxValue = grid[i][j];
                }
            }
        }
        // Return the largest element found in the 3x3 subgrid
        return maxValue;
    }
}

More from this blog

S

Software and Performance Testing Insights

462 posts

Results-Driven Agile QA Specialist | Expert in Mobile & Web Testing | Proficient in Test Planning, Execution, and Root Cause Analysis.