Skip to content
geeksforgeeks
  • 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
  • Tutorials
    • Data Structures & Algorithms
    • ML & Data Science
    • Interview Corner
    • Programming Languages
    • Web Development
    • CS Subjects
    • DevOps And Linux
    • School Learning
  • Practice
    • Build your AI Agent
    • GfG 160
    • Problem of the Day
    • Practice Coding Problems
    • GfG SDE Sheet
  • Contests
    • Accenture Hackathon (Ending Soon!)
    • GfG Weekly [Rated Contest]
    • Job-A-Thon Hiring Challenge
    • All Contests and Events
  • DSA
  • Interview Questions on Array
  • Practice Array
  • MCQs on Array
  • Tutorial on Array
  • Types of Arrays
  • Array Operations
  • Subarrays, Subsequences, Subsets
  • Reverse Array
  • Static Vs Arrays
  • Array Vs Linked List
  • Array | Range Queries
  • Advantages & Disadvantages
Open In App
Next Article:
Program to find sum of elements in a given 2D array
Next article icon

Find the sum between given cells of a 3D Array

Last Updated : 11 Nov, 2021
Comments
Improve
Suggest changes
Like Article
Like
Report

Prerequisite: Prefix Sum – 3D

Given a 3-Dimensional array of integers arr[L][R][C] (where L, R, and C are dimensions of the array) and 6 integers D, E, F, X, Y, Z, find the sum of integers between arr[D][E][F] and arr[X][Y][Z] inclusively.

Example:

Input:
 A[L][R][C]:
{{ { 1, 1, 1, 1 }
{ 1, 1, 1, 1 }, 
{ 1, 1, 1, 1 }, 
{ 1, 1, 1, 1 } }, 
{ { 1, 1, 1, 1 }
{ 1, 1, 1, 1 }, 
{ 1, 1, 1, 1 }, 
{ 1, 1, 1, 1 } }, 
{ { 1, 1, 1, 1 }
{ 1, 1, 1, 1 }, 
{ 1, 1, 1, 1 }, 
{ 1, 1, 1, 1 } }, 
{ { 1, 1, 1, 1 }
{ 1, 1, 1, 1 }, 
{ 1, 1, 1, 1 }, 
{ 1, 1, 1, 1 } }};

A: 1, B: 1, C: 1, X: 1, Y: 1, Z: 1

Output: 27

Explanation: 
The sum between arr(1, 1, 1) and arr(3, 3, 3) is 27

Approach:

  • Calculate the prefix sum of given array arr[L][R][C] and store it in a variable prefixSum (see this for reference ).
  • Create a variable sum, which will store the answer i.e. sum between arr[D][E][F] and arr[X][Y][Z] and initialise it with the prefixSum till X, Y, Z. So, sum=prefixSum[X][Y][Z].
  • Now, just remove the value of the cells not needed in this prefix sum. This can be done by:
    • Remove prefix sum till (D-1, Y, Z): To remove the unnecessary cells from the left of the required region.
    • Remove prefix sum till (X, E-1, Z): To remove the unnecessary cells from the bottom of each layer, not present in the required region.
    • Remove prefix sum till (X, Y, F-1): To remove the unnecessary layers present below the required region.
  • Now the region below (D-1, E-1, Z), (X, E-1, F-1), (D-1, Y, F-1) is removed twice. To compensate for this, add the prefix sum till all these points into the sum.
  • Now, while compensating the cell removed twice, cells below (D-1, E-1, F-1) are considered again. Remove them to get the final answer.

Below is the implementation of the above approach:

