Questions tagged [max]

Maximum value. Largest, biggest, greatest.

max

max() is a math library function available in many programming environments which returns the entity of greatest value from two or more candidate values.

Do not use for questions about the maximal value of a type, e.g. Long.MAX_VALUE, Integer.MAX_VALUE, etc.

Examples

SQL

SQL MAX() function is one of aggregate functions. It returns the largest value of column.

SELECT MAX(column_name) FROM table_name;

Python

In Python, the native max function identifies the largest element of a set (e.g. a list).

>> max([1, 2, 3])
3

Java

In Java we can use java.lang.Math class to compute maximum value of two arguments.

int maxElement = Math.max(x, y);

To compute maximum element in collection we can use, max() method from java.util.Collections class.

int maxElement = Collections.max(list);

Ruby

In Ruby to compute maximum element of collection we simply use max function.

[4,7].max

Clojure

In Clojure we use max function in the following way.

(max 1 2 3 4)

In above code max is the function name and numbers 1..4 are arguments passed to it. We can also apply the max function to lists.

(apply max [1 2 3 4])

R

In R we have function max.

x <- c(1, 2, 3)
y <- c(4, 5)
z <- c(6, NA)
max(x)
max(y)
max(z)
max(z, na.rm = TRUE)
max(x, y, z, na.rm = TRUE)  ## as same as max(c(x, y, z), na.rm = TRUE)

Rust

In Rust we can use std::cmp::max to compute the maximum value of two arguments.

let max = std::cmp::max(x, y);

We can compute the maximum element in a collection by transforming it into an Iterator and then calling the max method on the iterator.

let max = collection.into_iter().max();
8982 questions
2
votes
3 answers

Return the maximun of a matrix

I want to return the max value of a matrix. For example this is my matrix: matrix = [[0, 1, 10, 0, 0], [0, 0, 6, 0, 1], [0, 1, 4, 0, 0]] I want to return the max so here '10' This is my code but I have an error: max = 0 for i in…
mario_nsport
  • 160
  • 8
2
votes
4 answers

how to find how many times the highest number in a list appeared using python?

e.g. if a list is: list = [1, 2, 3, 4, 4] how to count the amount of times number 4 appeared, while not specifying it. using max(list) or something like that.
My name
  • 21
  • 2
2
votes
2 answers

Using R - reshape a dataframe based on group max values of another dataframe

I am working with a very large dataset. Consider the following example for illustration: df1<-{data.frame(MyID=c(1, 2, 3, 1, 2, 3, 1, 2, 3, 4, 5),v1=c(0.1, 0.2, NA, 0.4, 0.2, 0.1, 0.8, 0.3, 0.1, 0.4, 0.3), v2=c(NA, 0.4, 0.2, 0.1, 0.8, 0.3, 0.1, 0.4,…
ie-con
  • 53
  • 4
2
votes
2 answers

Time complexity of finding max value in list using recursive bsearch

Sorry if this seems like a dumb question (I am new to Big O) but what is the difference between a) the time complexity of this function based on its no. of comparisons vs. b) the time complexity of the function overall? def bmax(list): if…
Jane Doe
  • 79
  • 8
2
votes
4 answers

SQL: How to exclude maximum if another column doesn't match

I'm using Oracle 10g. My research at SQL - How to select a row having a column with max value and http://jan.kneschke.de/projects/mysql/groupwise-max/ address what to do with max(row) and then finding other max(rows) But, I'm not there yet.…
lamarz
  • 35
  • 1
  • 5
2
votes
3 answers

Finding max value in an array of sub arrays

function largestOfFour(arr) { let newArr = []; let max = 0; for(let i = 0; imax){ max = arr[i][j]; console.log(max); …
medOnline5
  • 73
  • 1
  • 5
2
votes
5 answers

How to find the minimum, and maximum value within a Binary Tree

++ EDIT ++ Thanks to all of you, I have taken many compartments to get the code to finally working. Here's the full code overview class Node: # Binary Roots: Left and Right, initiating data def __init__(self, data): # Creating an object, Data,…
Steven H
  • 19
  • 1
  • 5
2
votes
2 answers

SQL SELECT most recently created row WHERE something is true

I am trying to SELECT the most recently created row, WHERE the ID field in the row is a certain number, so I don't want the most recently created row in the WHOLE table, but the most recently created one WHERE the ID field is a specific number. My…
Anthony
  • 53
  • 8
2
votes
1 answer

How do I set a maximum text width for a GtkTextView?

I have a GtkTextView, and I would like to be able to set a maximum line width for the text. If the width of the TextView exceeds the maximum text width, the extra space should be filled with padding on the left and right on the text. Although Gtk…
2
votes
0 answers

Finding max value on raw experimental data

I'm working on an investigation proyect, where I have to plot some experimetal data, and then find the max value (critical point), like on this image: I was trying to adjust a fit curve (using a cuadratic equation) in order to find that max value,…
2
votes
1 answer

SQL Group with Min and Max

I have a super simple query using group by and I just can't figure out how to get the desired result. It's literally a simple query with min() and max(). I have a table where assets belong to a certain location with a date in/out (it also has…
Linear_D
  • 25
  • 5
2
votes
2 answers

Theoretical average case runtime complexity of `numpy.argmax()`

I was looking at the code of numpy.argmax function. I am confused which data structure numpy maintains for the argmax function. https://numpy.org/doc/stable/reference/generated/numpy.argmax.html Eventually, I want to know what is the theoretical…
Mazharul Islam
  • 187
  • 1
  • 1
  • 10
2
votes
2 answers

What is the problem in IF statement in this code?

select studentid, sum(score) 'Total', avg(IF(score>85)) 'Average', max(score) 'Maximum', min(score) 'Minimum' from results group by studentid; Error Message: You have an error in your SQL syntax; check the manual that corresponds to your MariaDB…
2
votes
1 answer

SQL select the record whose HIGHEST timestamp is the LOWEST

I have a database that contains duplicated names and timestamps I want to retrieve the record whose timestamp is the lowest grouped by the name. table : people +------------+-------------+ | Name | Timestamp | +------------+-------------+ | …
Nacho R
  • 71
  • 1
  • 10
2
votes
2 answers

if statement and the maximal value in Pascal

Looking at this code: I thought that the program won't give the right maximal value if z>x and y>x, but, to my surprise, it did give the correct value. Why is that? Did the program compare Y and Z and gave the biggest value without me ordering it…