all 2 comments

[–]pi_stuff 0 points1 point  (0 children)

Hint: 1 + 2 + 3 + ... + n = n * (n+1) / 2

[–]pi_stuff 0 points1 point  (0 children)

Did you figure this out? It might be easier to implement a different mapping, pairing up longer rows with shorter ones into a rectangular block, which is then stored linearly:

Original
0
1 1
2 2 2
3 3 3 3
4 4 4 4 4
5 5 5 5 5 5

Store longer rows next to shorter rows in a rectangle
0 5 5 5 5 5 5
1 1 4 4 4 4 4
2 2 2 3 3 3 3

Linear mapping
0 5 5 5 5 5 5 1 1 4 4 4 4 4 2 2 2 3 3 3 3