Search
 
SCRIPT & CODE EXAMPLE
 

JAVA

java Prefix Sum of Matrix (Or 2D Array)

// 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);
    }
}
Comment

PREVIOUS NEXT
Code Example
Java :: if(ResultSet.next()) 
Java :: fix intellij resetting the java version everytime you add a dependency 
Java :: spigot change move speed of living entity creature 
Java :: Which of the following is an example of a Method reference? 
Java :: Java Copying Arrays Using Assignment Operator 
Java :: Provide an ADT java class for one-dimension arrays named “MyArray” 
Java :: trémaux’ method java 
Java :: move the zero elementts in array in java in tutorialspoint.dev 
Java :: Algorithms - decision 
Java :: java create a hashmap 
Java :: ConnectionString connection timeOut mongodb java 
Java :: how to increase a variable once java 
Java :: javafx.controls,javafx.fxml caused by: java.lang.classnotfoundexception: javafx.controls,javafx.fxml 
Java :: forge close gui java 
Java :: check is element present in queue java 
Java :: Double matrix 
Java :: kotless scheduled event 
Java :: system out java quick 
Java :: Rotate Vector by an angle 
Java :: class in java 
Java :: java use of super keyword 
Java :: call activity method from adapter 
Java :: how to saperate string to array 
Java :: calling a method in java 
Java :: code to get date and time in android 
Java :: public class extends implements java 
Java :: partioning operation Java 
Sql :: how to get the size of the database in postgresql 
Sql :: alter user mysql native password 
Sql :: mysql where column not integer 
ADD CONTENT
Topic
Content
Source link
Name
7+4 =