Advertisement
rajeshinternshala

Untitled

Nov 30th, 2023
69
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Java 1.58 KB | None | 0 0
  1. public class MaxNetVulnerability {
  2.     static int getMaxNetVulnerability(int[][] vulnerability) {
  3.         int n = vulnerability.length;
  4.         int m = vulnerability[0].length;
  5.  
  6.         int maxNetVulnerability = Integer.MIN_VALUE;
  7.  
  8.         for (int row1 = 0; row1 < n; row1++) {
  9.             for (int row2 = row1 + 1; row2 < n; row2++) {
  10.                 int[][] subsetGrid = new int[2][m];
  11.  
  12.                 subsetGrid[0] = vulnerability[row1];
  13.                 subsetGrid[1] = vulnerability[row2];
  14.  
  15.                 int[] maxInColumns = new int[m];
  16.  
  17.                 for (int col = 0; col < m; col++) {
  18.                     int max = Integer.MIN_VALUE;
  19.                     for (int row = 0; row < 2; row++) {
  20.                         max = Math.max(max, subsetGrid[row][col]);
  21.                     }
  22.                     maxInColumns[col] = max;
  23.                 }
  24.  
  25.                 int minMaxInColumns = Integer.MAX_VALUE;
  26.                 for (int max : maxInColumns) {
  27.                     minMaxInColumns = Math.min(minMaxInColumns, max);
  28.                 }
  29.  
  30.                 maxNetVulnerability = Math.max(maxNetVulnerability, minMaxInColumns);
  31.             }
  32.         }
  33.  
  34.         return maxNetVulnerability;
  35.     }
  36.  
  37.     public static void main(String[] args) {
  38.         int[][] vulnerability = {
  39.                 {1, 3, 1},
  40.                 {3, 1, 1},
  41.                 {1, 2, 2},
  42.                 {1, 1, 3}
  43.         };
  44.         int maxVulnerability = getMaxNetVulnerability(vulnerability);
  45.         System.out.println("Maximum possible net vulnerability: " + maxVulnerability);
  46.     }
  47. }
  48.  
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement