Given A Square Matrix
Given A Square Matrix
123
456
989
The left-to-right diagonal = . The right to left diagonal = . Their absolute difference is .
Function description
Return
Input Format
The first line contains a single integer, , the number of rows and columns in the square matrix .
Constraints
Output Format
Return the absolute difference between the sums of the matrix's two diagonals as a single integer.
Sample Input
3
11 2 4
456
10 8 -12
Sample Output
15
Explanation
11
5
-12
Sum across the primary diagonal: 11 + 5 - 12 = 4
4
5
10
Sum across the secondary diagonal: 4 + 5 + 10 = 19
Difference: |4 - 19| = 15
import java.io.*;
import java.math.*;
import java.security.*;
import java.text.*;
import java.util.*;
import java.util.concurrent.*;
import java.util.regex.*;
class Result {
/*
* Complete the 'diagonalDifference' function below.
*
* The function is expected to return an INTEGER.
* The function accepts 2D_INTEGER_ARRAY arr as parameter.
*/
public static int diagonalDifference(List<List<Integer>> arr) {
// Write your code here
}
}
public class Solution {
public static void main(String[] args) throws IOException {
BufferedReader bufferedReader = new BufferedReader(new InputStrea
mReader(System.in));
BufferedWriter bufferedWriter = new BufferedWriter(new FileWriter
(System.getenv("OUTPUT_PATH")));
int n = Integer.parseInt(bufferedReader.readLine().trim());
List<List<Integer>> arr = new ArrayList<>();
for (int i = 0; i < n; i++) {
String[] arrRowTempItems = bufferedReader.readLine().replaceA
ll("\\s+$", "").split(" ");
List<Integer> arrRowItems = new ArrayList<>();
for (int j = 0; j < n; j++) {
int arrItem = Integer.parseInt(arrRowTempItems[j]);
arrRowItems.add(arrItem);
}
arr.add(arrRowItems);
}
int result = Result.diagonalDifference(arr);
bufferedWriter.write(String.valueOf(result));
bufferedWriter.newLine();
bufferedReader.close();
bufferedWriter.close();
}
}
Input (stdin)
Download
3
11 2 4
456
10 8 -12
Expected Output
Download
15
Given an array of integers, calculate the ratios of its elements that are positive, negative, and zero. Print the
decimal value of each fraction on a new line with places after the decimal.
Note: This challenge introduces precision problems. The test cases are scaled to six decimal places, though
Example
There are elements, two positive, two negative and one zero. Their ratios are , and . Results are printed as:
0.400000
0.400000
0.200000
Function Description
Print the ratios of positive, negative and zero values in the array. Each value should be printed on a separate
line with digits after the decimal. The function should not return a value.
Input Format
Constraints
Output Format
Print the following lines, each to decimals:
3. proportion of zeros
Sample Input
6
-4 3 -9 0 4 1
Sample Output
0.500000
0.333333
0.166667
Explanation
import java.io.*;
import java.math.*;
import java.security.*;
import java.text.*;
import java.util.*;
import java.util.concurrent.*;
import java.util.regex.*;
public class Solution {
// Complete the plusMinus function below.
static void plusMinus(int[] arr) {
}
private static final Scanner scanner = new Scanner(System.in);
public static void main(String[] args) {
int n = scanner.nextInt();
scanner.skip("(\r\n|[\n\r\u2028\u2029\u0085])?");
int[] arr = new int[n];
String[] arrItems = scanner.nextLine().split(" ");
scanner.skip("(\r\n|[\n\r\u2028\u2029\u0085])?");
for (int i = 0; i < n; i++) {
int arrItem = Integer.parseInt(arrItems[i]);
arr[i] = arrItem;
}
plusMinus(arr);
scanner.close();
}
}