> For the complete documentation index, see [llms.txt](https://mayanktyagi3111.gitbook.io/interview-prep/llms.txt). Markdown versions of documentation pages are available by appending `.md` to page URLs; this page is available as [Markdown](https://mayanktyagi3111.gitbook.io/interview-prep/dynamic-programming/minimum-sum-path-in-3-d-array.md).

# Minimum Sum Path In 3-D Array

Given a 3-D array arr\[l]\[m]\[n], the task is to find the minimum path sum from the first cell of array to the last cell of array. We can only traverse to adjacent element, i.e., from a given cell (i, j, k), cells (i+1, j, k), (i, j+1, k) and (i, j, k+1) can be traversed, diagonal traversing is not allowed, We may assume that all costs are positive integers.

**Examples:**

```
Input : arr[][][]= { {{1, 2}, {3, 4}},
                     {{4, 8}, {5, 2}} };
Output : 9
Explanation : arr[0][0][0] + arr[0][0][1] + 
              arr[0][1][1] + arr[1][1][1]

Input : { { {1, 2}, {4, 3}},
          { {3, 4}, {2, 1}} };
Output : 7
Explanation : arr[0][0][0] + arr[0][0][1] + 
              arr[0][1][1] + arr[1][1][1]
```

```java
public class solution {

    public int minPathSum(int arr[][][]) {
        int l = arr.length, m = arr[0].length, n = arr[0][0].length;
        int dp[][][] = new int[l][m][n];
        // Base case
        dp[0][0][0] = arr[0][0][0];

        // Building row edge
        for (int i = 1; i < l; i++)
            dp[i][0][0] = dp[i - 1][0][0] + arr[i][0][0];

        // Building column edge
        for (int j = 1; j < m; j++)
            dp[0][j][0] = dp[0][j - 1][0] + arr[0][j][0];

        // Building height edge
        for (int k = 1; k < n; k++)
            dp[0][0][k] = dp[0][0][k - 1] + arr[0][0][k];

        // Building row-col area
        for (int i = 1; i < l; i++)
            for (int j = 1; j < m; j++)
                dp[i][j][0] = Math.min(dp[i - 1][j][0], dp[i][j - 1][0]) + arr[i][j][0];

        // Building row-height area
        for (int i = 1; i < l; i++)
            for (int k = 1; k < n; k++)
                dp[i][0][k] = Math.min(dp[i - 1][0][k], dp[i][0][k - 1]) + arr[i][0][k];

        // Building col-height area
        for (int k = 1; k < n; k++)
            for (int j = 1; j < m; j++)
                dp[0][j][k] = Math.min(dp[0][j][k - 1], dp[0][j - 1][k]) + arr[0][j][k];

        // Building rest of the 3d cube
        for (int i = 1; i < l; i++)
            for (int j = 1; j < m; j++)
                for (int k = 1; k < n; k++)
                    dp[i][j][k] = Math.min(dp[i - 1][j][k], Math.min(dp[i][j - 1][k], dp[i][j][k - 1])) + arr[i][j][k];

        return dp[l - 1][m - 1][n - 1];
    }
}
```


---

# Agent Instructions
This documentation is published with GitBook. GitBook is the documentation platform designed so that both humans and AI agents can read, navigate, and reason over technical content effectively. Learn more at gitbook.com.

## Querying This Documentation
If you need additional information that is not directly available in this page, you can query the documentation dynamically by asking a question.

Perform an HTTP GET request on the current page URL with the `ask` query parameter, and the optional `goal` query parameter:

```
GET https://mayanktyagi3111.gitbook.io/interview-prep/dynamic-programming/minimum-sum-path-in-3-d-array.md?ask=<question>&goal=<endgoal>
```

`ask` is the immediate question: it should be specific, self-contained, and written in natural language.
`goal` is optional and describes the broader end goal you are ultimately trying to accomplish on behalf of the user. GitBook uses it to tailor the answer towards what is most useful for that goal.

The response will contain a direct answer to the question and relevant excerpts and sources from the documentation.

Use this mechanism when the answer is not explicitly present in the current page, you need clarification or additional context, or you want to retrieve related documentation sections.
