Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Scroll backward circular array

I have an array and I want to scroll the positions in it backward.

I have a boolean array and I need to count how many true are there from a given cell going forward and backward.

I know that if N is the number of elements into the array, I can go ahead doing i=(i+1)%N. But what if I need to scroll it in the opposite way?

Thank you in advance.

like image 419
Daniele Vitali Avatar asked Oct 13 '25 03:10

Daniele Vitali


1 Answers

I usually use i = (i - 1 + N) % N to prevent negative values.

like image 119
Vincent van der Weele Avatar answered Oct 14 '25 15:10

Vincent van der Weele