Code360 powered by Coding Ninjas X Naukri.com. Code360 powered by Coding Ninjas X Naukri.com

# Game Of Life

Moderate
0/80
Average time to solve is 15m
Contributed by

## Problem statement

You’re given a board of N * M size. Each element of this board is represented by either 0 or 1, where 0 means ‘dead’ and 1 means ‘live’. Each element can interact with all of its eight neighbors using the following four rules.

``````If any live cell has less than two live neighbors, then it dies.

If any live cell has two or three live neighbors, then it lives onto the next generation.

If any live cell has more than three live neighbors, then it dies.

A dead element with exactly three live neighbors becomes a live element.

Your task is to print the next state of this board using these four rules.
``````
Detailed explanation ( Input/output format, Notes, Images )
Constraints :
``````1 <= T <= 5
1 <= M, N <= 1000

Time limit: 1 sec
``````
##### Sample Input 1 :
``````1
2 2
1 0
0 1
``````
##### Sample Output 1 :
`````` 0 0
0 0
``````
##### Explanation For Sample Input 1 :

``````The first element(0,0) and the fourth element(1,1) are ‘live’ and have one live and two dead neighbors. Hence they both will die and become 0 in the next state. The second element(0,1) and the third element(1,0) are ‘dead’ and have one dead and two live neighbors. Hence they will also die and become 0 in the next state.
``````
##### Sample Input 2 :
``````1
3 3
0 0 1
0 0 0
1 0 1
``````
##### Sample Output 2 :
``````0 0 0
0 1 0
0 0 0
``````
Console