Accenture Coding Question | Maximum Sum of Column and Row | CodeWindow

Maximum Sum

Write a program that adds up the largest row sum and the largest column sum from an N-rows *M-columns array numbers.

Input specification:
Input1: Integer for row dimension of the array.
Input2: Integer for column dimension of the array.
Input3: Array elements to be entered in row major.

Output Specification:
Largest row sum + Largest column sum

Example 1:
Input 1: 2
Input 2: 2
Input 3: {1,2,5,6}

Output: 19
Explanation:
Largest row(5,6) + Largest Column(2,6) = 11 + 8 =19

Example 2:
Input 1: 3
Input 2: 3
Input 3: {1,2,3,4,5,6,7,8,9}

Output: 42
Explanation:
Largest row(7,8,9) + Largest Column(3,6,9) = 24 + 18 = 42

Solution in JAVA 3:

public class CodeWindow {
public static void main(String[] args) {
int input1=3;
int input2=3;
int [] input3={3,6,9,1,4,7,2,8,9};
int ans= max( input1,input2,input3);
System.out.println(ans);
}

private static int max(int input1, int input2, int[] input3) {
int rows = input1;
int cols = input2;
int sumRow, sumCol;
int [] [] a= new int[input1][input2];
int count=0;

for(int i=0;i<input1;i++)

{
for(int j=0;j<input2;j++)
{
if(count==input3.length) break;
a[i][j]=input3[count];
count++;
}
}

int maxRow = Integer.MIN_VALUE;
for(int i = 0; i < rows; i++){
sumRow = 0;
for(int j = 0; j < cols; j++){
sumRow = sumRow + a[i][j];
}
maxRow=Math.max(maxRow,sumRow);
}

int maxCol=Integer.MIN_VALUE;

for(int i = 0; i < cols; i++){
sumCol = 0;
for(int j = 0; j < rows; j++){
sumCol = sumCol + a[j][i];
}
maxCol=Math.max(maxCol,sumCol);
}

return maxCol+maxRow;
}

}

Contributed by: Akshay Pokharkar

Recent Posts

Leave a Comment

Your email address will not be published. Required fields are marked *