i am trying to access an array elements and make some Logic gates operations over it, so basically i am trying to get the neighbors of each element, but i am getting the error, Index was outside the bounds of the array error.. thats one of my conditional if statements, and i know i am trying to access an element which is not exist either less than 0 element position or bigger than or = array.length
public static int[] cellCompete(int[] states, int days)
{
int counter = 0;
// INSERT YOUR CODE HERE
while (counter <= days)
{
for (int i = 0; i < states.Length; i++)
{
if(i < states.Length - 1 || i > states[0])
{
if (states[0] == 0 && states[i + 1] == 0)
{
states[i - 1] = 0;
}
if (states[0] == 0 && states[i + 1] == 1)
{
states[i - 1] = 1;
}
if (states[i] == states.Length - 1 && states[i - 1] == 0)
{
states[i] = 0;
}
if (states[i] == states.Length - 1 && states[i - 1] == 1)
{
states[i] = 1;
}
if (states[i + 1] == 0 && states[i - 1] == 0)
{
states[i] = 0;
}
if (states[i + 1] == 0 && states[i - 1] == 1)
{
states[i] = 1;
}
if (states[i + 1] == 1 && states[i - 1] == 0)
{
states[i] = 1;
}
if (states[i + 1] == 1 && states[i - 1] == 1)
{
states[i] = 0;
}
}
}
counter++;
}
return states;
}
any idea how to fix it?