Related Articles

# Prefix Sum Of 3D Array

• Last Updated : 23 Jul, 2021

Prerequisite: Prefix Sum â€“ 1D, Prefix Sum -2D

Given a 3-Dimensional array of integers A[L][R][C], where L, R, and C are dimensions of the array (Layer, Row, Column). Find the prefix sum 3d array for it. Let the Prefix sum 3d array be pre[L][R][C]. Here pre[k][i][j]  gives the sum of all integers between pre[0][0][0] and pre[k][i][j] (including both).

Example:

Input: A[L][R][C] = {

{{1, 1, 1, 1}, //Layer 1

{1, 1, 1, 1},

{1, 1, 1, 1},

{1, 1, 1, 1}},

{{1, 1, 1, 1}, //Layer 2

{1, 1, 1, 1},

{1, 1, 1, 1},

{1, 1, 1, 1}},

{{1, 1, 1, 1}, //Layer 3

{1, 1, 1, 1},

{1, 1, 1, 1},

{1, 1, 1, 1}},

{{1, 1, 1, 1}, //Layer 4

{1, 1, 1, 1},

{1, 1, 1, 1},

{1, 1, 1, 1}}

}

Output: pre[L][R][C]

Layer 1:

1 2 3 4

2 4 6 8

3 6 9 12

4 8 12 16

Layer 2:

2 4 6 8

4 8 12 16

6 12 18 24

8 16 24 32

Layer 3:

3 6 9 12

6 12 18 24

9 18 27 36

12 24 36 48

Layer 4:

4 8 12 16

8 16 24 32

12 24 36 48

16 32 48 64

Approach: Consider the above image to understand that the cell  pre[0][0][0] is at the origin of x, y, z-axis. To fill the pre[][][] Array perform the steps below for calculating the prefix sum.

Below is the implementation of the above approach.

## C++

 `// C++ program for the above approach.``#include ``using` `namespace` `std;` `// Declaring size of the array``#define L 4 // Layer``#define R 4 // Row``#define C 4 // Column` `// Calculating prefix sum array``void` `prefixSum3d(``int` `A[L][R][C])``{``    ``int` `pre[L][R][C];` `    ``// Step 0:``    ``pre[0][0][0] = A[0][0][0];` `    ``// Step 1: Filling the first row,``    ``// column, and pile of ceils.``    ``// Using prefix sum of 1d array``    ``for` `(``int` `i = 1; i < L; i++)``        ``pre[i][0][0] = pre[i - 1][0][0] + A[i][0][0];` `    ``for` `(``int` `i = 1; i < R; i++)``        ``pre[0][i][0] = pre[0][i - 1][0] + A[0][i][0];` `    ``for` `(``int` `i = 1; i < C; i++)``        ``pre[0][0][i] = pre[0][0][i - 1] + A[0][0][i];` `    ``// Step 2: Filling the cells``    ``// of sides(made up using cells)``    ``// which have common element A[0][0][0].``    ``// using prefix sum on 2d array``    ``for` `(``int` `k = 1; k < L; k++) {``        ``for` `(``int` `i = 1; i < R; i++) {``            ``pre[k][i][0] = A[k][i][0]``                           ``+ pre[k - 1][i][0]``                           ``+ pre[k][i - 1][0]``                           ``- pre[k - 1][i - 1][0];``        ``}``    ``}``    ``for` `(``int` `i = 1; i < R; i++) {``        ``for` `(``int` `j = 1; j < C; j++) {``            ``pre[0][i][j] = A[0][i][j]``                           ``+ pre[0][i - 1][j]``                           ``+ pre[0][i][j - 1]``                           ``- pre[0][i - 1][j - 1];``        ``}``    ``}``    ``for` `(``int` `j = 1; j < C; j++) {``        ``for` `(``int` `k = 1; k < L; k++) {``            ``pre[k][0][j] = A[k][0][j]``                           ``+ pre[k - 1][0][j]``                           ``+ pre[k][0][j - 1]``                           ``- pre[k - 1][0][j - 1];``        ``}``    ``}` `    ``// Step 3: Filling value``    ``// in remaining cells using formula``    ``for` `(``int` `k = 1; k < L; k++) {``        ``for` `(``int` `i = 1; i < R; i++) {``            ``for` `(``int` `j = 1; j < C; j++) {``                ``pre[k][i][j] = A[k][i][j]` `                               ``+ pre[k - 1][i][j]``                               ``+ pre[k][i - 1][j]``                               ``+ pre[k][i][j - 1]` `                               ``- pre[k - 1][i - 1][j]``                               ``- pre[k][i - 1][j - 1]``                               ``- pre[k - 1][i][j - 1]` `                               ``+ pre[k - 1][i - 1][j - 1];``            ``}``        ``}``    ``}` `    ``// Displaying final prefix sum of array``    ``for` `(``int` `k = 0; k < L; k++) {``        ``cout << ``"Layer "` `<< k + 1 << ``':'` `<< endl;``        ``for` `(``int` `i = 0; i < R; i++) {``            ``for` `(``int` `j = 0; j < C; j++) {``                ``cout << pre[k][i][j] << ``" "``;``            ``}``            ``cout << endl;``        ``}``        ``cout << endl;``    ``}``}` `// Driver Code``int` `main()``{``    ``int` `A[L][R][C] = {``        ``{ { 1, 1, 1, 1 }, ``// Layer 1``          ``{ 1, 1, 1, 1 },``          ``{ 1, 1, 1, 1 },``          ``{ 1, 1, 1, 1 } },` `        ``{ { 1, 1, 1, 1 }, ``// Layer 2``          ``{ 1, 1, 1, 1 },``          ``{ 1, 1, 1, 1 },``          ``{ 1, 1, 1, 1 } },` `        ``{ { 1, 1, 1, 1 }, ``// Layer 3``          ``{ 1, 1, 1, 1 },``          ``{ 1, 1, 1, 1 },``          ``{ 1, 1, 1, 1 } },` `        ``{ { 1, 1, 1, 1 }, ``// Layer 4``          ``{ 1, 1, 1, 1 },``          ``{ 1, 1, 1, 1 },``          ``{ 1, 1, 1, 1 } }``    ``};` `    ``prefixSum3d(A);``    ``return` `0;``}`

