No idea why this does not work!


#1

Scanner scan = new Scanner(System.in);
String[] arr = scan.nextLine().trim().split(" “);
int m = Integer.valueOf(arr[0]);
int n = Integer.valueOf(arr[1]);
int[][] mn = new int[m][n];
for (int i=0; i<m; i++) {
String[] row = scan.nextLine().trim().split(” ");
int[] op = Arrays.stream(row).mapToInt(x -> Integer.valueOf(x)).toArray();
mn[i]=op;
}
for (int j=0; j<n; j++) {
int sum = 0;
for (int i=0; i<m; i++) {
sum += mn[i][j];
}
System.out.print(j == 0 ? sum : " "+sum);
}

Also, cant seem to test with custom input, nothing gets printed except compile (green tick)


#2

Try :
System.out.print(sum+" ");

//Your solution is perfect but here the test cases are designed as this format
sum1sum2…sumM

Hope it helps :slight_smile:


#3

No need to make a 2D array

import java.lang.*;
import java.util.*;

public class Main {
    public static void main(String[] args) {
        // YOUR CODE GOES HERE
        // Please take input and print output to standard input/output (stdin/stdout)
        // DO NOT USE ARGUMENTS FOR INPUTS
        // E.g. 'Scanner' for input & 'System.out' for output
        Scanner sc = new Scanner(System.in);
        int n = sc.nextInt();
        int m = sc.nextInt();
        sc.nextLine();
        int[] arr = new int[m];
        for (int i = 0; i < n; ++i) {
            for (int j = 0; j < m; ++j)
                arr[j] += sc.nextInt();
            sc.nextLine();
        }
        sc.close();
        for (int num : arr)
            System.out.print(num + " ");
    }
}

#4

Java

import java.lang.;
import java.util.
;

public class Main {
public static void main(String[] args) {
Scanner inp = new Scanner(System.in);
int n,m;
n = inp.nextInt();
m = inp.nextInt();
inp.nextLine();
int[][] arr = new int[n][m];
for(int i = 0; i < n; i++)
{
for(int j = 0; j < m; j++)
{
arr[i][j] = inp.nextInt();
}
inp.nextLine();
}
inp.close();

    int[] colSum = new int[m];
    for(int i = 0; i < n; i++)
    {
        for(int j = 0; j < m; j++)
        {
            colSum[j] += arr[i][j];
        }
    }
    for(int i = 0; i < m; i++)
        System.out.print(colSum[i] + " ");
    System.out.println();
}

}
this is working good please check it once