anand.n
BAN USERpackage com.algorithms;
public class MaxPathBinaryArray {
public static void main(String[] args){
int[][] a = {
{ 0, 0, 1, 1 },
{ 1, 1 ,1, 0 },
{ 1, 0, 1 ,0 },
{ 0, 1, 0, 1 }
};
int ROWS = 4;
int COLS = 4;
boolean sequence = false;
int currMax = 0;
int max = 0;
for ( int i = 0; i < ROWS; i++){
currMax = 0;
sequence = false;
for (int j = 0; j < COLS; j++){
if ( a[i][j] == 1 && !sequence){
currMax = 0;
if ( i+1 < ROWS ){
if ( a[i+1][j] == 1){
currMax++;
}
}
sequence = true;
}else if ( a[i][j] == 1 && sequence){
currMax++;
if ( i+1 < ROWS){
if ( a[i+1][j] == 1){
currMax++;
}
}
sequence = true;
}else{
sequence = false;
if ( currMax != 0){
if ( max < currMax){
max = currMax;
}
}
}
}
if ( currMax != 0){
if ( max < currMax){
max = currMax;
}
}
}
System.out.println("Max Path is : "+max);
}
}
package com.algorithms;
public class MatrixMaxMinRowCol {
public static void main(String[] args){
int[][] a = {
{9,2,3,18},
{4,5,6,23},
{1,8,9,5}
};
int ROWS = 3;
int COLS = 4;
for ( int i = 0; i < ROWS; i++){
int biggest = a[i][0];
int smallest = a[0][i];
int bigPos = 0;
int smallPos = 0;
for ( int j = 0; j < COLS; j++){
if ( a[i][j] > biggest){
biggest = a[i][j];
bigPos = j;
}
if ( j < ROWS ){
if ( a[j][i] < smallest ){
smallest = a[j][i];
smallPos = j;
}
}
}
System.out.println("Biggest in row "+(i+1)+" is :"+biggest+" Pos is : "+bigPos);
System.out.println("Smallest in col "+(i+1)+" is :"+smallest+" Pos is :"+smallPos);
}
for ( int i = ROWS; i < COLS; i++){
int smallest = a[0][i];
int smallPos = 0;
for ( int j = 0; j < ROWS; j++){
if ( a[j][i] < smallest ){
smallest = a[j][i];
smallPos = j;
}
}
System.out.println("Smallest in col "+(i+1)+" is : "+smallest+" Pos is : "+smallPos);
}
}
}
Output:
Biggest in row 1 is :18 Pos is : 3
Smallest in col 1 is :1 Pos is :2
Biggest in row 2 is :23 Pos is : 3
Smallest in col 2 is :2 Pos is :0
Biggest in row 3 is :9 Pos is : 2
Smallest in col 3 is :3 Pos is :0
Smallest in col 4 is : 5 Pos is : 2
}}
- anand.n May 05, 2013