Given an n x n square matrix, find sum of all sub-squares of size k x k

Solution:
1
2
3
4
5
6
7
8
9
10
11
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

Popular posts from this blog

Print staircase with both base and height equal to n

Find the maximum occurring character in given String ?

Find the longest substring without repeating characters