Reputation: 33
I need to write a java code that get a 2D array as a matrix and check every rows and columns and find the number that its the max at the rows but the min at the cols.
For example, the 13 is the min at col and max at the row:
I wrote the code but I get lost:
import java.util.Scanner;
public class maxmin {
public static Scanner input = new Scanner(System.in);
public static void main(String[] args) {
System.out.println("enter matrix size ");
int num = input.nextInt();
int num1 = input.nextInt();
maxMin(num, num1);
}
private static void maxMin(int num, int num1) {
int max = 0;
int min = 0;
int[][] matrix = new int[num][num1];
System.out.println("ENTER ARRAY NUMBERS");
for (int i = 0; i < matrix.length; i++) {
for (int j = 0; j < matrix[0].length; j++) {
matrix[i][j] = input.nextInt();
}
}
for (int i = 0; i < matrix.length; i++) {
for (int j = 0; j < matrix[0].length; j++) {
if (matrix[i][j] >= max) {
max = matrix[i][j];
for (int a = 0; a < matrix.length; a++) {
if (matrix[i][a] <= min) {
min = matrix[i][a];
}
if (max == min) {
System.out.println(max);
}
}
}
}
}
}
}
Upvotes: 3
Views: 2500
Reputation:
You can use IntStream
for this purpose:
int[][] arr = {
{1, 2, 3, 6, 5},
{3, 2, 5, 6, 7},
{5, 6, 7, 8, 9},
{1, 3, 0, 2, 4}};
int num = Arrays
// iterate over the nested arrays,
// i.e. rows of the 2d array
.stream(arr)
// find maximum value of the row
// return IntStream of maximums
.mapToInt(row -> Arrays.stream(row)
// maximum value of the row
.max().orElse(Integer.MIN_VALUE))
// find minimum value of row maximums
.min().orElse(Integer.MIN_VALUE);
// output
System.out.println(num); // 4
Upvotes: 2
Reputation: 69
So first thing. Since you are trying to find a number that is MAX at row and MIN at column, it means you are trying to find multiple numbers. As such, there should be some resetting code for resetting the max and min in order to find new max and min.
Then, what you need to think about is HOW you are gonna find it. Since it must be max in the column first, you have to iterate all positions in each row first while recording the max position. Then you iterate that particular column associated with the max position in that row. You get it? You ONLY iterate the column again (beside the first time) when you finish an entire row first and know the position.
@Alex Rudenko notes that since there are negative numbers in the matrix, an initial value of max and min should default to
int max = Integer.MIN_VALUE;
int min = Integer.MAX_VALUE;
which is essential for the below code to work.
So the code for the last block should look something like this:
for (int i = 0; i < matrix.length; i++){
for (int j = 0; j < matrix[0].length; j++){
if (matrix[i][j] > max){
max = matrix[i][j];
maxPos = j; //declare this beforehand
}
}
for (int a = 0; a < matrix.length; a++ ){
if( matrix[a][maxPos] < min){
min = matrix[a][maxPos];
}
}
if(max == min) {
System.out.println(matrix[i][maxPos]);
max = Integer.MIN_VALUE;
min = Integer.MAX_VALUE;
}
}
That’s it! There could be something down to avoid repetition on already checked position, but this is the central logic of this operation.
Also, I assume you are only printing the max&min number. You can also save those numbers in an array or something if you need to use it later.
Since you are trying to learn, I would suggest refrain from using advanced methods. This is a helpful basic to solidify you coding muscle.
Upvotes: -1