C++
// C++ program for the above approach. #include <bits/stdc++.h> 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(     vector<vector<vector<int> > >& arr,     vector<vector<vector<int> > >& prefixSum) {     // Step 0:     prefixSum[0][0][0] = arr[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++)         prefixSum[i][0][0]             = prefixSum[i - 1][0][0] + arr[i][0][0];      for (int i = 1; i < R; i++)         prefixSum[0][i][0]             = prefixSum[0][i - 1][0] + arr[0][i][0];      for (int i = 1; i < C; i++)         prefixSum[0][0][i]             = prefixSum[0][0][i - 1] + arr[0][0][i];      // Step 2: Filling the cells     // of sides(made up using cells)     // which have common element arr[0][0][0].     // using prefix sum on 2d array     for (int k = 1; k < L; k++) {         for (int i = 1; i < R; i++) {             prefixSum[k][i][0]                 = arr[k][i][0] + prefixSum[k - 1][i][0]                   + prefixSum[k][i - 1][0]                   - prefixSum[k - 1][i - 1][0];         }     }     for (int i = 1; i < R; i++) {         for (int j = 1; j < C; j++) {             prefixSum[0][i][j]                 = arr[0][i][j] + prefixSum[0][i - 1][j]                   + prefixSum[0][i][j - 1]                   - prefixSum[0][i - 1][j - 1];         }     }     for (int j = 1; j < C; j++) {         for (int k = 1; k < L; k++) {             prefixSum[k][0][j]                 = arr[k][0][j] + prefixSum[k - 1][0][j]                   + prefixSum[k][0][j - 1]                   - prefixSum[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++) {                 prefixSum[k][i][j]                     = arr[k][i][j]                        + prefixSum[k - 1][i][j]                       + prefixSum[k][i - 1][j]                       + prefixSum[k][i][j - 1]                        - prefixSum[k - 1][i - 1][j]                       - prefixSum[k][i - 1][j - 1]                       - prefixSum[k - 1][i][j - 1]                        + prefixSum[k - 1][i - 1][j - 1];             }         }     } }  int calculateSum(     vector<vector<vector<int> > >& arr,     vector<vector<vector<int> > >& prefixSum,     int D, int E, int F, int X, int Y, int Z) {      // store prefix sum up to arr[X][Y][Z]:     int sum = prefixSum[X][Y][Z];      // Remove prefix sum till D, E, F.     if (D > 0) {         sum -= prefixSum[D - 1][Y][Z];     }     if (E > 0) {         sum -= prefixSum[X][E - 1][Z];     }     if (F > 0) {         sum -= prefixSum[X][Y][F - 1];     }      // Add to compensate cells removed multiple times.     if (D > 0 && E > 0) {         sum += prefixSum[D - 1][E - 1][Z];     }     if (E > 0 && F > 0) {         sum += prefixSum[X][E - 1][F - 1];     }     if (F > 0 && D > 0) {         sum += prefixSum[D - 1][Y][F - 1];     }      // Removing cells added twice in the above step.     if (D > 0 && E > 0 && F > 0) {         sum -= prefixSum[D - 1][E - 1][F - 1];     }      return sum; }  // Driver Code int main() {     // Given 3D array:     vector<vector<vector<int> > > arr(         L, vector<vector<int> >(R, vector<int>(C)));     arr = { { { 1, 1, 1, 1 },               { 1, 1, 1, 1 },               { 1, 1, 1, 1 },               { 1, 1, 1, 1 } },              { { 1, 1, 1, 1 },               { 1, 1, 1, 1 },               { 1, 1, 1, 1 },               { 1, 1, 1, 1 } },              { { 1, 1, 1, 1 },               { 1, 1, 1, 1 },               { 1, 1, 1, 1 },               { 1, 1, 1, 1 } },              { { 1, 1, 1, 1 },               { 1, 1, 1, 1 },               { 1, 1, 1, 1 },               { 1, 1, 1, 1 } } };      // To store the prefixSum     vector<vector<vector<int> > > prefixSum(         L, vector<vector<int> >(R, vector<int>(C)));      // To calculate the prefixSum     prefixSum3d(arr, prefixSum);     int D, E, F, X, Y, Z;     D = 1;     E = 1;     F = 1;     X = 3;     Y = 3;     Z = 3;      cout << calculateSum(arr, prefixSum, D, E, F, X, Y, Z);     return 0; } 
Java
// Java program for the above approach. import java.util.*;  class GFG{  // Declaring size of the array static final int L = 4; // Layer static final int R = 4; // Row static final int C = 4; // Column  // Calculating prefix sum array static void prefixSum3d(     int [][][] arr,     int [][][] prefixSum) {     // Step 0:     prefixSum[0][0][0] = arr[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++)         prefixSum[i][0][0]             = prefixSum[i - 1][0][0] + arr[i][0][0];      for (int i = 1; i < R; i++)         prefixSum[0][i][0]             = prefixSum[0][i - 1][0] + arr[0][i][0];      for (int i = 1; i < C; i++)         prefixSum[0][0][i]             = prefixSum[0][0][i - 1] + arr[0][0][i];      // Step 2: Filling the cells     // of sides(made up using cells)     // which have common element arr[0][0][0].     // using prefix sum on 2d array     for (int k = 1; k < L; k++) {         for (int i = 1; i < R; i++) {             prefixSum[k][i][0]                 = arr[k][i][0] + prefixSum[k - 1][i][0]                   + prefixSum[k][i - 1][0]                   - prefixSum[k - 1][i - 1][0];         }     }     for (int i = 1; i < R; i++) {         for (int j = 1; j < C; j++) {             prefixSum[0][i][j]                 = arr[0][i][j] + prefixSum[0][i - 1][j]                   + prefixSum[0][i][j - 1]                   - prefixSum[0][i - 1][j - 1];         }     }     for (int j = 1; j < C; j++) {         for (int k = 1; k < L; k++) {             prefixSum[k][0][j]                 = arr[k][0][j] + prefixSum[k - 1][0][j]                   + prefixSum[k][0][j - 1]                   - prefixSum[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++) {                 prefixSum[k][i][j]                     = arr[k][i][j]                        + prefixSum[k - 1][i][j]                       + prefixSum[k][i - 1][j]                       + prefixSum[k][i][j - 1]                        - prefixSum[k - 1][i - 1][j]                       - prefixSum[k][i - 1][j - 1]                       - prefixSum[k - 1][i][j - 1]                        + prefixSum[k - 1][i - 1][j - 1];             }         }     } }  static int calculateSum(     int [][][] arr,     int [][][] prefixSum,     int D, int E, int F, int X, int Y, int Z) {      // store prefix sum up to arr[X][Y][Z]:     int sum = prefixSum[X][Y][Z];      // Remove prefix sum till D, E, F.     if (D > 0) {         sum -= prefixSum[D - 1][Y][Z];     }     if (E > 0) {         sum -= prefixSum[X][E - 1][Z];     }     if (F > 0) {         sum -= prefixSum[X][Y][F - 1];     }      // Add to compensate cells removed multiple times.     if (D > 0 && E > 0) {         sum += prefixSum[D - 1][E - 1][Z];     }     if (E > 0 && F > 0) {         sum += prefixSum[X][E - 1][F - 1];     }     if (F > 0 && D > 0) {         sum += prefixSum[D - 1][Y][F - 1];     }      // Removing cells added twice in the above step.     if (D > 0 && E > 0 && F > 0) {         sum -= prefixSum[D - 1][E - 1][F - 1];     }      return sum; }  // Driver Code public static void main(String[] args) {        // Given 3D array:     int [][][] arr = { { { 1, 1, 1, 1 },               { 1, 1, 1, 1 },               { 1, 1, 1, 1 },               { 1, 1, 1, 1 } },              { { 1, 1, 1, 1 },               { 1, 1, 1, 1 },               { 1, 1, 1, 1 },               { 1, 1, 1, 1 } },              { { 1, 1, 1, 1 },               { 1, 1, 1, 1 },               { 1, 1, 1, 1 },               { 1, 1, 1, 1 } },              { { 1, 1, 1, 1 },               { 1, 1, 1, 1 },               { 1, 1, 1, 1 },               { 1, 1, 1, 1 } } };      // To store the prefixSum     int [][][] prefixSum= new int[L][R][C];      // To calculate the prefixSum     prefixSum3d(arr, prefixSum);     int D, E, F, X, Y, Z;     D = 1;     E = 1;     F = 1;     X = 3;     Y = 3;     Z = 3;      System.out.print(calculateSum(arr, prefixSum, D, E, F, X, Y, Z)); } }  // This code is contributed by 29AjayKumar  
Python3
# Python3 program for the above approach.  # Declaring size of the array L = 4  # Layer R = 4  # Row C = 4  # Column  # Calculating prefix sum array def prefixSum3d(arr, prefixSum):          # Step 0:     prefixSum[0][0][0] = arr[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):         prefixSum[i][0][0] = prefixSum[i - 1][0][0] + arr[i][0][0]      for i in range(1, R):         prefixSum[0][i][0] = prefixSum[0][i - 1][0] + arr[0][i][0]      for i in range(1, C):         prefixSum[0][0][i] = prefixSum[0][0][i - 1] + arr[0][0][i]          # Step 2: Filling the cells         # of sides(made up using cells)         # which have common element arr[0][0][0].         # using prefix sum on 2d array     for k in range(1, L):         for i in range(1, R):             prefixSum[k][i][0] = arr[k][i][0] + prefixSum[k - 1][i][0] + \                 prefixSum[k][i - 1][0] - prefixSum[k - 1][i - 1][0]      for i in range(1, R):         for j in range(1, C):             prefixSum[0][i][j] = arr[0][i][j] + prefixSum[0][i - 1][j] + \                 prefixSum[0][i][j - 1] - prefixSum[0][i - 1][j - 1]      for j in range(1, C):         for k in range(1, L):             prefixSum[k][0][j] = arr[k][0][j] + prefixSum[k - 1][0][j] + \                 prefixSum[k][0][j - 1] - prefixSum[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):                 prefixSum[k][i][j] = arr[k][i][j] + prefixSum[k - 1][i][j] + prefixSum[k][i - 1][j] + prefixSum[k][i][j - 1] - \                     prefixSum[k - 1][i - 1][j] - prefixSum[k][i - 1][j - 1] - \                     prefixSum[k - 1][i][j - 1] + prefixSum[k - 1][i - 1][j - 1]   def calculateSum(arr, prefixSum, D, E, F, X, Y, Z):          # store prefix sum up to arr[X][Y][Z]:     sum = prefixSum[X][Y][Z]      # Remove prefix sum till D, E, F.     if (D > 0):         sum -= prefixSum[D - 1][Y][Z]      if (E > 0):         sum -= prefixSum[X][E - 1][Z]      if (F > 0):         sum -= prefixSum[X][Y][F - 1]      # Add to compensate cells removed multiple times.     if (D > 0 and E > 0):         sum += prefixSum[D - 1][E - 1][Z]      if (E > 0 and F > 0):         sum += prefixSum[X][E - 1][F - 1]      if (F > 0 and D > 0):         sum += prefixSum[D - 1][Y][F - 1]      # Removing cells added twice in the above step.     if (D > 0 and E > 0 and F > 0):         sum -= prefixSum[D - 1][E - 1][F - 1]      return sum   # Driver Code if __name__ == "__main__":      # Given 3D array:      arr = [[[1, 1, 1, 1],             [1, 1, 1, 1],             [1, 1, 1, 1],             [1, 1, 1, 1]],             [[1, 1, 1, 1],             [1, 1, 1, 1],             [1, 1, 1, 1],             [1, 1, 1, 1]],             [[1, 1, 1, 1],             [1, 1, 1, 1],             [1, 1, 1, 1],             [1, 1, 1, 1]],             [[1, 1, 1, 1],             [1, 1, 1, 1],             [1, 1, 1, 1],             [1, 1, 1, 1]]]      # To store the prefixSum     prefixSum = [[[0 for _ in range(C)] for _ in range(R)] for _ in range(L)]      # To calculate the prefixSum     prefixSum3d(arr, prefixSum)     D = 1     E = 1     F = 1     X = 3     Y = 3     Z = 3      print(calculateSum(arr, prefixSum, D, E, F, X, Y, Z))      # This code is contributed by rakeshsahni 
C#
// C# program for the above approach. using System;  public class GFG{  // Declaring size of the array static readonly int L = 4; // Layer static readonly int R = 4; // Row static readonly int C = 4; // Column  // Calculating prefix sum array static void prefixSum3d(     int [,,] arr,     int [,,] prefixSum) {     // Step 0:     prefixSum[0,0,0] = arr[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++)         prefixSum[i,0,0]             = prefixSum[i - 1,0,0] + arr[i,0,0];      for (int i = 1; i < R; i++)         prefixSum[0,i,0]             = prefixSum[0,i - 1,0] + arr[0,i,0];      for (int i = 1; i < C; i++)         prefixSum[0,0,i]             = prefixSum[0,0,i - 1] + arr[0,0,i];      // Step 2: Filling the cells     // of sides(made up using cells)     // which have common element arr[0,0,0].     // using prefix sum on 2d array     for (int k = 1; k < L; k++) {         for (int i = 1; i < R; i++) {             prefixSum[k,i,0]                 = arr[k,i,0] + prefixSum[k - 1,i,0]                   + prefixSum[k,i - 1,0]                   - prefixSum[k - 1,i - 1,0];         }     }     for (int i = 1; i < R; i++) {         for (int j = 1; j < C; j++) {             prefixSum[0,i,j]                 = arr[0,i,j] + prefixSum[0,i - 1,j]                   + prefixSum[0,i,j - 1]                   - prefixSum[0,i - 1,j - 1];         }     }     for (int j = 1; j < C; j++) {         for (int k = 1; k < L; k++) {             prefixSum[k,0,j]                 = arr[k,0,j] + prefixSum[k - 1,0,j]                   + prefixSum[k,0,j - 1]                   - prefixSum[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++) {                 prefixSum[k,i,j]                     = arr[k,i,j]                        + prefixSum[k - 1,i,j]                       + prefixSum[k,i - 1,j]                       + prefixSum[k,i,j - 1]                        - prefixSum[k - 1,i - 1,j]                       - prefixSum[k,i - 1,j - 1]                       - prefixSum[k - 1,i,j - 1]                        + prefixSum[k - 1,i - 1,j - 1];             }         }     } }  static int calculateSum(     int [,,] arr,     int [,,] prefixSum,     int D, int E, int F, int X, int Y, int Z) {      // store prefix sum up to arr[X,Y,Z]:     int sum = prefixSum[X,Y,Z];      // Remove prefix sum till D, E, F.     if (D > 0) {         sum -= prefixSum[D - 1,Y,Z];     }     if (E > 0) {         sum -= prefixSum[X,E - 1,Z];     }     if (F > 0) {         sum -= prefixSum[X,Y,F - 1];     }      // Add to compensate cells removed multiple times.     if (D > 0 && E > 0) {         sum += prefixSum[D - 1,E - 1,Z];     }     if (E > 0 && F > 0) {         sum += prefixSum[X,E - 1,F - 1];     }     if (F > 0 && D > 0) {         sum += prefixSum[D - 1,Y,F - 1];     }      // Removing cells added twice in the above step.     if (D > 0 && E > 0 && F > 0) {         sum -= prefixSum[D - 1,E - 1,F - 1];     }      return sum; }  // Driver Code public static void Main(String[] args) {        // Given 3D array:     int [,,] arr = { { { 1, 1, 1, 1 },               { 1, 1, 1, 1 },               { 1, 1, 1, 1 },               { 1, 1, 1, 1 } },              { { 1, 1, 1, 1 },               { 1, 1, 1, 1 },               { 1, 1, 1, 1 },               { 1, 1, 1, 1 } },              { { 1, 1, 1, 1 },               { 1, 1, 1, 1 },               { 1, 1, 1, 1 },               { 1, 1, 1, 1 } },              { { 1, 1, 1, 1 },               { 1, 1, 1, 1 },               { 1, 1, 1, 1 },               { 1, 1, 1, 1 } } };      // To store the prefixSum     int [,,] prefixSum= new int[L,R,C];      // To calculate the prefixSum     prefixSum3d(arr, prefixSum);     int D, E, F, X, Y, Z;     D = 1;     E = 1;     F = 1;     X = 3;     Y = 3;     Z = 3;      Console.Write(calculateSum(arr, prefixSum, D, E, F, X, Y, Z)); } }  // This code is contributed by 29AjayKumar  
JavaScript
<script> // Javascript program for the above approach.  // Declaring size of the array let L = 4; // Layer let R = 4; // Row let C = 4; // Column  // Calculating prefix sum array function prefixSum3d(arr, prefixSum) {   // Step 0:   prefixSum[0][0][0] = arr[0][0][0];    // Step 1: Filling the first row,   // column, and pile of ceils.   // Using prefix sum of 1d array   for (let i = 1; i < L; i++)     prefixSum[i][0][0] = prefixSum[i - 1][0][0] + arr[i][0][0];    for (let i = 1; i < R; i++)     prefixSum[0][i][0] = prefixSum[0][i - 1][0] + arr[0][i][0];    for (let i = 1; i < C; i++)     prefixSum[0][0][i] = prefixSum[0][0][i - 1] + arr[0][0][i];    // Step 2: Filling the cells   // of sides(made up using cells)   // which have common element arr[0][0][0].   // using prefix sum on 2d array   for (let k = 1; k < L; k++) {     for (let i = 1; i < R; i++) {       prefixSum[k][i][0] =         arr[k][i][0] +         prefixSum[k - 1][i][0] +         prefixSum[k][i - 1][0] -         prefixSum[k - 1][i - 1][0];     }   }   for (let i = 1; i < R; i++) {     for (let j = 1; j < C; j++) {       prefixSum[0][i][j] =         arr[0][i][j] +         prefixSum[0][i - 1][j] +         prefixSum[0][i][j - 1] -         prefixSum[0][i - 1][j - 1];     }   }   for (let j = 1; j < C; j++) {     for (let k = 1; k < L; k++) {       prefixSum[k][0][j] =         arr[k][0][j] +         prefixSum[k - 1][0][j] +         prefixSum[k][0][j - 1] -         prefixSum[k - 1][0][j - 1];     }   }    // Step 3: Filling value   // in remaining cells using formula   for (let k = 1; k < L; k++) {     for (let i = 1; i < R; i++) {       for (let j = 1; j < C; j++) {         prefixSum[k][i][j] =           arr[k][i][j] +           prefixSum[k - 1][i][j] +           prefixSum[k][i - 1][j] +           prefixSum[k][i][j - 1] -           prefixSum[k - 1][i - 1][j] -           prefixSum[k][i - 1][j - 1] -           prefixSum[k - 1][i][j - 1] +           prefixSum[k - 1][i - 1][j - 1];       }     }   } }  function calculateSum(arr, prefixSum, D, E, F, X, Y, Z) {   // store prefix sum up to arr[X][Y][Z]:   let sum = prefixSum[X][Y][Z];    // Remove prefix sum till D, E, F.   if (D > 0) {     sum -= prefixSum[D - 1][Y][Z];   }   if (E > 0) {     sum -= prefixSum[X][E - 1][Z];   }   if (F > 0) {     sum -= prefixSum[X][Y][F - 1];   }    // Add to compensate cells removed multiple times.   if (D > 0 && E > 0) {     sum += prefixSum[D - 1][E - 1][Z];   }   if (E > 0 && F > 0) {     sum += prefixSum[X][E - 1][F - 1];   }   if (F > 0 && D > 0) {     sum += prefixSum[D - 1][Y][F - 1];   }    // Removing cells added twice in the above step.   if (D > 0 && E > 0 && F > 0) {     sum -= prefixSum[D - 1][E - 1][F - 1];   }    return sum; }  // Driver Code // Given 3D array:  let arr = [   [     [1, 1, 1, 1],     [1, 1, 1, 1],     [1, 1, 1, 1],     [1, 1, 1, 1],   ],    [     [1, 1, 1, 1],     [1, 1, 1, 1],     [1, 1, 1, 1],     [1, 1, 1, 1],   ],    [     [1, 1, 1, 1],     [1, 1, 1, 1],     [1, 1, 1, 1],     [1, 1, 1, 1],   ],    [     [1, 1, 1, 1],     [1, 1, 1, 1],     [1, 1, 1, 1],     [1, 1, 1, 1],   ], ];  // To store the prefixSum let prefixSum = new Array(L)   .fill(0)   .map(() => new Array(R).fill(0).map(() => new Array(C).fill(0)));  // To calculate the prefixSum prefixSum3d(arr, prefixSum); let D, E, F, X, Y, Z; D = 1; E = 1; F = 1; X = 3; Y = 3; Z = 3;  document.write(calculateSum(arr, prefixSum, D, E, F, X, Y, Z));  // This code is contributed by gfgking. </script> 

 
 


