Skip to content
geeksforgeeks
  • Tutorials
    • Python
    • Java
    • Data Structures & Algorithms
    • ML & Data Science
    • Interview Corner
    • Programming Languages
    • Web Development
    • CS Subjects
    • DevOps And Linux
    • School Learning
    • Practice Coding Problems
  • Courses
    • DSA to Development
    • Get IBM Certification
    • Newly Launched!
      • Master Django Framework
      • Become AWS Certified
    • For Working Professionals
      • Interview 101: DSA & System Design
      • Data Science Training Program
      • JAVA Backend Development (Live)
      • DevOps Engineering (LIVE)
      • Data Structures & Algorithms in Python
    • For Students
      • Placement Preparation Course
      • Data Science (Live)
      • Data Structure & Algorithm-Self Paced (C++/JAVA)
      • Master Competitive Programming (Live)
      • Full Stack Development with React & Node JS (Live)
    • Full Stack Development
    • Data Science Program
    • All Courses
  • DSA
  • Interview Problems on DP
  • Practice DP
  • MCQs on DP
  • Tutorial on Dynamic Programming
  • Optimal Substructure
  • Overlapping Subproblem
  • Memoization
  • Tabulation
  • Tabulation vs Memoization
  • 0/1 Knapsack
  • Unbounded Knapsack
  • Subset Sum
  • LCS
  • LIS
  • Coin Change
  • Word Break
  • Egg Dropping Puzzle
  • Matrix Chain Multiplication
  • Palindrome Partitioning
  • DP on Arrays
  • DP with Bitmasking
  • Digit DP
  • DP on Trees
  • DP on Graph
Open In App
Next Article:
Count ways to create string of size N with given restrictions
Next article icon

Prefix Sum of Matrix (Or 2D Array)

Last Updated : 29 Mar, 2024
Comments
Improve
Suggest changes
Like Article
Like
Report

Given a matrix (or 2D array) a[][] of integers, find the prefix sum matrix for it. Let prefix sum matrix be psa[][]. The value of psa[i][j] contains the sum of all values which are above it or on the left of it.
 

prefix-sum-matrix

Recommended Practice
Prefix Sum of Matrix (Or 2D Array)
Try It!


Prerequisite: Prefix Sum - 1D

A simple solution is to find psa[i][j] by traversing and adding values from a[0][0] to a[i][j]. Time complexity of this solution is O(R * C * R * C).

An efficient solution is to use previously computed values to compute psa[i][j]. Unlike 1D array prefix sum, this is tricky, here if we simply add psa[i][j-1] and psa[i-1][j], we get sum of elements from a[0][0] to a[i-1][j-1] twice, so we subtract psa[i-1][j-1]. 

Example : 

psa[3][3] = psa[2][3] + psa[3][2] -              psa[2][2] + a[3][3]            = 6 + 6 - 4 + 1            = 9  The general formula:   psa[i][j] = psa[i-1][j] + psa[i][j-1] -               psa[i-1][j-1] + a[i][j]    Corner Cases (First row and first column)  If i = 0 and j = 0     psa[i][j] = a[i][j]  If i = 0 and j > 0     psa[i][j] = psa[i][j-1] + a[i][j]  If i > 0 and j = 0     psa[i][j] = psa[i-1][j] + a[i][j]

Below is the implementation of the above approach  

