Suppose I have something like this:
int[][] arr = new int[22][];
for (int i = 0; i < 22; ++i)
{
arr[i] = new int[22];
}
for (int x = 0; x < 22; ++x)
{
for (int y = 0; y < 22; ++y)
{
arr[x][y] = ...;
}
}
I'm trying to flatten arr, but still being able to use x and y separately as indexes.
I was able to achieve this by creating a new 1d array with a size of ((21 << 5) | 21) + 1, because 21 uses 5 bits, and you can combine them like that.
You can then convert x and y into a single array index by doing: (x << 5) | y, and this works fine.
However, the issue is this is space inefficient. There are only 22 * 22 elements, but you have to create an array with size ((21 << 5) | 21) + 1. That is 484 vs 694 elements.
Is there any way to flatten arr but still being able to use x and y separately as indexes (and being space-efficient)?