Jump to content

Do you mean cardinality? I don't understand the question.

To store a 2D array in a 1D array, simply make a 1D array with size rows × columns, and take each row starting with the first and insert it into the 1D array. Now if you want to access array2D[i][j], convert the index of this cell to the corresponding index in the 1D array.

If you want to do this with a 3D array, you can visualize it as several layers each layer contains a single 2D array. Start with the first layer, do the previous steps, then the second layer, and so on.

Given the size of a N-Dimensional array and an index, you are asked to convert this index to the corresponding index in a 1D array.

Link to post
Share on other sites

 

To store a 2D array in a 1D array, simply make a 1D array with size rows × columns, and take each row starting with the first and insert it into the 1D array. Now if you want to access array2D[i][j], convert the index of this cell to the corresponding index in the 1D array.

If you want to do this with a 3D array, you can visualize it as several layers each layer contains a single 2D array. Start with the first layer, do the previous steps, then the second layer, and so on.

Given the size of a N-Dimensional array and an index, you are asked to convert this index to the corresponding index in a 1D array.

 

The first line of the input will be a single integer T, the number of test cases (1  T  50). Followed by the test cases, each test case starts with an integer N the number of dimensions of the array (1  N  15). Followed by two lines, each contains N numbers. The first line represents the sizes of the dimensions (1  size[i]  15). The second line represents the index that you are asked to convert (0  index[i]  <  size[i]).

Link to post
Share on other sites

2d: index = (y * width) + x

3d: index = (((z * height) + y) * width) + x

 

edit:

3d can also be written as

(z * height * width) + (y * width) + x

so for higher order N you just keep adding another dimension at the front making 4th dimension

(w * 3rdDim * 2ndDim * 1stDim) + (z * 2ndDim * 1stDim) + (y * 1stDim) + x

1474412270.2748842

Link to post
Share on other sites

2d: index = (y * width) + x

3d: index = (((z * height) + y) * width) + x

 

edit:

3d can also be written as

(z * height * width) + (y * width) + x

so for higher order N you just keep adding another dimension at the front making 4th dimension

(w * 3rdDim * 2ndDim * 1stDim) + (z * 2ndDim * 1stDim) + (y * 1stDim) + x

Can you explain it with numbers please ?

Link to post
Share on other sites

Create an account or sign in to comment

You need to be a member in order to leave a comment

Create an account

Sign up for a new account in our community. It's easy!

Register a new account

Sign in

Already have an account? Sign in here.

Sign In Now

×