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

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