Design a 3D Binary Matrix with Efficient Layer Tracking - Problem

You are given a n × n × n binary 3D array matrix. Implement the Matrix3D class:

Matrix3D(int n) Initializes the object with the 3D binary array matrix, where all elements are initially set to 0.

void setCell(int x, int y, int z) Sets the value at matrix[x][y][z] to 1.

void unsetCell(int x, int y, int z) Sets the value at matrix[x][y][z] to 0.

int largestMatrix() Returns the index x where matrix[x] contains the most number of 1's. If there are multiple such indices, return the largest x.

Input & Output

Example 1 — Basic Operations
$ Input: operations = [["Matrix3D", 2], ["setCell", 0, 0, 0], ["setCell", 1, 1, 1], ["largestMatrix"], ["setCell", 0, 1, 1], ["largestMatrix"]]
Output: [null, null, null, 1, null, 0]
💡 Note: Initialize 2×2×2 matrix. Set (0,0,0) - layer 0 has 1 cell. Set (1,1,1) - layer 1 has 1 cell. Both layers tie with count 1, return larger index 1. Set (0,1,1) - layer 0 now has 2 cells. Layer 0 has most cells, return 0.
Example 2 — With Unset Operations
$ Input: operations = [["Matrix3D", 3], ["setCell", 1, 0, 0], ["setCell", 1, 0, 1], ["setCell", 2, 1, 1], ["largestMatrix"], ["unsetCell", 1, 0, 0], ["largestMatrix"]]
Output: [null, null, null, null, 1, null, 2]
💡 Note: Initialize 3×3×3 matrix. Set cells in layers 1 (count=2) and 2 (count=1). Layer 1 has most, return 1. Unset one cell from layer 1 (count=1). Now layers 1 and 2 tie with count 1, return larger index 2.
Example 3 — All Layers Empty
$ Input: operations = [["Matrix3D", 2], ["largestMatrix"]]
Output: [null, 1]
💡 Note: All layers have count 0, return the largest index which is 1 (n-1).

Constraints

  • 1 ≤ n ≤ 100
  • 0 ≤ x, y, z < n
  • At most 104 calls will be made to setCell, unsetCell, and largestMatrix

Visualization

Tap to expand
3D Binary Matrix with Layer Tracking INPUT n x n x n Binary 3D Matrix (n=2) Layer x=0: 0 0 0 0 Layer x=1: 0 0 0 0 Operations: 1. Matrix3D(2) 2. setCell(0, 0, 0) 3. setCell(1, 1, 1) 4. largestMatrix() 5. setCell(0, 1, 1) 6. largestMatrix() Count 1s in each layer Return largest index ALGORITHM STEPS 1 Initialize Create layerCount[n] array All counts = 0 2 setCell(x,y,z) If cell=0: set to 1 layerCount[x]++ 3 unsetCell(x,y,z) If cell=1: set to 0 layerCount[x]-- 4 largestMatrix() Find max in layerCount Return largest index Layer Count Tracking: Operation x=0 x=1 set(0,0,0) 1 0 set(1,1,1) 1 1 set(0,1,1) 2 1 FINAL RESULT Final Matrix State: x=0 (count=2) 1 0 0 1 x=1 (count=1) 0 0 0 1 Query Results: After op 4: largestMatrix() counts: [1,1], tie --> return 1 (largest index with max) After op 6: largestMatrix() counts: [2,1], max at 0 --> return 0 Output: [null,null,null,1,null,0] OK - All operations complete! Key Insight: Instead of counting 1s in each layer on every query (O(n^2)), maintain a layerCount array. Update count on setCell/unsetCell: O(1). Query largestMatrix scans layerCount: O(n). For ties, iterate from largest index to smallest, returning first max found. Time: O(n) per query. TutorialsPoint - Design a 3D Binary Matrix with Efficient Layer Tracking | Layer Count Tracking
Asked in
Google 25 Amazon 18 Microsoft 15 Meta 12
23.4K Views
Medium Frequency
~25 min Avg. Time
892 Likes
Ln 1, Col 1
Smart Actions
💡 Explanation
AI Ready
💡 Suggestion Tab to accept Esc to dismiss
// Output will appear here after running code
Code Editor Closed
Click the red button to reopen