C++
// C++ Program to find prefix sum of 2d array #include <bits/stdc++.h> using namespace std;  #define R 4 #define C 5  // calculating new array void prefixSum2D(int a[][C]) {     int psa[R][C];     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 = 1; 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];     }      // displaying the values of the new array     for (int i = 0; i < R; i++) {         for (int j = 0; j < C; j++)             cout << psa[i][j] << " ";         cout << "\n";     } }  // driver code int main() {     int a[R][C] = { { 1, 1, 1, 1, 1 },                     { 1, 1, 1, 1, 1 },                     { 1, 1, 1, 1, 1 },                     { 1, 1, 1, 1, 1 } };      prefixSum2D(a);      return 0; } 
Java
// Java program to find prefix sum of 2D array import java.util.*;  class GFG {      // calculating new array     public static void prefixSum2D(int a[][])     {         int R = a.length;         int C = a[0].length;          int psa[][] = new int[R][C];          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 = 1; 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];          for (int i = 0; i < R; i++) {             for (int j = 0; j < C; j++)                 System.out.print(psa[i][j] + " ");             System.out.println();         }     }      // driver code     public static void main(String[] args)     {         int a[][] = { { 1, 1, 1, 1, 1 },                       { 1, 1, 1, 1, 1 },                       { 1, 1, 1, 1, 1 },                       { 1, 1, 1, 1, 1 } };         prefixSum2D(a);     } } 
Python3
# Python Program to find  # prefix sum of 2d array R = 4 C = 5  # calculating new array def prefixSum2D(a) :     global C, R     psa = [[0 for x in range(C)]                for y in range(R)]      psa[0][0] = a[0][0]      # Filling first row      # and first column     for i in range(1, C) :         psa[0][i] = (psa[0][i - 1] +                         a[0][i])     for i in range(0, R) :         psa[i][0] = (psa[i - 1][0] +                         a[i][0])      # updating the values in      # the cells as per the      # general formula     for i in range(1, R) :         for j in range(1, C) :              # 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])      # displaying the values     # of the new array     for i in range(0, R) :         for j in range(0, C) :             print (psa[i][j],                     end = " ")         print ()  # Driver Code a = [[ 1, 1, 1, 1, 1 ],      [ 1, 1, 1, 1, 1 ],      [ 1, 1, 1, 1, 1 ],      [ 1, 1, 1, 1, 1 ]]  prefixSum2D(a)  # This code is contributed by  # Manish Shaw(manishshaw1) 
C#
// C# program to find prefix // sum of 2D array using System;  class GFG  {      // calculating new array     static void prefixSum2D(int [,]a)     {         int R = a.GetLength(0);         int C = a.GetLength(1);          int [,]psa = new int[R, C];          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 = 1; 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];          for (int i = 0; i < R; i++)          {             for (int j = 0; j < C; j++)                 Console.Write(psa[i, j] + " ");             Console.WriteLine();         }     }      // Driver Code     static void Main()     {         int [,]a = new int[,]{{1, 1, 1, 1, 1},                               {1, 1, 1, 1, 1},                               {1, 1, 1, 1, 1},                               {1, 1, 1, 1, 1}};         prefixSum2D(a);     } }  // This code is contributed by manishshaw1 
PHP
<?php // PHP Program to find  // prefix sum of 2d array $R = 4; $C = 5;  // calculating new array function prefixSum2D($a) {     global $C, $R;     $psa = array();     $psa[0][0] = $a[0][0];      // Filling first row      // and first column     for ($i = 1; $i < $C; $i++)         $psa[0][$i] = $psa[0][$i - 1] +                               $a[0][$i];     for ($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 ($i = 1; $i < $R; $i++)     {         for ($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];     }      // displaying the values     // of the new array     for ($i = 0; $i < $R; $i++)      {         for ($j = 0; $j < $C; $j++)             echo ($psa[$i][$j]. " ");         echo ("\n");     } }  // Driver Code $a = array(array( 1, 1, 1, 1, 1 ),            array( 1, 1, 1, 1, 1 ),            array( 1, 1, 1, 1, 1 ),            array( 1, 1, 1, 1, 1 ));  prefixSum2D($a);  // This code is contributed by  // Manish Shaw(manishshaw1) ?> 
JavaScript
<script> // Javascript program to find prefix sum of 2D array  // calculating new array function prefixSum2D(a) {     let R = a.length;         let C = a[0].length;           let psa = new Array(R);         for(let i = 0; i < R; i++)         {             psa[i] = new Array(C);             for(let j = 0; j < C; j++)                 psa[i][j] = 0;         }               psa[0][0] = a[0][0];           // Filling first row and first column         for (let i = 1; i < C; i++)             psa[0][i] = psa[0][i - 1] + a[0][i];         for (let i = 1; 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 (let i = 1; i < R; i++)             for (let 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];           for (let i = 0; i < R; i++) {             for (let j = 0; j < C; j++)                 document.write(psa[i][j] + " ");             document.write("<br>");         } }  // driver code let a=[[ 1, 1, 1, 1, 1 ],                       [ 1, 1, 1, 1, 1 ],                       [ 1, 1, 1, 1, 1 ],                       [ 1, 1, 1, 1, 1 ]]; prefixSum2D(a);  // This code is contributed by avanitrachhadiya2155 </script> 

