FastPrepSum Every Third Spiral Element

Sum Every Third Spiral Element

ZipRecruiter logoZipRecruiter● MediumNEW GRADOA
Learn

Problem statement

Traverse a square integer matrix in clockwise spiral order starting at the top-left. Return the sum of elements at zero-based spiral indices divisible by three.

Function

sumEveryThirdSpiral(matrix: int[][]) → long

Examples

Example 1

matrix = [[1,2,3],[4,5,6],[7,8,9]]return = 14

The selected spiral values are 1, 6, and 7.

Example 2

matrix = [[5]]return = 5

The only value has spiral index zero.

Constraints

  • 1 <= matrix.length == matrix[i].length <= 500

More ZipRecruiter problems

See ZipRecruiter hiring insights
public long sumEveryThirdSpiral(int[][] matrix) {
    // Write your code here.
}
matrix[[1,2,3],[4,5,6],[7,8,9]]
expected14
Checking account…