Largest Row or Column in Matrix

`Given an NxM 2D array, you need to find out which row or column has largest sum (sum of its elements) overall amongst all rows and columns.

Input Format : Line 1 : 2 integers N and M respectively, separated by space

Line 2: Single line having N*M elements entered in row wise manner, each separated by space.

Output Format : If row sum is maximum then - "row" row_num max_sum If column sum is maximum then - "column" col_num max_sum

Note : If there are more than one rows/columns with maximum sum consider the row/column that comes first. And if ith row and jth column has same sum (which is largest), consider the ith row as answer.

My code below

public static void findLargest(int input[][]){
  int m=input.length;
  int n=input[0].length;
  int max = Integer.MIN_VALUE;
  int q = 0;
  int sum=0;
  String s = "";
 // int a[]=new int [m+n];
  //for row sum
  for(int i=0; i<n; i++)
  {
    sum=0;
    for(int j=0; j<m; j++)
    {
      sum =sum +input[i][j];

    }
    if(sum>max){
      max = sum;
      q = i;
      s = "row";
    }
    }
  // for col
  for(int i=0; i<m; i++)
  {
    sum=0;
    for(int j=0; j<n; j++)
    {
      sum=sum+input[j][i];

    }
  if(sum>max){
    max = sum;
    q = i;
    s = "column";
  } 
  }
  System.out.println(s + " "  + q + " " + max);
}

Upvotes: 1

Views: 15056

Answers (5)

Yash Batra
Yash Batra

Reputation: 1

# LARGEST COLUMN SUM IN A 2-D ARRAY
def lar_Col_Sum1(li):
    n = len(li) 
    m = len(li[0]) 
    max_sum = -1  
    max_sum = -1 
    for j in range(m):
        sum = 0
        for ele in li:
            sum += ele[j]
        if sum>max_sum:
            max_sum = sum
            max_index = j

    max_rindex = -1
    max_rsum = -1
    for i in range(n):
        sum = 0
        for j in range(m):
            sum += li[i][j]
        if sum > max_rsum:
            max_rsum = sum
            max_rindex = i
    if max_sum> max_rsum:
        print('column', max_index, max_sum)
    else:
        print('row', max_rindex, max_rsum)



li = [[6,9,8,5],[9,2,4,1],[8,3,9,3],[8,7,8,6]]
lar_Col_Sum1(li)

Upvotes: 0

Aman Varshney
Aman Varshney

Reputation: 1

This code will work for every case, rather it be a square matrix or not -->
( The code has been written in 

public static void rowOrColumn(int[][] input) {
        int rows = input.length;
        int columns = input[0].length;
        int largest = Integer.MIN_VALUE;
        int x = 0;
        int sum1 = 0;
        int sum2 = 0;
        String s = "";
        for(int i=0;i<rows;i++) {
            sum1 = 0;
            int j = 0;
            for(;j<columns;j++) {
                sum1 += input[i][j];
            }
            if(sum1>largest) {
                largest = sum1;
                x = i;
                s = "row";
            }
        }
        for(int i=0;i<columns;i++) {
            sum2 = 0;
            int j = 0;
            for(;j<rows;j++) {
                sum2 += input[j][i];
            }
            if(sum2>largest) {
                largest = sum2;
                x = i;
                s = "column";
            }
        }
    System.out.println(s + " " + x + " " + largest);
    }

    public static int[][] takeInput(){
        Scanner sc = new Scanner(System.in);
        int rows = sc.nextInt();
        int columns = sc.nextInt();
        int[][] arr = new int[rows][columns];
        for(int i=0;i<rows;i++) {
            for(int j=0;j<columns;j++) {
                arr[i][j] = sc.nextInt();
            }
        }
        return arr;
    }

    public static void main(String[] args) {
        int[][] arr = takeInput();
        rowOrColumn(arr);
    }

Upvotes: 0

Finally I code for the same and assuming N*M Matrix elements are separated by space and its output be largest sum among rows and columns and print the respective row or column.

enter code here


public static void findLargest(int input[][]){
  int m=input.length;
  int n=input[0].length;
  int max = Integer.MIN_VALUE;
  int q = 0;
  String s = "";
  for(int i=0; i<m; i++)
  {
    int sum=0;
    for(int j=0; j<n; j++)
    {
      sum =sum +input[i][j];

    }
    if(sum>max){
      max = sum;
      q = i;
      s = "row";
    }
  }
  for(int i=0; i<n; i++)
  {
   int sum=0;
    for(int j=0; j<m; j++)
    {
      sum=sum +input[j][i];

    }
  if(sum>max){
    max = sum;
    q = i;
    s = "column";
  } 
  }
  System.out.println(s + " "  + q + " " + max);
}

Upvotes: 0

joachimwedin
joachimwedin

Reputation: 1564

Assuming you have the array data as a string where the elements are separated by spaces and you have saved the dimensions N,M as integers the following code should solve your problem.

private int findMax(String s, int N, int M) {
    return findMaxSum(s.split(" "), N, M);
}

private int findMaxSum(String[] data, int N, int M) {
    int max = 0;

    for (int i = 0; i < M; i++) {
        max = Math.max(columnSum(data, i, N, M), max);
        max = Math.max(rowSum(data, i, N, M), max);
    }

    return max;
}

private int columnSum(String[] data, int columnIndex, int N, int M) {
    int sum = 0;
    for (int i = 0; i < M; i++) {
        sum += Integer.parseInt(data[i * N + columnIndex]);
    }
    return sum;
}

private int rowSum(String[] data, int rowIndex, int N, int M) {
    int sum = 0;
    System.out.println("row");
    for (int i = 0; i < N; i++) {
        sum += Integer.parseInt(data[rowIndex * N + i]);
        System.out.println(data[rowIndex * N + i]);
    }
    return sum;
}

Upvotes: 0

NiVeR
NiVeR

Reputation: 9806

You have almost gotten the solution, the only problem is that you are not resetting the sum1 and sum2 before calculating the sum of each row and column respectively. You should do it right before the second loop in each case.

Upvotes: 0

Related Questions