package leetcode;

/**
 * https://leetcode.com/problems/find-kth-largest-xor-coordinate-value/
 */
public class Problem1738 {
    public int kthLargestValue(int[][] matrix, int k) {
        int maxRow = matrix.length;
        int maxCol = maxRow > 0 ? matrix[0].length : 0;
        int xor1 = 0;
        for (int row = 0; row < maxRow; row++) {
            int xor2 = 0;
            for (int col = 0; col < maxCol; col++) {
                if (col == 0) {
                    xor2 = matrix[row][col];
                } else {
                    xor2 ^= matrix[row][col];
                }
            }
            if (row == 0) {
                xor1 = xor2;
            } else {
                xor1 ^= xor2;
            }
        }
        xor1 = 0;
        for (int col = 0; col < maxCol; col++) {
            int xor2 = 0;
            for (int row = 0; row < maxRow; row++) {
                if (col == 0) {
                    xor2 = matrix[row][col];
                } else {
                    xor2 ^= matrix[row][col];
                }
            }
            if (col == 0) {
                xor1 = xor2;
            } else {
                xor1 ^= xor2;
            }
        }
        return 0;
    }

    public static void main(String[] args) {
        Problem1738 prob = new Problem1738();
        System.out.println(prob.kthLargestValue(new int[][]{
            {5,2},{1,6}
        }, 1)); // 7
        System.out.println(prob.kthLargestValue(new int[][]{
            {5,2},{1,6}
        }, 2)); // 5
        System.out.println(prob.kthLargestValue(new int[][]{
            {5,2},{1,6}
        }, 3)); // 4
        System.out.println(prob.kthLargestValue(new int[][]{
            {5,2},{1,6}
        }, 4)); // 0
    }
}

@@ -12,16 +12,98 @@
 tcode;%0A%0A
+import java.util.ArrayList;%0Aimport java.util.Collections;%0Aimport java.util.List;%0A%0A
 /**%0A * h
@@ -361,20 +361,87 @@
     
-int xor1 = 0
+List%3CInteger%3E list = new ArrayList%3C%3E();%0A        int%5B%5D xorCols = new int%5BmaxCol%5D
 ;%0A  
@@ -498,33 +498,32 @@
          int xor
-2
  = 0;%0A          
@@ -612,33 +612,32 @@
              xor
-2
  = matrix%5Brow%5D%5Bc
@@ -681,33 +681,32 @@
              xor
-2
  %5E= matrix%5Brow%5D%5B
@@ -733,38 +733,32 @@
   %7D%0A        
-    %7D%0A
         
     if (row 
@@ -741,28 +741,24 @@
             
-    
 if (row == 0
@@ -781,383 +781,105 @@
     
-xor1 = xor2;%0A            %7D else %7B%0A                xor1 %5E= xor2;%0A            %7D%0A        %7D%0A        xor1 = 0;%0A        for (int col = 0; col %3C maxCol; col++) %7B%0A            int xor2 = 0;%0A            for (int row = 0; row %3C maxRow; row++) %7B%0A                if (col == 0) %7B%0A                    xor2 = matrix%5Brow%5D%5Bcol%5D;%0A                %7D else %7B%0A                   
+    xorCols%5Bcol%5D = xor;%0A                %7D else %7B%0A                    xorCols%5Bcol%5D =
  xor
-2
  %5E
-= matrix%5Brow%5D
+ xorCols
 %5Bcol
@@ -915,469 +915,124 @@
     
-%7D%0A            if (col == 0) %7B%0A                xor1 = xor2;%0A            %7D else %7B%0A                xor1 %5E= xor2;%0A            %7D%0A        %7D%0A        return 0;%0A    %7D%0A%0A    public static void main(String%5B%5D args) %7B%0A        Problem1738 prob = new Problem1738();%0A        System.out.println(prob.kthLargestValue(new int%5B%5D%5B%5D%7B%0A            %7B5,2%7D,%7B1,6%7D%0A        %7D, 1)); // 7%0A        System.out.println(prob.kthLargestValue(new int%5B%5D%5B%5D%7B%0A            %7B5,2%7D,%7B1,6%7D%0A        %7D, 2
+    list.add(xorCols%5Bcol%5D);%0A            %7D%0A        %7D%0A        Collections.sort(list, (a, b) -%3E Integer.compare(b, a
 ));
- // 5
 %0A   
@@ -1040,211 +1040,31 @@
     
-System.out.println(prob.kthLargestValue(new int%5B%5D%5B%5D%7B%0A            %7B5,2%7D,%7B1,6%7D%0A        %7D, 3)); // 4%0A        System.out.println(prob.kthLargestValue(new int%5B%5D%5B%5D%7B%0A            %7B5,2%7D,%7B1,6%7D%0A        %7D, 4)); // 0
+return list.get(k - 1);
 %0A   

Solve problem 1738