Repository: TheAlgorithms/Java
Pull request #4590 (2023-10-03 09:05:33)

Fet: add median of matrix
<!--
Thank you for your contribution!
In order to reduce the number of notifications sent to the maintainers, please:
- create your PR as draft, cf. https://docs.github.com/en/pull-requests/collaborating-with-pull-requests/proposing-changes-to-your-work-with-pull-requests/about-pull-requests#draft-pull-requests,
- make sure that all of the CI checks pass,
- mark your PR as ready for review, cf. https://docs.github.com/en/pull-requests/collaborating-with-pull-requests/proposing-changes-to-your-work-with-pull-requests/changing-the-stage-of-a-pull-request#marking-a-pull-request-as-ready-for-review
-->

<!-- For completed items, change [ ] to [x] -->

- [x] I have read [CONTRIBUTING.md](https://github.com/TheAlgorithms/Java/blob/master/CONTRIBUTING.md).
- [x] This pull request is all my own work -- I have not plagiarized it.
- [x] All filenames are in PascalCase.
- [x] All functions and variable names follow Java naming conventions.
- [x] All new algorithms have a URL in their comments that points to Wikipedia or other similar explanations.


BamaCharanChhandogi: @siriak check please!

Patch:
diff --git a/src/main/java/com/thealgorithms/misc/MedianOfMatrix.java b/src/main/java/com/thealgorithms/misc/MedianOfMatrix.java
new file mode 100644
index 00000000..4d8b980f
--- /dev/null
+++ b/src/main/java/com/thealgorithms/misc/MedianOfMatrix.java
@@ -0,0 +1,30 @@
+package com.thealgorithms.misc;
+
+import java.util.ArrayList;
+import java.util.Collections;
+import java.util.List;
+
+/**
+ * Median of Matrix (https://medium.com/@vaibhav.yadav8101/median-in-a-row-wise-sorted-matrix-901737f3e116)
+ * Author: Bama Charan Chhandogi (https://github.com/BamaCharanChhandogi)
+ */
+
+public final class MedianOfMatrix {
+
+    public static int median(List<List<Integer>> matrix) {
+        // Flatten the matrix into a 1D list
+        List<Integer> linear = new ArrayList<>();
+        for (List<Integer> row : matrix) {
+            linear.addAll(row);
+        }
+
+        // Sort the 1D list
+        Collections.sort(linear);
+
+        // Calculate the middle index
+        int mid = (0 + linear.size() - 1) / 2;
+
+        // Return the median
+        return linear.get(mid);
+    }
+}


Test patch:
diff --git a/src/test/java/com/thealgorithms/misc/MedianOfMatrixtest.java b/src/test/java/com/thealgorithms/misc/MedianOfMatrixtest.java
new file mode 100644
index 00000000..c1fa30d6
--- /dev/null
+++ b/src/test/java/com/thealgorithms/misc/MedianOfMatrixtest.java
@@ -0,0 +1,34 @@
+package com.thealgorithms.misc;
+
+import static org.junit.jupiter.api.Assertions.*;
+
+import java.util.ArrayList;
+import java.util.Arrays;
+import java.util.List;
+import org.junit.jupiter.api.Test;
+
+public class MedianOfMatrixtest {
+
+    @Test
+    public void testMedianWithOddNumberOfElements() {
+        List<List<Integer>> matrix = new ArrayList<>();
+        matrix.add(Arrays.asList(1, 3, 5));
+        matrix.add(Arrays.asList(2, 4, 6));
+        matrix.add(Arrays.asList(7, 8, 9));
+
+        int result = MedianOfMatrix.median(matrix);
+
+        assertEquals(5, result);
+    }
+
+    @Test
+    public void testMedianWithEvenNumberOfElements() {
+        List<List<Integer>> matrix = new ArrayList<>();
+        matrix.add(Arrays.asList(2, 4));
+        matrix.add(Arrays.asList(1, 3));
+
+        int result = MedianOfMatrix.median(matrix);
+
+        assertEquals(2, result);
+    }
+}