Skip to content

Commit 4288a5e

Browse files
authored
Create 1: Row Wise Sum
1 parent 95dc4d1 commit 4288a5e

File tree

1 file changed

+62
-0
lines changed

1 file changed

+62
-0
lines changed
Lines changed: 62 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,62 @@
1+
/*
2+
"Row Wise Sum"
3+
4+
For a given two-dimensional integer array/list of size (N x M), find and print the sum of each of the row elements in a single line, separated by a single space.
5+
Input Format :
6+
The first line contains an Integer 't' which denotes the number of test cases or queries to be run. Then the test cases follow.
7+
8+
First line of each test case or query contains two integer values, 'N' and 'M', separated by a single space. They represent the 'rows' and 'columns' respectively, for the two-dimensional array/list.
9+
10+
Second line onwards, the next 'N' lines or rows represent the ith row values.
11+
12+
Each of the ith row constitutes 'M' column values separated by a single space.
13+
Output Format :
14+
For each test case, print the sum of every ith row elements in a single line separated by a single space.
15+
16+
Output for every test case will be printed in a seperate line.
17+
Constraints :
18+
1 <= t <= 10^2
19+
0 <= N <= 10^3
20+
0 <= M <= 10^3
21+
Time Limit: 1sec
22+
Sample Input 1:
23+
1
24+
4 2
25+
1 2
26+
3 4
27+
5 6
28+
7 8
29+
Sample Output 1:
30+
3 7 11 15
31+
Sample Input 2:
32+
2
33+
2 5
34+
4 5 3 2 6
35+
7 5 3 8 9
36+
4 4
37+
1 2 3 4
38+
9 8 7 6
39+
3 4 5 6
40+
-1 1 -10 5
41+
Sample Output 2:
42+
20 32
43+
10 30 18 -5
44+
45+
*/
46+
47+
48+
49+
public class Solution {
50+
51+
public static void rowWiseSum(int[][] mat) {
52+
for(int i=0;i<mat.length;i++){
53+
int sum=0;
54+
for(int j=0;j<mat[i].length;j++){
55+
sum=sum+mat[i][j];
56+
}
57+
System.out.print(sum+" ");
58+
}
59+
60+
}
61+
62+
}

0 commit comments

Comments
 (0)