Finding the Max Value in a Two Dimensional Array

Finding the Max value in a two dimensional Array

Max of max numbers (map(max, numbers) yields 1, 2, 2, 3, 4):

>>> numbers = [0, 0, 1, 0, 0, 1], [0, 1, 0, 2, 0, 0], [0, 0, 2, 0, 0, 1], [0, 1, 0, 3, 0, 0], [0, 0, 0, 0, 4, 0]

>>> map(max, numbers)
<map object at 0x0000018E8FA237F0>
>>> list(map(max, numbers)) # max numbers from each sublist
[1, 2, 2, 3, 4]

>>> max(map(max, numbers)) # max of those max-numbers
4

How to find first 5 highest value in a two dimensional array?

  1. Let MAX_N = 5.
  2. Find count as the total number of elements in matrix[][].
  3. Create flattened = new int[count] and fill it with all elements of matrix[][].
  4. Create max = new int[MAX_N] to store maximum n numbers. Also, create maxPos = new int[MAX_N] to store the position of the maximum numbers.
  5. Loop MAX_N times and in each iteration, assume flattened[0] is the largest number.
  6. If flattened[j] >= max[i], check if the position, j has already been processed. If not assign flattened[j] to max[i] and j to maxPos[i].

Demo:

import java.util.Arrays;

public class Main {
public static void main(String[] args) {
final int MAX_N = 5;
int[][] matrix = {
{16, -20, 11, 19},
{2, 5, 6, 8},
{17, 25, 16, 19},
{7, 17, 4, 17}};

// Find count as the total number of elements
int count = 0, row, col;
for (row = 0; row < matrix.length; row++) {
count += matrix[row].length;
}

// Create flattened = new int[count] and
// fill it with all elements of matrix[][]
int[] flattened = new int[count];
int i = 0;
for (row = 0; row < matrix.length; row++) {
for (col = 0; col < matrix[row].length; col++) {
flattened[i++] = matrix[row][col];
}
}

// Create max = new int[MAX_N] to store maximum
// n numbers. Also, create maxPos = new int[MAX_N]
// to store the position of the maximum numbers.
int[] max = new int[MAX_N];
int[] maxPos = new int[MAX_N];

// Loop MAX_N times. In each iteration,
// assume flattened[0] is the largest number.
for (i = 0; i < max.length; i++) {
max[i] = flattened[0];

for (int j = 1; j < flattened.length; j++) {
// If flattened[j] >= max[i], check if the
// position, j has already been processed.
// If not assign flattened[j] to max[i]
// and j to maxPos[i].
if (flattened[j] >= max[i]) {
boolean posAlreadyProcessed = false;
for (int k = 0; k <= i; k++) {
if (maxPos[k] == j) {
posAlreadyProcessed = true;
break;
}
}
if (!posAlreadyProcessed) {
max[i] = flattened[j];
maxPos[i] = j;
}
}
}
}
System.out.println("Largest " + MAX_N +
" values: " + Arrays.toString(max));
}
}

Output:

Largest 5 values: [25, 19, 19, 17, 17]

Finding minimum and maximum in Java 2D array

Ok, I've kinda fixed your code. Actually your mistake was that you have not been traversing all the cells of your multidimensional array.

So, I've added additional loop into getMinValue/getMinValue methods and fixed array elements addressing.

import java.util.*;

class MinMax {
public static void main(String[] args) {
int[][] data = {
{3, 2, 5},
{1, 4, 4, 8, 13},
{9, 1, 0, 2},
{0, 2, 6, 3, -1, -8}
};
System.out.println(getMaxValue(data));
System.out.println(getMinValue(data));
}


public static int getMaxValue(int[][] numbers) {
int maxValue = numbers[0][0];
for (int j = 0; j < numbers.length; j++) {
for (int i = 0; i < numbers[j].length; i++) {
if (numbers[j][i] > maxValue) {
maxValue = numbers[j][i];
}
}
}
return maxValue;
}

public static int getMinValue(int[][] numbers) {
int minValue = numbers[0][0];
for (int j = 0; j < numbers.length; j++) {
for (int i = 0; i < numbers[j].length; i++) {
if (numbers[j][i] < minValue ) {
minValue = numbers[j][i];
}
}
}
return minValue ;
}
}

