Created
February 18, 2021 13:06
-
-
Save sanya-3-chitkara/3fcbba4d740d6a739948135512f96706 to your computer and use it in GitHub Desktop.
2dArrays/coding ninjas
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
import java.util.Scanner; | |
public class LargestRowOrColumn { | |
public static void findLargest(int mat[][]){ | |
//Your code goes here | |
int min=-2147483648; | |
boolean Row=true; | |
int largestsum=min; | |
int num=0; | |
int rows=mat.length; | |
if(rows==0){ | |
System.out.println("row 0 "+min); | |
return; | |
} | |
for(int i=0;i<rows;i++){ | |
int cols=mat[0].length; | |
int rowsum=0; | |
for(int j=0;j<cols;j++){ | |
rowsum+=mat[i][j]; | |
} | |
if(rowsum>largestsum){ | |
largestsum=rowsum; | |
num=i; | |
} | |
} | |
int cols=mat[0].length; | |
for(int j=0;j<cols;j++){ | |
int colsum=0; | |
for(int i=0;i<rows;i++){ | |
colsum+=mat[i][j]; | |
} | |
if(colsum>largestsum){ | |
largestsum=colsum; | |
num=j; | |
Row=false; | |
} | |
} | |
if(Row==true){ | |
System.out.println("row "+num+" "+ largestsum); | |
} | |
else{ | |
System.out.println("column "+num+" "+ largestsum); | |
} | |
} | |
public static void main(String[] args) { | |
// TODO Auto-generated method stub | |
Scanner s=new Scanner(System.in); | |
int n=s.nextInt(); | |
int rows=s.nextInt(); | |
int cols=s.nextInt(); | |
int[][] arr=new int[rows][cols]; | |
for(int i=0;i<rows;i++) { | |
for(int j=0;j<cols;j++) { | |
arr[i][j]=s.nextInt(); | |
} | |
} | |
findLargest(arr); | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment