One-dimensional circular Conway's Game of Life I have a program that is working, but it takes way too long to get the answer. With input like

15 100000000
111010111001100


The program checks for 15 numbers, and checks if two number beside the numbers in the second row adds up to exactly 1. If it adds up to 1, then the number would stay/change to 1, otherwise it change/stay 0. And this need to happen for 100000000 times (it changes each time). Right now I can get the right answer, but it takes like 1 minute to get it.

import java.util.*;
public class circle {
final Scanner in;
public circle()
{
   in=new Scanner(System.in);
   run();
   in.close();
}
public void run()
{
   String input=in.nextLine();
   //I created two arrays, checks for one array while change the other one so the changed number wouldn't affect other checking
   final int[] list=createarray(input);
   final int totalcells=list[0];
   final int generation=list[1];
   String input1=in.nextLine();
   long[] deadalive=createarray2(input1,totalcells);
   long[] changing=new long[totalcells];
   System.arraycopy(deadalive,0,changing,0,totalcells);
   //It checks for y times and checks all the cells
   for(int y=0;y&lt;generation;y++)
   {
       for(int x=0;x&lt;totalcells;x++)
       {
           if(x==0)
           {
               if(deadalive[x+1]+deadalive[totalcells-1]==1)
               {
                changing[x]=1;  
               }
               else
                   changing[x]=0;

           }
           else if(x==totalcells-1)
           {

                   if(deadalive[x-1]+deadalive[0]==1)
                   {
                       changing[x]=1;
                   }
                   else
                       changing[x]=0;
           }
           else
           {
                   if(deadalive[x-1]+deadalive[x+1]==1)
                   {
                       changing[x]=1;
                   }
                   else
                       changing[x]=0;   
           }
       }
       //make two arrays the same after each changes
       System. arraycopy(changing, 0, dead alive, 0, changing.length);

   }
   System.out.println(java.util.Arrays.toString(changing));

}
private static int[] createarray(String input)
{
   final String[] args=input.trim().split("\\s+");
    final int aray[]=new int[2];
    for(int i=0;i&lt;2;i++)
    {
        aray[i]=Integer.parseInt(args[i]);
    }
    return aray;
}
private static long[] createarray2(String input1,int totalcells)
{
   final long array[]=new long[totalcells];
    for(int i=0;i&lt;input1.length();i++)
    {
        array[i]=Character.digit(input1. charAt(i),10);
    }
    return array;
}
public static void main(String args[])
{
   new circle();
}
}


This code is supposed to change the second row of numbers like input:

7 1
0000001


Output would be:

1000010


0 changes to 1 if the two "neighbor" adds up to 1 exactly. other wise they are 0, but this one only runs for one time unit, where there are ones that needs to run 10000000 time units. For loop probably isn't efficient enough to do it in a short time period, so I thought maybe there are better ways to do it.
Here is an idea to speed things up, given that the input is only 15 numbers:


Since the numbers are binary, you could represent the board as a bitmask in an integer instead of an array (see wikipedia/masks).
There are only \$2^{15}\$ possible states. You could create a Map&lt;Integer, Integer&gt; from each state bitmask to the next. Then each iteration in your algorithm is gonna be a simple map lookup.


There are more optimizations you could to make it even faster, but this should be enough.