-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathLecture23.java
134 lines (97 loc) · 2.96 KB
/
Lecture23.java
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
import java.util.Scanner;
public class Lecture23 {
public static boolean isPresent(int[][] arr, int target){
for(int i = 0;i<3;i++){
for(int j = 0;j<4;j++){
if(arr[i][j] == target){
return true;
}
}
}
return false;
}
public static void printRowSum(int[][] arr){
int sum = 0;
for(int i = 0;i<3;i++){
for(int j = 0;j<4;j++){
sum = sum + arr[i][j];
}
System.out.println("Sum of " + i + "th row is " + sum);
sum = 0;
}
}
public static void printColSum(int[][] arr){
int sum = 0;
for(int i = 0;i<4;i++){
for(int j = 0;j<3;j++){
sum = sum + arr[j][i];
}
System.out.println("Sum of " + i + "th Coloumn is " + sum);
sum = 0;
}
}
public static int largestRowSum(int[][] arr){
int max = Integer.MIN_VALUE;
int rowIndex = -1;
int sum = 0;
for(int i = 0;i<3;i++){
for(int j = 0;j<4;j++){
sum = sum + arr[i][j];
}
if(sum > max){
max = sum;
rowIndex = i;
}
sum = 0;
}
System.out.println("Maximum Sum is: " + max);
return rowIndex;
}
public static void main(String[] args){
Scanner sc = new Scanner(System.in);
int[][] arr = new int[3][4];
System.out.println("Enter 12 elements in the array");
// Row wise input -->
for(int i = 0;i<3;i++){
for(int j = 0;j<4;j++){
arr[i][j] = sc.nextInt();
}
}
// Coloumn wise input -->
// for(int i = 0;i<4;i++){
// for(int j = 0;j<3;j++){
// arr[j][i] = sc.nextInt();
// }
// }
// int[][] arr = {
// {1,2,3,4},
// {5,6,7,8},
// {9,10,11,12}
// };
// int[][] arr = {
// {1,11,111,1111},
// {2,22,222,2222},
// {3,33,333,3333},
// };
// Print -->
for(int i = 0;i<3;i++){
for(int j = 0;j<4;j++){
System.out.print(arr[i][j] + " ");
}
System.out.println();
}
// System.out.println("Enter an element to search");
// int target = sc.nextInt();
// if(isPresent(arr, target)){
// System.out.println("Element found");
// }
// else{
// System.out.println("Not Found");
// }
// printRowSum(arr);
// printColSum(arr);
int ans = largestRowSum(arr);
System.out.println("Largest Sum Row is: " + ans);
sc.close();
}
}