## Java

 `// java program to calculate prefix sum of 3d array``import` `java.util.*;` `class` `GFG``{``  ` `    ``// Declaring size of the array``    ``public` `static` `int` `L = ``4``;``// Layer``    ``public` `static` `int` `R = ``4``;``// Row``    ``public` `static` `int` `C = ``4``;``// Column` `    ``// Calculating prefix``    ``public` `static` `void` `prefixSum3d(``int` `A[][][]) {``        ``int` `pre[][][] = ``new` `int``[L][R][C];``        ` `          ``// Step 0:``        ``pre[``0``][``0``][``0``] = A[``0``][``0``][``0``];` `        ``// Step 1: Filling the first row,column, and pile of ceils.``        ``// Using prefix sum of 1d array``        ``for` `(``int` `i = ``1``; i < L; i++)``            ``pre[i][``0``][``0``] = pre[i - ``1``][``0``][``0``] + A[i][``0``][``0``];` `        ``for` `(``int` `i = ``1``; i < R; i++)``            ``pre[``0``][i][``0``] = pre[``0``][i - ``1``][``0``] + A[``0``][i][``0``];` `        ``for` `(``int` `i = ``1``; i < C; i++)``            ``pre[``0``][``0``][i] = pre[``0``][``0``][i - ``1``] + A[``0``][``0``][i];` `        ``// Step 2: Filling the cells of sides(made up using cells)``        ``// which have common element A[0][0][0].``        ``// using prefix sum on 2d array``        ``for` `(``int` `k = ``1``; k < L; k++) {``            ``for` `(``int` `i = ``1``; i < R; i++) {``                ``pre[k][i][``0``] = A[k][i][``0``] + pre[k - ``1``][i][``0``] + pre[k][i - ``1``][``0``]``                                  ``- pre[k - ``1``][i - ``1``][``0``];``            ``}``        ``}``        ``for` `(``int` `i = ``1``; i < R; i++) {``            ``for` `(``int` `j = ``1``; j < C; j++) {``                ``pre[``0``][i][j] = A[``0``][i][j] + pre[``0``][i - ``1``][j] + pre[``0``][i][j - ``1``]``                                  ``- pre[``0``][i - ``1``][j - ``1``];``            ``}``        ``}``        ``for` `(``int` `j = ``1``; j < C; j++) {``            ``for` `(``int` `k = ``1``; k < L; k++) {``                ``pre[k][``0``][j] = A[k][``0``][j] + pre[k - ``1``][``0``][j] + pre[k][``0``][j - ``1``]``                                  ``- pre[k - ``1``][``0``][j - ``1``];``            ``}``        ``}` `        ``// Step 3: Filling value in remaining cells using formula``        ``for` `(``int` `k = ``1``; k < L; k++) {``            ``for` `(``int` `i = ``1``; i < R; i++) {``                ``for` `(``int` `j = ``1``; j < C; j++) {``                    ``pre[k][i][j] = A[k][i][j]``                            ``+ pre[k - ``1``][i][j] + pre[k][i - ``1``][j] + pre[k][i][j - ``1``]``                            ``- pre[k - ``1``][i - ``1``][j] - pre[k][i - ``1``][j - ``1``] - pre[k - ``1``][i][j - ``1``]``                            ``+ pre[k - ``1``][i - ``1``][j - ``1``];``                ``}``            ``}``        ``}` `        ``// Displaying final prefix sum of array``        ``for` `(``int` `k = ``0``; k < L; k++) {``            ``System.out.println(``"Layer "` `+ (k + ``1``) + ``":"``);``            ``for` `(``int` `i = ``0``; i < R; i++) {``                ``for` `(``int` `j = ``0``; j < C; j++) {``                    ``System.out.print(pre[k][i][j] + ``" "``);``                ``}``                ``System.out.println();``            ``}``            ``System.out.println();``        ``}``    ``}` `    ``// Driver program to test above function``    ``public` `static` `void` `main(String[] args) {``        ``int` `A[][][] = {``            ``{{``1``, ``1``, ``1``, ``1``}, ``// Layer 1``            ``{``1``, ``1``, ``1``, ``1` `},``            ``{``1``, ``1``, ``1``, ``1` `},``            ``{``1``, ``1``, ``1``, ``1` `}},` `            ``{{``1``, ``1``, ``1``, ``1``}, ``// Layer 2``            ``{``1``, ``1``, ``1``, ``1` `},``            ``{``1``, ``1``, ``1``, ``1` `},``            ``{``1``, ``1``, ``1``, ``1` `}},``            ` `            ``{{``1``, ``1``, ``1``, ``1``}, ``// Layer 3``            ``{``1``, ``1``, ``1``, ``1` `},``            ``{``1``, ``1``, ``1``, ``1` `},``            ``{``1``, ``1``, ``1``, ``1` `}},``            ` `            ``{{``1``, ``1``, ``1``, ``1``}, ``// Layer 4``            ``{``1``, ``1``, ``1``, ``1` `},``            ``{``1``, ``1``, ``1``, ``1` `},``            ``{``1``, ``1``, ``1``, ``1` `}}``        ``};` `        ``prefixSum3d(A);``    ``}``}` `// This code is contributed by gajjardeep50.`

