Largest Row or Column
If there doesn't exist a sum at all then print "row 0 -2147483648", where -2147483648 or -2^31 is the smallest value for the range of Integer.
1
2 2
1 1
1 1
row 0 2
import java.util.Scanner;
public class LargestRowOrCol {
public static void main(String[] args) {
Scanner s = new Scanner(System.in);
int n = s.nextInt();
int m = s.nextInt();
int input[][] = new int[n][m];
for(int i = 0; i < n; i++)
{
for(int j = 0; j < m; j++)
{
input[i][j] = s.nextInt();
}
}
findLargest(input);
}
public static void findLargest(int mat[][]){
int maxR=Integer.MIN_VALUE;
int maxIndexR=Integer.MIN_VALUE;
int maxIndexC=Integer.MIN_VALUE;
int maxC=Integer.MIN_VALUE;
//int l=mat.length;
if(mat.length==0)
{
System.out.println("row 0 "+ Integer.MIN_VALUE);
}
for(int i=0;i<mat.length;i++)
{
int sumR=0;
for(int j=0;j<mat[i].length;j++)
{
sumR=sumR+mat[i][j];
}
if(sumR>maxR)
{
maxR=sumR;
maxIndexR=i;
}
}
for(int i=0;i<mat[0].length;i++)
{
int sumC=0;
for(int j=0;j<mat.length;j++)
{
sumC=sumC+mat[j][i];
}
if(sumC>maxC)
{
maxC=sumC;
maxIndexC=i;
}
}
if(maxR>=maxC)
{
System.out.println("row "+ maxIndexR + " "+ maxR);
}
else
{
System.out.println("column "+ maxIndexC + " "+ maxC);
}
}
}
Comments
Post a Comment