Join Regular Classroom : Visit ClassroomTech

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 Python:

m=int(input())
n=int(input())
l=[int(x) for x in input().split()]

maxr=0
i=0
while(i<len(l)):
    rowsum=0
    for j in range(i,i+n):
        rowsum+=l[j]
    if(maxr<rowsum):
        maxr=rowsum
    i+=n
maxc=0  
i=0
while(i<n):
    colsum=0
    for j in range(i,len(l),n):
        colsum+=l[j]
    if(maxc<colsum):
        maxc=colsum
    i+=1
print(maxr+maxc)

Contributed by: Rajonya Mondal

Solution in JAVA 3:

//https://codewindow.in
//join our telegram channel @codewindow

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

Solution in C++ :

//https://codewindow.in
//join our telegram channel @codewindow

#include<iostream>
using namespace std;

int main()
{
    int N, M;
    cin>>N;
    cin>>M;
    int Matrix[N][M];
    for(int i = 0; i < N; i++)
        for(int j = 0; j < M; j++)
            cin>>Matrix[i][j];

    int Row = 0;
    for(int i = 0; i < N; i++)
    {
        int sum = 0;
        for(int j = 0; j < M; j++)
            sum = sum + Matrix[i][j];
        if(Row < sum)
            Row = sum;
    }
    int Column = 0;
    for(int i = 0; i < M; i++)
    {
        int sum = 0;
        for(int j = 0; j < N; j++)
            sum = sum + Matrix[j][i];
        if(Column < sum)
            Column = sum;
    }
    cout<<Row+Column;
    return 0;
}

Solution in C :

//https://codewindow.in
//join our telegram channel @codewindow

#include<stdio.h>

int main()
{
    int N, M;
    scanf("%d",&N);
    scanf("%d",&M);
    int Matrix[N][M];
    for(int i = 0; i < N; i++)
        for(int j = 0; j < M; j++)
            scanf("%d",&Matrix[i][j]);

    int Row = 0;
    for(int i = 0; i < N; i++)
    {
        int sum = 0;
        for(int j = 0; j < M; j++)
            sum = sum + Matrix[i][j];
        if(Row < sum)
            Row = sum;
    }
    int Column = 0;
    for(int i = 0; i < M; i++)
    {
        int sum = 0;
        for(int j = 0; j < N; j++)
            sum = sum + Matrix[j][i];
        if(Column < sum)
            Column = sum;
    }
    printf("%d",Row+Column);
    return 0;
}
Recent Posts
Pages