Output
1 2 3 4 5   2 4 6 8 10   3 6 9 12 15   4 8 12 16 20 

Time Complexity: O(R*C) 
Auxiliary Space: O(R*C)

Another Efficient solution in which we also use the previously calculated sums in two main steps would be:

  1. Calculate the vertical prefix sum for each column.
  2. Calculate the horizontal prefix sum for each row.

Example

// c = the number of columns  // r = the number of rows  // a is the matrix    // calculating the vertical sum for each column in the Matrix   for(column = 0 to column = c-1)      for(row = 1 to row = r-1)          a[row][column] += a[row-1][column];    // calculating the horizontal sum for each row in the Matrix   for(row = 0 to row = r-1)      for(column = 1 to column = c-1)          a[row][column] += a[row][column -1];

Below is the Implementation of the above approach

C++
#include <iostream> #include <iomanip> using namespace std; void prefixSum(int arr[3][3], int n); void print(int arr[3][3], int n); int main() {     int n = 3;     int arr[3][3] = {{10,20,30},                      {5, 10, 20},                      {2, 4, 6}                     };     prefixSum(arr, n);       print(arr, n);  } void prefixSum(int arr[3][3], int n) {     //vertical prefixsum     for (int j = 0; j < n; j++) {         for (int i = 1; i < n; i++) {             arr[i][j] += arr[i-1][j];         }     }     //horizontal prefixsum     for (int i = 0; i < n; i++) {         for (int j = 1; j < n; j++) {             arr[i][j] += arr[i][j-1];         }     } }   void print(int arr[3][3], int n) {     for (int i = 0; i < n; i++) {         for (int j = 0; j < n; j++) {             cout << setw(3) << left << arr[i][j] << " ";         }         cout << '\n';     } } 
Java
import java.io.*; import java.lang.*; import java.util.*; public class GFG {   public static void main(String[] args)   {     int n = 3;     int arr[][] = new int[][] { { 10, 20, 30 },                                { 5, 10, 20 },                                { 2, 4, 6 } };     prefixSum(arr, n);     print(arr, n);   }   static void prefixSum(int arr[][], int n)   {          // vertical prefixsum     for (int j = 0; j < n; j++) {       for (int i = 1; i < n; i++) {         arr[i][j] += arr[i - 1][j];       }     }          // horizontal prefixsum     for (int i = 0; i < n; i++) {       for (int j = 1; j < n; j++) {         arr[i][j] += arr[i][j - 1];       }     }   }   static void print(int arr[][], int n)   {     for (int i = 0; i < n; i++) {       for (int j = 0; j < n; j++) {         System.out.print(arr[i][j] + " ");       }       System.out.println();     }   } }  // This code is contributed by ishankhandelwals. 
Python3
def prefixsum(arr, n):     # vertical prefixsum     for j in range(n):         for i in range(1, n):             arr[i][j] += arr[i - 1][j]                  # horizontal prefixsum     for i in  range(n):         for j in range(1, n):             arr[i][j] += arr[i][j - 1]              def printarr(arr, n):     for i in range(n):         for j in range(n):             print(arr[i][j], end = " ")         print()          # Driver Code n = 3 arr = [[10,20,30],[5,10,20],[2,4,6]] prefixsum(arr,n) printarr(arr,n)    # This code is contributed by # Vibhu Karnwal      
C#
// C# code for above approach using System; public class gfg {   public static void prefixSum(int[,] arr,int n){     for (int j = 0; j < n; j++) {       for (int i = 1; i < n; i++) {         arr[i,j] += arr[i-1,j];       }     }          //horizontal prefixsum     for (int i = 0; i < n; i++) {       for (int j = 1; j < n; j++) {         arr[i,j] += arr[i,j-1];       }     }   }   public static void print(int[,] arr,int n){     for (int i = 0; i < n; i++) {       for (int j = 0; j < n; j++) {         Console.Write("{0} ",arr[i,j]);       }       Console.WriteLine();     }    }    public static void Main(string[] args)   {     int n = 3;     int[ , ] arr = new int[3,3] {{10,20,30},                                  {5, 10, 20},                                  {2, 4, 6}                                 } ;     prefixSum(arr, n);     print(arr, n);   } }  // This code is contributed by ishankhandelwals. 
JavaScript
// Js code for above approach function prefixSum(arr, n) {     //vertical prefixsum     for (let j = 0; j < n; j++) {         for (let i = 1; i < n; i++) {             arr[i][j] += arr[i - 1][j];         }     }     //horizontal prefixsum     for (let i = 0; i < n; i++) {         for (let j = 1; j < n; j++) {             arr[i][j] += arr[i][j - 1];         }     } } function print(arr, n) {     for (let i = 0; i < n; i++) {         for (let j = 0; j < n; j++) {             console.log(arr[i][j]);         }     } } let n = 3; let arr= [[ 10, 20, 30 ],         [5, 10, 20 ],         [ 2, 4, 6 ]]; prefixSum(arr, n); print(arr, n);  // This code is contributed by ishankhandelwals. 

Output
10  30  60    15  45  95    17  51  107 

Time Complexity: O(R*C), where R and C are the Rows and Columns of the given matrix respectively.
Auxiliary Space: O(R*C)


Next Article
Count ways to create string of size N with given restrictions

A

akash1295
Improve
Article Tags :
  • Dynamic Programming
  • Mathematical
  • DSA
  • Arrays
  • prefix-sum
Practice Tags :
  • Arrays
  • Dynamic Programming
  • Mathematical
  • prefix-sum

Similar Reads

    Inclusion Exclusion principle for Competitive Programming
    What is the Inclusion-Exclusion Principle?The inclusion-exclusion principle is a combinatoric way of computing the size of multiple intersecting sets or the probability of complex overlapping events. Generalised Inclusion-Exclusion over Set:For 2 Intersecting Set A and B: A\bigcup B= A + B - A\bigca
    5 min read
    Prefix Sum of Matrix (Or 2D Array)
    Given a matrix (or 2D array) a[][] of integers, find the prefix sum matrix for it. Let prefix sum matrix be psa[][]. The value of psa[i][j] contains the sum of all values which are above it or on the left of it. Recommended PracticePrefix Sum of Matrix (Or 2D Array)Try It! Prerequisite: Prefix Sum -
    12 min read
    Count ways to create string of size N with given restrictions
    Given a number N, the task is to count the number of ways to create a string of size N (only with capital alphabets) such that no vowel is between two consonants and the string does not start with the letter 'A' and does not end with the letter 'Z'. (Print the answer modulo 109 + 7). Examples: Input
    15 min read
    Count ways of selecting X red balls and Y blue balls
    Given integers A, B, C, and D, There are two boxes First Box has A red balls and B blue balls and the second box has C red balls and D blue balls, the task is to count ways to select 3 red balls and 3 blue balls so that there are 3 balls drawn from the first box and 3 balls drawn from the second box
    15+ min read
    Count ways choosing K balls from any given A boxes
    Given integers A and K, there are A boxes the first box contains K balls, the Second box contains K + 1 balls, the Third Box contains K + 3 balls so on till the A'th box contains K + A balls, the task for this problem is to count the number of ways of picking K balls from any of the boxes. (Print an
    9 min read
    Count number of ways in which following people can be arranged
    Given integers X and Y representing X girls and Y boys, the task is to count the number of ways arranging X girls and Y boys such that girls always stay together and two boys from Y refuse to stay consecutive. Print the answer modulo 109 + 7. Examples: Input: X = 2, Y = 2Output: 4Explanation: Let's
    6 min read
    Count ways to choose Triplets of Pairs such that either first or second values are distinct
    Given an array of pairs arr[] of size N (N ? 3) where each element of pair is at most N and each pair is unique, the task is to determine the number of ways to select triplets from the given N pairs that satisfy at least one of the following conditions: The first value (a) of each pair should be dis
    7 min read
geeksforgeeks-footer-logo
Corporate & Communications Address:
A-143, 7th Floor, Sovereign Corporate Tower, Sector- 136, Noida, Uttar Pradesh (201305)
Registered Address:
K 061, Tower K, Gulshan Vivante Apartment, Sector 137, Noida, Gautam Buddh Nagar, Uttar Pradesh, 201305
GFG App on Play Store GFG App on App Store
Advertise with us
  • Company
  • About Us
  • Legal
  • Privacy Policy
  • In Media
  • Contact Us
  • Advertise with us
  • GFG Corporate Solution
  • Placement Training Program
  • Languages
  • Python
  • Java
  • C++
  • PHP
  • GoLang
  • SQL
  • R Language
  • Android Tutorial
  • Tutorials Archive
  • DSA
  • Data Structures
  • Algorithms
  • DSA for Beginners
  • Basic DSA Problems
  • DSA Roadmap
  • Top 100 DSA Interview Problems
  • DSA Roadmap by Sandeep Jain
  • All Cheat Sheets
  • Data Science & ML
  • Data Science With Python
  • Data Science For Beginner
  • Machine Learning
  • ML Maths
  • Data Visualisation
  • Pandas
  • NumPy
  • NLP
  • Deep Learning
  • Web Technologies
  • HTML
  • CSS
  • JavaScript
  • TypeScript
  • ReactJS
  • NextJS
  • Bootstrap
  • Web Design
  • Python Tutorial
  • Python Programming Examples
  • Python Projects
  • Python Tkinter
  • Python Web Scraping
  • OpenCV Tutorial
  • Python Interview Question
  • Django
  • Computer Science
  • Operating Systems
  • Computer Network
  • Database Management System
  • Software Engineering
  • Digital Logic Design
  • Engineering Maths
  • Software Development
  • Software Testing
  • DevOps
  • Git
  • Linux
  • AWS
  • Docker
  • Kubernetes
  • Azure
  • GCP
  • DevOps Roadmap
  • System Design
  • High Level Design
  • Low Level Design
  • UML Diagrams
  • Interview Guide
  • Design Patterns
  • OOAD
  • System Design Bootcamp
  • Interview Questions
  • Inteview Preparation
  • Competitive Programming
  • Top DS or Algo for CP
  • Company-Wise Recruitment Process
  • Company-Wise Preparation
  • Aptitude Preparation
  • Puzzles
  • School Subjects
  • Mathematics
  • Physics
  • Chemistry
  • Biology
  • Social Science
  • English Grammar
  • Commerce
  • World GK
  • GeeksforGeeks Videos
  • DSA
  • Python
  • Java
  • C++
  • Web Development
  • Data Science
  • CS Subjects
@GeeksforGeeks, Sanchhaya Education Private Limited, All rights reserved
We use cookies to ensure you have the best browsing experience on our website. By using our site, you acknowledge that you have read and understood our Cookie Policy & Privacy Policy
Lightbox
Improvement
Suggest Changes
Help us improve. Share your suggestions to enhance the article. Contribute your expertise and make a difference in the GeeksforGeeks portal.
geeksforgeeks-suggest-icon
Create Improvement
Enhance the article with your expertise. Contribute to the GeeksforGeeks community and help create better learning resources for all.
geeksforgeeks-improvement-icon
Suggest Changes
min 4 words, max Words Limit:1000

Thank You!

Your suggestions are valuable to us.

What kind of Experience do you want to share?

Interview Experiences
Admission Experiences
Career Journeys
Work Experiences
Campus Experiences
Competitive Exam Experiences