I have a question

How to create the prefix array?
Do I run 2 nested loops to go through every element of the matrix ?

Hi @sahilsaini137
To create prefix sum of a 2d matrix you have to use nested for loops. This can be computed as follows :

psa[0][0] = a[0][0];

// Filling first row and first column 

for (int i = 1; i < C; i++) 

    psa[0][i] = psa[0][i - 1] + a[0][i]; 

for (int i = 0; i < R; i++) 

    psa[i][0] = psa[i - 1][0] + a[i][0]; 



// updating the values in the cells 

// as per the general formula 

for (int i = 1; i < R; i++) { 

    for (int j = 1; j < C; j++) 



        // values in the cells of new 

        // array are updated 

        psa[i][j] = psa[i - 1][j] + psa[i][j - 1] 

                    - psa[i - 1][j - 1] + a[i][j]; 

}

Ok i will try it Thanks :slight_smile:

Hi @sahilsaini137
If your doubt is clear than mark it as resolved.