Output
27


 

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


 


Next Article
Program to find sum of elements in a given 2D array

G

gajjardeep50
Improve
Article Tags :
  • Matrix
  • DSA
  • Arrays
  • array-range-queries
Practice Tags :
  • Arrays
  • Matrix

Similar Reads

  • Program to find sum of elements in a given 2D array
    Given a 2D array of order M * N, the task is to find out the sum of elements of the matrix. Examples: Input: array[2][2] = {{1, 2}, {3, 4}};Output: 10 Input: array[4][4] = {{1, 2, 3, 4}, {5, 6, 7, 8}, {9, 10, 11, 12}, {13, 14, 15, 16}};Output: 136 Approach: The sum of each element of the 2D array ca
    12 min read
  • Sum of all elements in an array between zeros
    Given an array arr[] of N integers, the task is to find the sum of all elements between two zeros in the given array. If possible, then print all the sum, else print "-1".Note: There is no continuous zero in the given array. Examples: Input: arr[] = { 1, 0, 3, 4, 0, 4, 4, 0, 2, 1, 4, 0, 3 } Output:
    7 min read
  • Sum of array elements excluding the elements which lie between a and b
    Given an array of N unique numbers. Also given two numbers a and b such that a will always be before b in the array. The task is to find the sum of the array elements excluding the elements which lie between a and b. Examples: Input : arr = [2, 1, 6, 9, 11], a = 6, b = 9 Output : 14 Input : arr = [1
    5 min read
  • Calculate the Manhattan Distance between two cells of given 2D array
    Given a 2D array of size M * N and two points in the form (X1, Y1) and (X2 , Y2) where X1 and X2 represents the rows and Y1 and Y2 represents the column. The task is to calculate the Manhattan distance between the given points. Examples: Input: M = 5, N = 5, X1 = 1, Y1 = 2, X2 = 3, Y2 = 3Output: 3Ex
    4 min read
  • Find sum of all unique elements in the array for K queries
    Given an arrays arr[] in which initially all elements are 0 and another array Q[][] containing K queries where every query represents a range [L, R], the task is to add 1 to each subarrays where each subarray is defined by the range [L, R], and return sum of all unique elements.Note: One-based index
    8 min read
  • Find the Surface area of a 3D figure
    Given a N*M matrix A[][] representing a 3D figure. The height of the building at [Tex](i, j) [/Tex]is [Tex]A[i][j] [/Tex]. Find the surface area of the figure. Examples : Input : N = 1, M = 1 A[][] = { {1} } Output : 6 Explanation : The total surface area is 6 i.e 6 side of the figure and each are o
    12 min read
  • Sum of elements of an AP in the given range
    Given an arithmetic series in arr and Q queries in the form of [L, R], where L is the left boundary of the range and R is the right boundary. The task is to find the sum of the AP elements in the given range.Note: The range is 1-indexed and 1 ? L, R ? N, where N is the size of arr. Examples: Input:
    10 min read
  • Find three closest elements from given three sorted arrays
    Given three sorted arrays A[], B[] and C[], find 3 elements i, j and k from A, B and C respectively such that max(abs(A[i] - B[j]), abs(B[j] - C[k]), abs(C[k] - A[i])) is minimized. Here abs() indicates absolute value. Example : Input : A[] = {1, 4, 10} B[] = {2, 15, 20} C[] = {10, 12} Output: 10 15
    15+ min read
  • Sum of special triplets having elements from 3 arrays
    Given three arrays A, B, and C, the task is to find sum of values of all special triplets. A special triplet is defined as a triplet (X, Y, Z) where the condition : X ? Y and Z ? Y always hold true. The value of each triplet (X, Y, Z) is given by: f(X, Y, Z) = (X + Y) * (Y + Z) Note: If a triplet is
    15+ min read
  • Find Matrix With Given Row and Column Sums
    Given two arrays rowSum[] and colSum[] of size n and m respectively, the task is to construct a matrix of dimensions n × m such that the sum of matrix elements in every ith row is rowSum[i] and the sum of matrix elements in every jth column is colSum[j]. Note: The resultant matrix can have only non-
    15 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