Skip to main content

Command Palette

Search for a command to run...

Design Neighbor Sum Service

Published
3 min read
Design Neighbor Sum Service
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 a n x n 2D array grid containing distinct elements in the range [0, n<sup>2</sup> - 1].

Implement the neighborSum class:

  • neighborSum(int [][]grid) initializes the object.

  • int adjacentSum(int value) returns the sum of elements which are adjacent neighbors of value, that is either to the top, left, right, or bottom of value in grid.

  • int diagonalSum(int value) returns the sum of elements which are diagonal neighbors of value, that is either to the top-left, top-right, bottom-left, or bottom-right of value in grid.

LeetCode Problem - 3242

class neighborSum {

    private int[][] grid; // 2D grid to store the matrix
    private int n; // Size of the grid (assuming it's a square matrix)

    // Constructor to initialize the grid and its size
    public neighborSum(int[][] grid) {
        this.grid = grid;
        this.n = grid.length; // Set the size of the grid
    }

    // Method to calculate the sum of adjacent cells of a given value
    public int adjacentSum(int value) {
        // Find the position (row, col) of the given value in the grid
        int[] position = findPosition(value);
        int row = position[0];
        int col = position[1];

        int sum = 0; // Initialize the sum of adjacent cells
        // Define directions for adjacent cells (up, down, left, right)
        int[][] directions = {{-1, 0}, {1, 0}, {0, -1}, {0, 1}};

        // Iterate through each direction
        for (int[] dir : directions) {
            int newRow = row + dir[0]; // Calculate the new row index
            int newCol = col + dir[1]; // Calculate the new column index
            // Check if the new position is within bounds
            if (isValid(newRow, newCol)) {
                sum += grid[newRow][newCol]; // Add the value of the adjacent cell to the sum
            }
        }

        return sum; // Return the sum of adjacent cells
    }

    // Method to calculate the sum of diagonal cells of a given value
    public int diagonalSum(int value) {
        // Find the position (row, col) of the given value in the grid
        int[] position = findPosition(value);
        int row = position[0];
        int col = position[1];

        int sum = 0; // Initialize the sum of diagonal cells
        // Define directions for diagonal cells (top-left, top-right, bottom-left, bottom-right)
        int[][] directions = {{-1, -1}, {-1, 1}, {1, -1}, {1, 1}};

        // Iterate through each direction
        for (int[] dir : directions) {
            int newRow = row + dir[0]; // Calculate the new row index
            int newCol = col + dir[1]; // Calculate the new column index
            // Check if the new position is within bounds
            if (isValid(newRow, newCol)) {
                sum += grid[newRow][newCol]; // Add the value of the diagonal cell to the sum
            }
        }

        return sum; // Return the sum of diagonal cells
    }

    // Helper method to find the position (row, col) of a given value in the grid
    private int[] findPosition(int value) {
        for (int i = 0; i < n; i++) {
            for (int j = 0; j < n; j++) {
                // Return the position if the value is found
                if (grid[i][j] == value) {
                    return new int[]{i, j};
                }
            }
        }
        return null; // Return null if the value is not found
    }

    // Helper method to check if the given position is within bounds of the grid
    private boolean isValid(int row, int col) {
        return row >= 0 && row < n && col >= 0 && col < n;
    }
}

/**
 * Your neighborSum object will be instantiated and called as such:
 * neighborSum obj = new neighborSum(grid);
 * int param_1 = obj.adjacentSum(value);
 * int param_2 = obj.diagonalSum(value);
 */

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.