Home > Back-end >  How to find Lowest and Highest values and their coordinates in a Matrix that can contain Duplicates
How to find Lowest and Highest values and their coordinates in a Matrix that can contain Duplicates

Time:04-11

Problem statement:

Write a 3 x 6 matrix program, calculate and show:

  • The largest element of the matrix and its respective position;
  • The smallest element of the matrix and its respective position.

For example, the lowest number 1 repeats 3 times:

  • located in row 1 and column 1;
  • row 2 and column 1;
  • and row 3 and column 1.

But how can I find and print all tree values?

enter image description here

Code:

import java.util.*;
public class ex04 {
    public static void main(String[]args ){

        int mat[][] = {{1,2,3,4,5,6,},{1,12,13,14,15,16},{1,22,23,24,25,26}};
        int i,j;
        int lowest=0, highest=0;

        System.out.println("informed values: ");
            for(i=0;i<mat.length;i  ){ 
                for(j=0;j<mat[i].length;j  ){ 
                    if (mat[i][j] > highest) {
                        highest = mat[i][j];
                }
                    System.out.print(mat[i][j] "\t");
                }
                System.out.println("");
        }

        lowest = highest;
    
        for (i = 0; i < mat.length; i  ) {
            for (j = 0; j < mat[i].length; j  ) {
                if (mat[i][j] < lowest) {
                    lowest = mat[i][j];
        } } }
        System.out.println("\n");
        for (i = 0; i < mat.length; i  ) {
            for (j = 0; j < mat[i].length; j  ) {
                if(mat[i][j] == highest){
                     System.out.println(" highest is : "   highest   " located in row "   
                     (i 1)   " and column "   (j 1) );
        }
                if(mat[i][j] == lowest){
            System.out.println(" lowest is : "   lowest   " located in row "   (i 1)   
            " and column "   (j 1));
        } } }
    }    
}

CodePudding user response:

Looking for something like this?

private void foo(){
    int[][] mat = {{1,2,3,4,5,6,},{1,12,13,14,15,16},{1,22,23,24,25,26}};
    int rows = mat.length;
    int cols = mat[0].length;
    int lowest=Integer.MAX_VALUE;
    int highest=Integer.MIN_VALUE;
    List<int[]> highestPos = new ArrayList<>();
    List<int[]> lowestPos = new ArrayList<>();

    System.out.println("informed values: ");
    for(int i=0; i<rows; i  ){
        for(int j=0; j<cols; j  ){
            highest = Math.max(mat[i][j], highest);
            lowest = Math.min(mat[i][j], lowest);
            System.out.print(mat[i][j] "\t");
        }
        System.out.println("");
    }

    for(int i=0; i<rows; i  ){
        for(int j=0; j<cols; j  ){
            if(mat[i][j] == highest){
                highestPos.add(new int[]{i, j});
            }
            else if(mat[i][j] == lowest){
                lowestPos.add(new int[]{i, j});
            }
        }
    }

    System.out.println("highest is : "   mat[highestPos.get(0)[0]][highestPos.get(0)[1]]);
    for(int[] h : highestPos){
        System.out.println("located in : "   "row : "   (h[0]   1)   " col : "   (h[1]   1));
    }

    System.out.println("\n");
    System.out.println("lowest is : "   mat[lowestPos.get(0)[0]][lowestPos.get(0)[1]]);
    for(int[] l : lowestPos){
        System.out.println("located in : "   "row : "   (l[0]   1)   " col : "   (l[1]   1));
    }
}

CodePudding user response:

You can do spot both max and min values and their coordinates in a single pass through the given matrix.

It would be far more continent to store the data related to a particular element (value, row, column) grouped as an object. And or in order to handle the case when highest and lowest values can be encountered multiple times, you can create two collection of elements.

There are two of representing the element. It can be defined either as a class, or as a record. Record is a special kind of class introduced with Java 16 which serves as a transparent carrier of data.

Syntax for defining a record is very concise:

public record Element(int row, int col, int value) {}

That is the equivalent of a class with a constructor, getters, equals/hasCode and toString() (all this code will be generated by the compiler for you).

While iterating over the matrix if the next encountered item is greater then items is the list of highest element, then all previously added elements need to be removed and new one has to be added. If the value of the next item is equal to the value of items in list, or the list is empty, then the next element need to be added to the list (no more actions required). Values lower than existing will be ignored.

Similarly, with the lowest values.

public static void main(String[] args) {
    int mat[][] = {{1, 2, 3, 4, 5, 6},
                    {1, 12, 13, 14, 15, 16},
                    {1, 22, 23, 24, 25, 26}};

    List<Element> lowestElements = new ArrayList<>();
    List<Element> highestElements = new ArrayList<>();

    for (int row = 0; row < mat.length; row  ) {
        for (int col = 0; col < mat[row].length; col  ) {
            if (highestElements.isEmpty() || mat[row][col] == highestElements.get(0).value()) {
                highestElements.add(new Element(row, col, mat[row][col]));
                
            } else if (mat[row][col] > highestElements.get(0).value()) {
                
                highestElements.clear();
                highestElements.add(new Element(row, col, mat[row][col]));
            }
            if (lowestElements.isEmpty() || mat[row][col] == lowestElements.get(0).value()) {
                lowestElements.add(new Element(row, col, mat[row][col]));
                
            } else if (mat[row][col] < lowestElements.get(0).value()) {

                highestElements.clear();
                lowestElements.add(new Element(row, col, mat[row][col]));
            }
        }
    }

    System.out.println("Lowest elements are:");
    for (Element element: lowestElements) System.out.println(element);
    System.out.println("Highest elements are:");
    for (Element element: highestElements) System.out.println(element);
}

Output

Lowest elements are:
Element[row=0, col=0, value=1]
Element[row=1, col=0, value=1]
Element[row=2, col=0, value=1]
Highest elements are:
Element[row=2, col=5, value=26]
  • Related