Find max value of each column in 2D array

You can init the result with the first row, then iterate on this matrix, if the current element test[row][column] is greater than result[column], reassign result[column]:

public static void main(String[] args) {
int[][] test = {{3, 9, 3, 5}, {4, 19, 4, 9}, {2, 10, 5, 6}};
int[] result = test[0];
for (int row = 1; row < test.length; row++) {
for (int column = 0; column < test[0].length; column++) {
if (test[row][column] > result[column]) {
result[column] = test[row][column];
}
}
}
for (int number : result) {
System.out.print(number + " "); // 4 19 5 9
}
}

Find highest value in multidimensional array

Since PHP 5.5 you can use array_column to get an array of values for specific key, and max it.

max(array_column($array, 'Total'))

how to find max value of the two-dimensional array in java using multithreading

Here is the outline how you would go about implementing this. I am not providing code intentionally, so that you can have the joy of implementing it yourself.

create a method to findmax out of an array lets call it findMax(int[]
input)

for each sub array in 2D array (can be accessed using matrix[i])

start a thread to findMax(matrix[i]) (hint: use ExecutorService)
in the thread, once max is found, fill it in to ith position
of a one dimensional array called results in the thread, indicate
its completion(hint: use CountDownLatch)

In the main thread, wait till all threads finish ( hint: use
CountDownLatch) Now call findMax(results) and you have the
maxiumum from matrix.

Considerations: Do we need to fork as many threads as the rows in matrix? So do we use a FixedThreadPool with number of rows ?

Find minimum and maximum value from two dimensional array

void fillUpArray(int newArray[5][5])
{
for (int i = 0; i < 5; ++i)
{
for (int j = 0; j < 5; ++j)
{
int randomNumber = rand() % 100 + 1;
printf("Random number[%d][%d]: %d\n", i, j, randomNumber);
newArray[i][j] = randomNumber;
}
}
}

void printMinimumMaximum(int myArray[5][5])
{
int minimum = myArray[0][0];
int maximum = myArray[0][0];

for (int i = 0; i < 5; ++i)
{
for (int j = 0; j < 5; ++j)
{
if (myArray[i][j] < minimum)
{
minimum = myArray[i][j];
}
if (myArray[i][j] > maximum)
{
maximum = myArray[i][j];
}
}
}

printf("Minimum: %d\n", minimum);
printf("Maximum: %d\n", maximum);
}

int main()
{
int minMax[5][5];
fillUpArray(minMax);
printMinimumMaximum(minMax);
return 0;
}

Max value of a multidimensional array javascript

let

var arr = [[2,3], [4,5]]; // a multidimensional array

then get an array with each row's maximum with

var maxRow = arr.map(function(row){ return Math.max.apply(Math, row); });

and the overal maximum with

var max = Math.max.apply(null, maxRow);

The right way to find the max value in a 2d list?

The problem is python has no notion of multi-dimensional arrays built in.

When you call max on a list of lists, you end up with a list that is highest in the lexicographic order, not the one containing the highest element.

x = [ [4,2,3], [3,10,0] ]
max(x) == [4,2,3] # does not contain 10
max(max(x)) == 4

In other words max(max(x)) in python is not the highest value over two axes of x, but rather the maximal value in an entry of x, that is highest under the default ordering of the objects there (which for lists is lexicographic).

The easiest way is to use numpy, which has a proper way of interpreting k-dimensional arrays

import numpy as np
np.max(x) == 10 # as expected

Find nested min/max array in a two dimensional array

The output you wanted is strange, but this does the trick.

const arr = [[1622306648284, 1.4025036293793085],[1622309604992, 1.384071162873584],[1622312530257, 1.3503030062861177],[1622315654724, 1.3625441847416848],[1622318703104, 1.3645747739529213],[1622321575558, 1.3611235799170127],[1622324539379, 1.3750838657128996],[1622327549644, 1.378768535066251],[1622330652746, 1.3916061750979443],[1622333538315, 1.4076792700030256],[1622336466156, 1.3674852893896725]];

const max = arr.reduce((a, b) => a[1] >= b[1] ? a : b);
const min = arr.reduce((a, b) => a[1] <= b[1] ? a : b);

console.log(`Min: ${min} Max: ${max}`);


Related Topics



Leave a reply



Submit