## Python3

 `# python program to calculate prefix sum of 3d array``# Declaring size of the array``L ``=` `4`  `# Layer``R ``=` `4`  `# Row``C ``=` `4`  `# Column` `# Calculating prefix``def` `prefixSum3d(A):` `    ``pre ``=` `[[[``0` `for` `a ``in` `range``(C)]``            ``for` `b ``in` `range``(R)]``           ``for` `d ``in` `range``(L)]``    ` `    ``# Step 0:``    ``pre[``0``][``0``][``0``] ``=` `A[``0``][``0``][``0``]` `    ``# Step 1: Filling the first row,column, and pile of ceils.``    ``# Using prefix sum of 1d array``    ``for` `i ``in` `range``(``1``, L):``        ``pre[i][``0``][``0``] ``=` `pre[i ``-` `1``][``0``][``0``] ``+` `A[i][``0``][``0``]` `    ``for` `i ``in` `range``(``1``, R):``        ``pre[``0``][i][``0``] ``=` `pre[``0``][i ``-` `1``][``0``] ``+` `A[``0``][i][``0``]` `    ``for` `i ``in` `range``(``1``, C):``        ``pre[``0``][``0``][i] ``=` `pre[``0``][``0``][i ``-` `1``] ``+` `A[``0``][``0``][i]` `    ``# Step 2: Filling the cells of sides(made up using cells)``    ``# which have common element A[0][0][0].``    ``# using prefix sum on 2d array``    ``for` `k ``in` `range``(``1``, L):``        ``for` `i ``in` `range``(``1``, R):``            ``pre[k][i][``0``] ``=` `(A[k][i][``0``] ``+` `pre[k ``-` `1``][i][``0``] ``+` `pre[k][i ``-` `1``][``0``]``                            ``-` `pre[k ``-` `1``][i ``-` `1``][``0``])``    ``for` `i ``in` `range``(``1``, R):``        ``for` `j ``in` `range``(``1``, C):``            ``pre[``0``][i][j] ``=` `(A[``0``][i][j] ``+` `pre[``0``][i ``-` `1``][j] ``+` `pre[``0``][i][j ``-` `1``]``                            ``-` `pre[``0``][i ``-` `1``][j ``-` `1``])``    ``for` `j ``in` `range``(``1``, C):``        ``for` `k ``in` `range``(``1``, L):``            ``pre[k][``0``][j] ``=` `(A[k][``0``][j] ``+` `pre[k ``-` `1``][``0``][j] ``+` `pre[k][``0``][j ``-` `1``]``                            ``-` `pre[k ``-` `1``][``0``][j ``-` `1``])` `    ``# Step 3: Filling value in remaining cells using formula``    ``for` `k ``in` `range``(``1``, L):``        ``for` `i ``in` `range``(``1``, R):``            ``for` `j ``in` `range``(``1``, C):``                ``pre[k][i][j] ``=` `(A[k][i][j]` `                                ``+` `pre[k ``-` `1``][i][j]``                                ``+` `pre[k][i ``-` `1``][j]``                                ``+` `pre[k][i][j ``-` `1``]` `                                ``-` `pre[k ``-` `1``][i ``-` `1``][j]``                                ``-` `pre[k][i ``-` `1``][j ``-` `1``]``                                ``-` `pre[k ``-` `1``][i][j ``-` `1``]` `                                ``+` `pre[k ``-` `1``][i ``-` `1``][j ``-` `1``])` `    ``# Displaying final prefix sum of array``    ``for` `k ``in` `range``(L):``        ``print``(``"Layer"``, k``+``1``, ``":"``)``        ``for` `i ``in` `range``(R):``            ``for` `j ``in` `range``(C):``                ``print``(pre[k][i][j], end``=``' '``)``            ``print``()``        ``print``()`  `# Driver program to test above function``A ``=` `[``    ``[[``1``, ``1``, ``1``, ``1``],  ``# Layer 1``     ``[``1``, ``1``, ``1``, ``1``],``     ``[``1``, ``1``, ``1``, ``1``],``     ``[``1``, ``1``, ``1``, ``1``]],` `    ``[[``1``, ``1``, ``1``, ``1``],  ``# Layer 2``     ``[``1``, ``1``, ``1``, ``1``],``     ``[``1``, ``1``, ``1``, ``1``],``     ``[``1``, ``1``, ``1``, ``1``]],` `    ``[[``1``, ``1``, ``1``, ``1``],  ``# Layer 3``     ``[``1``, ``1``, ``1``, ``1``],``     ``[``1``, ``1``, ``1``, ``1``],``     ``[``1``, ``1``, ``1``, ``1``]],` `    ``[[``1``, ``1``, ``1``, ``1``],  ``# Layer 4``     ``[``1``, ``1``, ``1``, ``1``],``     ``[``1``, ``1``, ``1``, ``1``],``     ``[``1``, ``1``, ``1``, ``1``]]``]` `prefixSum3d(A)` `# This code is contributed by gajjardeep50.`

