Advertisement
Georgi_Benchev

Untitled

Oct 12th, 2024
64
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Java 2.66 KB | None | 0 0
  1. package Telerik_Alpha_Java_2024.CodingTasks_3;
  2.  
  3. import java.util.Arrays;
  4. import java.util.Scanner;
  5.  
  6. public class Task3_MatrixMaxSum {
  7.     public static void main(String[] args) {
  8.         Scanner scanner = new Scanner(System.in);
  9.  
  10.         int n = Integer.parseInt(scanner.nextLine());
  11.         int[][] matrix = new int[n][];
  12.         //filling the matrix
  13.         fillingMatrix(n, matrix, scanner);
  14.         // getting the coordinates input
  15.         String[] coordinates = scanner.nextLine().split(" ");
  16.         // setting bestSum to int MIN so we can find the biggest number if we have only negative numbers
  17.         int bestSum = Integer.MIN_VALUE;
  18.  
  19.         //  we work with 2 values from Array "commands" so the loop length is commands.length and "i" is not "++" but "+=2"
  20.         for (int i = 0; i < coordinates.length; i += 2) {
  21.             //  "-1" converts the input number from "commands" and matches it to the targeted "matrix" index number
  22.             int rowTarget = Math.abs(Integer.parseInt(coordinates[i])) - 1;
  23.             int colTarget = Math.abs(Integer.parseInt(coordinates[i + 1])) - 1;
  24.             // sum is inside the "commands" loop and is "= 0" in every loop
  25.             int sum = 0;
  26.             // horizontal logic
  27.             if (coordinates[i].charAt(0) == '-') {   // case "-"
  28.                 for (int col = matrix[rowTarget].length - 1; col >= colTarget; col--) {
  29.                     sum += matrix[rowTarget][col];
  30.                 }
  31.             } else {        // case "+"
  32.                 for (int col = 0; col <= colTarget; col++) {
  33.                     sum += matrix[rowTarget][col];
  34.                 }
  35.             }
  36.  
  37.             // vertical logic
  38.             if (coordinates[i + 1].charAt(0) == '-') {   // case "-"
  39.                 for (int row = rowTarget + 1; row <= n - 1; row++) {
  40.                     sum += matrix[row][colTarget];
  41.                 }
  42.             } else {        //case +
  43.                 for (int row = 0; row < rowTarget; row++) {
  44.                     sum += matrix[row][colTarget];
  45.                 }
  46.             }
  47.             // updating the bestSum
  48.             if (sum >= bestSum) {
  49.                 bestSum = sum;
  50.             }
  51.         }
  52.         System.out.println(bestSum);
  53.     }
  54.  
  55.     private static void fillingMatrix(int n, int[][] matrix, Scanner scanner) {
  56.         for (int i = 0; i < n; i++) {
  57.             matrix[i] = Arrays.stream(scanner.nextLine().split(" ")).mapToInt(Integer::parseInt).toArray();
  58.         }
  59.     }
  60. }
  61. /*  best test cases for checking all 4 corners "1" "6" "9" "4"  --> output should be 9
  62. 6
  63. 1 2 3 4 5 6
  64. 2 3 4 5 6 7
  65. 6 5 4 3 2 1
  66. 3 4 5 6 7 8
  67. 4 5 6 7 8 9
  68. 9 8 7 6 5 4
  69. -6 -6 -1 6 1 1 6 -1
  70. */
  71.  
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement