Refactor files to be in correctly nested packages (#6120)

This commit is contained in:
varada610
2025-01-10 23:17:40 -08:00
committed by GitHub
parent a9633c0000
commit 1e6ed97fcf
13 changed files with 75 additions and 77 deletions

View File

@@ -0,0 +1,32 @@
package com.thealgorithms.matrix;
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 {
private MedianOfMatrix() {
}
public static int median(Iterable<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);
}
}