< 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
91%
Covered lines: 43
Uncovered lines: 4
Coverable lines: 47
Total lines: 94
Line coverage: 91.4%
Branch coverage
93%
Covered branches: 28
Total branches: 30
Branch coverage: 93.3%
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(...)91.66%252487.87%

File(s)

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

#LineLine coverage
 1// --------------------------------------------------------------------------------
 2// Copyright (C) 2026 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 sealed class MagicSquaresInGridBruteForce : IMagicSquaresInGrid
 16{
 17    private const int MagicSquareSum = 15;
 18
 19    /// <summary>
 20    ///     Time complexity - O(m * n)
 21    ///     Space complexity - O(1)
 22    /// </summary>
 23    /// <param name="grid"></param>
 24    /// <returns></returns>
 25    public int NumMagicSquaresInside(int[][] grid)
 726    {
 727        var result = 0;
 28
 4429        for (var i = 0; i < grid.Length - 2; i++)
 1530        {
 18031            for (var j = 0; j < grid[i].Length - 2; j++)
 7532            {
 7533                if (!IsMagicSquare(grid, i, j))
 7034                {
 7035                    continue;
 36                }
 37
 538                result++;
 539            }
 1540        }
 41
 742        return result;
 743    }
 44
 45    private static bool IsMagicSquare(int[][] grid, int row, int column)
 7546    {
 7547        if (grid[row + 1][column + 1] != 5 ||
 7548            grid[row + 1][column + 1] == grid[row][column + 1] ||
 7549            grid[row + 1][column + 1] == grid[row + 1][column])
 6650        {
 6651            return false;
 52        }
 53
 954        Span<bool> seen = stackalloc bool[10];
 55
 5056        for (var i = 0; i < 3; i++)
 2057        {
 2058            var rowSum = 0;
 2059            var columnSum = 0;
 60
 15261            for (var j = 0; j < 3; j++)
 5862            {
 5863                var value = grid[row + i][column + j];
 64
 5865                if (value < 1 || value > 9 || seen[value])
 266                {
 267                    return false;
 68                }
 69
 5670                seen[value] = true;
 71
 5672                rowSum += value;
 5673                columnSum += grid[row + j][column + i];
 5674            }
 75
 1876            if (rowSum != MagicSquareSum || columnSum != MagicSquareSum)
 277            {
 278                return false;
 79            }
 1680        }
 81
 582        if (grid[row][column] + grid[row + 1][column + 1] + grid[row + 2][column + 2] != MagicSquareSum)
 083        {
 084            return false;
 85        }
 86
 587        if (grid[row][column + 2] + grid[row + 1][column + 1] + grid[row + 2][column] != MagicSquareSum)
 088        {
 089            return false;
 90        }
 91
 592        return true;
 7593    }
 94}