Problem Statement
We use the following pseudocode to visit each cell of an N x N x N cube exactly once:
for sum = 0 .. 3*N-3:
for x = 0 .. N-1:
for y = 0 .. N-1:
for z = 0 .. N-1:
if x+y+z == sum:
visit (x,y,z)
You are given the int N and the long index: a 0-based index into the sequence of cells visited by the above pseudocode. Return a int[] containing the coordinates of that cell.
Definition
- Class:
- IterateOverACube
- Method:
- findCell
- Parameters:
- int, long
- Returns:
- int[]
- Method signature:
- int[] findCell(int N, long index)
- (be sure your method is public)
Constraints
- N will be between 1 and 10^6, inclusive.
- index will be between 0 and N^3 - 1, inclusive.
Examples
3
9
Returns: {2, 0, 0 }The first ten cells visited are (0,0,0), (0,0,1), (0,1,0), (1,0,0), (0,0,2), (0,1,1), (0,2,0), (1,0,1), (1,1,0), and (2,0,0).3
10
Returns: {0, 1, 2 }The next visited cell is (0,1,2).4747
106968940722
Returns: {4746, 4746, 4746 }The very last cell visited in this cube.4
32
Returns: {0, 2, 3 }