Q:
Given a symmetric array which contains either 1 or 0 as elements.If a row one in a column,all next elements in that row will be ones's
ex: [0,0,0,1,1,1]
[0,1,1,1,1,1,1]
Find the maximum number of ones which occured in a row
Given a symmetric array which contains either 1 or 0 as elements.If a row one in a column,all next elements in that row will be ones's
ex: [0,0,0,1,1,1]
[0,1,1,1,1,1,1]
Find the maximum number of ones which occured in a row
public class MaxNumber {
public static void main(String[] args) {
int[][] a = { { 0, 0, 0, 0, 0, 1 },
{ 0, 0, 0, 0, 1, 1 },
{ 0, 0, 1, 1, 1, 1 },
{ 0, 0, 0, 1, 1, 1 },
{ 0, 0, 0, 1, 1, 1 },
{ 0, 1, 1, 1, 1, 1 }
};
getMaxNumber(a);
}
public static void getMaxNumber(int[][] a) {
int length = a.length;
int max = length - 1;
int i = length - 1;
int j = length - 1;
while (i >= 0 && j >= 0) {
if (a[i][j] == 1)
j--;
else
i--;
}
int l = max - j;
System.out.println("maximum number of ones are : " + l);
}
}
No comments:
Post a Comment