Given an n x n square matrix, find sum of all sub-squares of size k x k
Solution:
for (int i = 0; i < n-k+1; i++) { for (int j = 0; j < n-k+1; j++) { int sum = 0; for (int p = i; p < k+i; p++) for (int q = j; q < k+j; q++) sum += mat[p][q]; } }
Comments
Post a Comment