## C#

 `// C# program for the above approach.``using` `System;``using` `System.Collections.Generic;` `class` `GFG{` `// Declaring size of the array``static` `int` `L = 4; ``// Layer``static` `int` `R = 4; ``// Row``static` `int` `C = 4; ``// Column` `// Calculating prefix sum array``static` `void` `prefixSum3d(``int` `[,,]A)``{``    ``int` `[,,]pre = ``new` `int``[L, R, C];` `    ``// Step 0:``    ``pre[0, 0, 0] = A[0, 0, 0];` `    ``// Step 1: Filling the first row,``    ``// column, and pile of ceils.``    ``// Using prefix sum of 1d array``    ``for``(``int` `i = 1; i < L; i++)``        ``pre[i, 0, 0] = pre[i - 1, 0, 0] + A[i, 0, 0];` `    ``for``(``int` `i = 1; i < R; i++)``        ``pre[0, i, 0] = pre[0, i - 1, 0] + A[0, i, 0];` `    ``for``(``int` `i = 1; i < C; i++)``        ``pre[0, 0, i] = pre[0, 0, i - 1] + A[0, 0, i];` `    ``// Step 2: Filling the cells``    ``// of sides(made up using cells)``    ``// which have common element A[0][0][0].``    ``// using prefix sum on 2d array``    ``for``(``int` `k = 1; k < L; k++)``    ``{``        ``for``(``int` `i = 1; i < R; i++)``        ``{``            ``pre[k, i, 0] = A[k, i, 0] + pre[k - 1, i, 0] +``                                        ``pre[k, i - 1, 0] -``                                        ``pre[k - 1, i - 1, 0];``        ``}``    ``}``    ``for``(``int` `i = 1; i < R; i++)``    ``{``        ``for``(``int` `j = 1; j < C; j++)``        ``{``            ``pre[0, i, j] = A[0, i, j] + pre[0, i - 1, j] +``                                        ``pre[0, i, j - 1] -``                                        ``pre[0, i - 1, j - 1];``        ``}``    ``}``    ``for``(``int` `j = 1; j < C; j++)``    ``{``        ``for``(``int` `k = 1; k < L; k++)``        ``{``            ``pre[k, 0, j] = A[k, 0, j] + pre[k - 1, 0, j] +``                                        ``pre[k, 0, j - 1] -``                                        ``pre[k - 1, 0, j - 1];``        ``}``    ``}` `    ``// Step 3: Filling value``    ``// in remaining cells using formula``    ``for``(``int` `k = 1; k < L; k++)``    ``{``        ``for``(``int` `i = 1; i < R; i++)``        ``{``            ``for``(``int` `j = 1; j < C; j++)``            ``{``                ``pre[k, i, j] = A[k, i, j] + pre[k - 1, i, j] +``                                            ``pre[k, i - 1, j] +``                                            ``pre[k, i, j - 1] -``                                            ``pre[k - 1, i - 1, j] -``                                            ``pre[k, i - 1, j - 1] -``                                            ``pre[k - 1, i, j - 1] +``                                            ``pre[k - 1, i - 1, j - 1];``            ``}``        ``}``    ``}` `    ``// Displaying final prefix sum of array``    ``for``(``int` `k = 0; k < L; k++)``    ``{``        ``Console.WriteLine(``"Layer "` `+ k + 1 + ``":"``);``        ``for``(``int` `i = 0; i < R; i++)``        ``{``            ``for``(``int` `j = 0; j < C; j++)``            ``{``                ``Console.Write(pre[k, i, j] +``" "``);``            ``}``            ``Console.WriteLine();``        ``}``        ``Console.WriteLine();``    ``}``}` `// Driver Code``public` `static` `void` `Main()``{``    ``int` `[,,]A = {``        ``{ { 1, 1, 1, 1 }, ``// Layer 1``          ``{ 1, 1, 1, 1 },``          ``{ 1, 1, 1, 1 },``          ``{ 1, 1, 1, 1 } },` `        ``{ { 1, 1, 1, 1 }, ``// Layer 2``          ``{ 1, 1, 1, 1 },``          ``{ 1, 1, 1, 1 },``          ``{ 1, 1, 1, 1 } },` `        ``{ { 1, 1, 1, 1 }, ``// Layer 3``          ``{ 1, 1, 1, 1 },``          ``{ 1, 1, 1, 1 },``          ``{ 1, 1, 1, 1 } },` `        ``{ { 1, 1, 1, 1 }, ``// Layer 4``          ``{ 1, 1, 1, 1 },``          ``{ 1, 1, 1, 1 },``          ``{ 1, 1, 1, 1 } }``    ``};` `    ``prefixSum3d(A);``}``}` `// This code is contributed by SURENDRA_GANGWAR`

## Javascript

 ``
Output
```Layer 1:
1 2 3 4
2 4 6 8
3 6 9 12
4 8 12 16

Layer 2:
2 4 6 8
4 8 12 16
6 12 18 24
8 16 24 32

Layer 3:
3 6 9 12
6 12 18 24
9 18 27 36
12 24 36 48

Layer 4:
4 8 12 16
8 16 24 32
12 24 36 48
16 32 48 64 ```

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

Attention reader! Don’t stop learning now. Get hold of all the important DSA concepts with the DSA Self Paced Course at a student-friendly price and become industry ready.  To complete your preparation from learning a language to DS Algo and many more,  please refer Complete Interview Preparation Course.

In case you wish to attend live classes with experts, please refer DSA Live Classes for Working Professionals and Competitive Programming Live for Students.

My Personal Notes arrow_drop_up