β Alternate Matrix Addition(Contest)
Alternate Matrix Addition medium Time Limit: 2 sec Memory Limit: 128000 kB
import java.io.*; // for handling input/output
import java.util.*; // contains Collections framework
// don't change the name of this class
// you can add inner classes if needed
class Main {
public static void main (String[] args)throws IOException {
// Your code here
BufferedReader br = new BufferedReader(new InputStreamReader(System.in));
int n = Integer.parseInt(br.readLine());
int arr[][] = new int[n][n];
for(int i=0;i<n;i++){
String []input = br.readLine().split(" ");
for(int j=0;j<n;j++){
arr[i][j] = Integer.parseInt(input[j]);
}
}
matSum(arr,n);
}
static void matSum(int arr[][], int n){
long sum = 0,sum1 = 0;
for(int i=0;i<n;i++){
for(int j=0;j<n;j++){
if((i+j)%2==0){
sum += arr[i][j];
}else{
sum1 += arr[i][j];
}
}
}
System.out.println(sum);
System.out.println(sum1);
}
}
//method 02
```java
import java.io.*; // for handling input/output
import java.util.*; // contains Collections framework
// don't change the name of this class
// you can add inner classes if needed
class Main {
public static void main (String[] args) {
// Your code here
Scanner in = new Scanner(System.in);
int n = in.nextInt();
int arr[][] = new int [n][n];
for(int i=0; i<n; i++){
for(int j=0; j<n; j++){
arr[i][j] = in.nextInt();
}
}
int blackSum = 0;
int whiteSum = 0;
for(int i=0; i<n; i++){
for(int j=0; j<n; j++){
if((i+j)%2 ==0){
blackSum += arr[i][j];
}else{
whiteSum += arr[i][j];
}
}
}
System.out.println(blackSum);
System.out.println(whiteSum);
}
}
```Last updated