## ADP Interview Question for abcs

- 0of 0 votes

AnswersWrite a java program:

- sarath.chalasani46 April 26, 2016 in India

There is plant species (say P) in a desert that has a specific living pattern. A rectangular

field of the land is divided into squara cell.The land will have R rows and C columns of the

cell.Each cell will be surrounded by up-to eight neighboring cells(note that the cells at the

edge of the field will have fewer then eight nuighboring cells). A cell can either be empty

(having no plant in it) or living(having a plant in it).A particular cell is said to have N

neighbors(1<N<8), if exactly N cells in the neighborhood have living plants.

the plants fallow a certain rules for survival.the rules are as fallows.

1) if a cell with plant s1 to s2 neighbors (1<=s1<=s2<=8), it survives to the next generation.

i.e. the plant dies if it has less than s1 neighbors or more than s2 neighbors.

2) If an empty cell has B1 to B2 neidhbors (1<=B1<=B2<=8), a new plant takes birth in that cell

in the next generation.

Input Specification:

theinput will have two arrays, as fallows

1)An integer array: {R,C,S1,S2,B1,B2,G}

where R and C specify size of the field in row and columns respectively, with 1<=R<=2000 and

1<=C<=2000 s1,s2,B1 and B2 are integers as explained in the survival rules. G is the number of

generations for wich the simulation has to be run.

2)An integer array specifying intial state of the field: size of the array will be R*c

the data will be expressed in the array as fallows:{E11,E12,E1c,E21,....,E2C,ER1,ERC} where Eij

is cell in the field at ith row and jth column. Eij can have value 0 or 1. 0 corresponds to

empty cell and 1 corresponds to living cell.

output specification:

output will be an integer array specifying state of the field at the end of the simulation in

the same manner as in input.

Examples:

Example 1:

input1: {3,4,2,3,3,3,3}

input2: {0,1,0,0,0,1,1,0,1,0,1,0}

output : {0,1,1,1,0,0,0,1,0,0,1,0}

Example 2:

input1: {3,4,2,3,3,3,4}

input2: {0,1,0,0,0,1,1,0,1,0,1,0}

output: {0,0,1,1,0,1,0,1,0,0,0,0}

Method Signature:

public static int[] survivalcells( int[] input1,int input2){

}| Report Duplicate | Flag | PURGE

ADP abc

**Country:**India

**Interview Type:**Written Test

**CareerCup**is the world's biggest and best source for software engineering interview preparation. See all our resources.

Open Chat in New Window

import java.util.Arrays;

- Anonymous May 10, 2016public class plant{

public static int[] survivalcells(int[] input1, int[] input2) {

int[] resultMatrix = null;

if (input1 == null || input1.length == 0) {

return input2;

}

int rows = input1[0];

int cols = input1[1];

int s1 = input1[2];

int s2 = input1[3];

int b1 = input1[4];

int b2 = input1[5];

int nbGen = input1[6];

if (rows <= 0 || cols <= 0) {

return input2;

}

int[][] newMatrix = new int[rows][cols];

for (int i = 0, k = 0; i < rows; i++) {

for (int j = 0; j < cols; j++) {

newMatrix[i][j] = input2[k++];

}

}

try {

if (rows >= 1 && rows <= 2000 && cols >= 1 && cols <= 2000) {

for (int gen = 0; gen < nbGen; gen++) {

resultMatrix = cellsSurvived(newMatrix, s1, s2, b1, b2);

}

}

} catch (ArrayIndexOutOfBoundsException e) {

}

return resultMatrix;

}

public static int[] cellsSurvived(int[][] cellMatrix, int s1, int s2,

int b1, int b2) {

if (cellMatrix == null || cellMatrix.length == 0) {

return null;

}

int m = cellMatrix.length, n = cellMatrix[0].length;

for (int i = 0; i < m; i++) {

for (int j = 0; j < n; j++) {

int lives = calcLiveNeighbours(cellMatrix, m, n, i, j);

if (cellMatrix[i][j] == 1 && lives >= s1 && lives <= s2) {

cellMatrix[i][j] = 3;

}

if (cellMatrix[i][j] == 0 && lives >= b1 && lives <= b2) {

cellMatrix[i][j] = 2;

}

}

}

for (int i = 0; i < m; i++) {

for (int j = 0; j < n; j++) {

cellMatrix[i][j] >>= 1;

}

}

int[] survivalCellsResult = new int[cellMatrix.length

* cellMatrix[0].length];

for (int i = 0; i < cellMatrix.length; i++) {

int[] row = cellMatrix[i];

for (int j = 0; j < row.length; j++) {

int number = cellMatrix[i][j];

survivalCellsResult[i * row.length + j] = number;

}

}

return survivalCellsResult;

}

public static int calcLiveNeighbours(int[][] plantMatrix, int rows,

int cols, int rIndex, int cIndex) {

int liveCellCount = 0;

for (int x = Math.max(rIndex - 1, 0); x <= Math.min(rIndex + 1,

rows - 1); x++) {

for (int y = Math.max(cIndex - 1, 0); y <= Math.min(cIndex + 1,

cols - 1); y++) {

liveCellCount += plantMatrix[x][y] & 1;

}

}

liveCellCount -= plantMatrix[rIndex][cIndex] & 1;

return liveCellCount;

}