< Summary

Information
Class: LeetCode.Algorithms.MagicSquaresInGrid.MagicSquaresInGridBruteForce
Assembly: LeetCode
File(s): D:\a\LeetCode-CS\LeetCode-CS\source\LeetCode\Algorithms\MagicSquaresInGrid\MagicSquaresInGridBruteForce.cs
Line coverage
100%
Covered lines: 43
Uncovered lines: 0
Coverable lines: 43
Total lines: 83
Line coverage: 100%
Branch coverage
96%
Covered branches: 25
Total branches: 26
Branch coverage: 96.1%
Method coverage

Feature is only available for sponsors

Upgrade to PRO version

Metrics

MethodBranch coverage Crap Score Cyclomatic complexity Line coverage
NumMagicSquaresInside(...)100%66100%
IsMagicSquare(...)95%2020100%

File(s)

D:\a\LeetCode-CS\LeetCode-CS\source\LeetCode\Algorithms\MagicSquaresInGrid\MagicSquaresInGridBruteForce.cs

#LineLine coverage
 1// --------------------------------------------------------------------------------
 2// Copyright (C) 2025 Eugene Eremeev (also known as Yevhenii Yeriemeieiv).
 3// All Rights Reserved.
 4// --------------------------------------------------------------------------------
 5// This software is the confidential and proprietary information of Eugene Eremeev
 6// (also known as Yevhenii Yeriemeieiv) ("Confidential Information"). You shall not
 7// disclose such Confidential Information and shall use it only in accordance with
 8// the terms of the license agreement you entered into with Eugene Eremeev (also
 9// known as Yevhenii Yeriemeieiv).
 10// --------------------------------------------------------------------------------
 11
 12namespace LeetCode.Algorithms.MagicSquaresInGrid;
 13
 14/// <inheritdoc />
 15public class MagicSquaresInGridBruteForce : IMagicSquaresInGrid
 16{
 17    /// <summary>
 18    ///     Time complexity - O(m * n)
 19    ///     Space complexity - O(1)
 20    /// </summary>
 21    /// <param name="grid"></param>
 22    /// <returns></returns>
 23    public int NumMagicSquaresInside(int[][] grid)
 724    {
 725        var result = 0;
 26
 4427        for (var i = 0; i < grid.Length - 2; i++)
 1528        {
 18029            for (var j = 0; j < grid[i].Length - 2; j++)
 7530            {
 7531                if (IsMagicSquare(grid, i, j))
 532                {
 533                    result++;
 534                }
 7535            }
 1536        }
 37
 738        return result;
 739    }
 40
 41    private static bool IsMagicSquare(IReadOnlyList<int[]> grid, int i, int j)
 7542    {
 7543        if (grid[i + 1][j + 1] != 5 || grid[i + 1][j + 1] == grid[i][j + 1] || grid[i + 1][j + 1] == grid[i + 1][j])
 6644        {
 6645            return false;
 46        }
 47
 48        const int sum = 15;
 49
 5050        for (var k = 0; k < 3; k++)
 2051        {
 2052            var rowSum = 0;
 2053            var columnSum = 0;
 54
 15855            for (var l = 0; l < 3; l++)
 6056            {
 6057                if (grid[i + k][j + l] > 9)
 158                {
 159                    return false;
 60                }
 61
 5962                rowSum += grid[i + k][j + l];
 5963                columnSum += grid[i + l][j + k];
 5964            }
 65
 1966            if (rowSum != sum || columnSum != sum)
 367            {
 368                return false;
 69            }
 1670        }
 71
 572        var leftDiagonalSum = 0;
 573        var rightDiagonalSum = 0;
 74
 4075        for (var k = 0; k < 3; k++)
 1576        {
 1577            leftDiagonalSum += grid[i + k][j + k];
 1578            rightDiagonalSum += grid[i + k][2 + j - k];
 1579        }
 80
 581        return leftDiagonalSum == sum && rightDiagonalSum == sum;
 7582    }
 83}