# Maximum Maxima!

Inspired by this question and refined by Luis Mendo.

## Challenge

Given a 2D matrix of integers, each row has a maximum value. One or more elements of each row will be equal to the maximum value of their respective row. Your goal is to determine which column(s) contain the most entries which are equal to their respective row's maximum value as well as the number of row-wise maxima found in these columns.

## Input

• The input will be a non-empty M x N matrix (M > 0 and N > 0) in whatever form is well-suited to your language of choice.

## Output

• Your program should return the index of each column containing the maximum number of row-wise maxima (either as separate values or a list). Either 0- or 1-based indexing can be used (specify in your description).
• Your program should also return the number of maxima that were present in these columns (a single number).
• The order/format of the output is flexible but should be explained in the text accompanying your answer.

• All entries in the input matrix will be positive integers.
• If the maximum value of a row is shared by multiple elements in that row, all occurrences of that value count towards their columns' total.
• If multiple columns contain the same number of maxima, you should return a list of all columns which had this number of maxima.

## An Example

Consider input

7  93
69  35
77  30

Row 1 has maxium 93, which occurs only once, namely at column 2. Row 2: occurs at column 1. Row 3: also at column 1. So the winner column is 1, with 2 maxima. Thus the output will be [1] [2]. If we change the input to

7  93
69  35
77  77

the output will be [1 2] [2], because both columns have 2 maxima.

## Test Cases

input                 =>    output ( [1-based index array], [nMaxima] )
----------------------------------------------
7  93
69  35                =>    [1], [2]
77  30

7  93
69  35                =>    [1 2], [2]
77  77

1   2   3   4         =>    [4], [2]
5   6   7   8

16   2   3  13
5  11  10   8        =>    [1  2  4], [1]
9   7   6  12

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

25   6  13  25        =>    [1  4], [1]

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

100                   =>    [1], [1]

## Scoring

This is , the shortest code in bytes wins. Tiebreaker goes to the earlier answer.

Below is a stack snippet for analyzing all entries.

• Fun fact; the Dutch queen is called Maxima, so technically we can only have 1 Maxima. – Bassdrop Cumberwubwubwub May 27 '16 at 10:37
• Fun fact; there is also an opensource CAS called Maxima. – flawr May 27 '16 at 11:57

# Jelly, 9 bytes

="Ṁ€SµM,Ṁ

Input is a 2D list, output is a pair: a list of 1-based indices and the maximal number of maxima.

### How it works

="Ṁ€SµM,Ṁ  Main link. Argument: M (matrix)

Ṁ€       Apply maximum to each row.
="         Zipwith equal; compare the entries of the nth row with its maxmium.
S      Sum; reduce across columns to count the maxima in each row.
M    Yield all indices with maximal value.
Ṁ  Yield the maximum of A.
,   Pair the results to both sides.

## J, 27 bytes

((I.@:=;])>./)@(+/@:=>./"1)

This is a monadic verb, used as follows in the case of the second example:

f =: ((I.@:=;])>./)@(+/@:=>./"1)
m =: 3 2 $7 93 69 35 77 77 f m +---+-+ |0 1|1| +---+-+ The output consists of two boxes, and uses 0-based indexing. Try it here! ## Explanation ((I.@:=;])>./)@(+/@:=>./"1) Input is m. ( )@( ) Composition: apply right hand side, then left hand side. >./"1 Take maximum of each row of m. = Replace row maxima by 1 and other values by 0, +/@: then take sum (number of maxima) on each column. The result is the array of number of row maxima in each column. >./ Compute the maximum of this array ( ;]) and put it in a box with I.@:= the indices of those entries that are equal to it. # MATL, 17 bytes vH3$X>G=XstX>tb=f

The first output is the max number of maxima and the second output is the columns in which this occured (1-based indexing).

Try it Online!

Explanation

v       % Vertically concatenate everything on the stack (nothing), yields []
% Implicitly grab the input
H       % Push the number 2 to the stack
3\$X>    % Compute the maximum value of each row (along the second dimension)
G       % Explicitly grab input again
=       % Compare each row of the input to the row-wise max (automatically broadcasts).
Xs      % Sum the number of matches in each column
t       % Duplicate the array
X>      % Determine the max number of maxima in all columns
t       % Duplicate this value
b=f     % Find the index of the columns which had the maximum number of maxima
% Implicitly display stack contents

# MATL, 17 bytes

!tvX>!G=5#fFTT#XM

Input is a 2D array, with rows separated by semicolons. So the inputs for the test cases are

[7 93; 69 35; 77  30]
[7 93; 69 35; 77  77]
[1 2 3 4; 5 6 7 8]
[16 2 3 13; 5 11 10 8; 9 7 6 12]
[1 1 1 1]
[25 6 13 25]
[1; 2; 3; 4]
[100]

Output is: first the maximum amount of maxima, then one or more column indices.

Try it online!

### Explanation

This uses a different approach from Suever's answer.

First a matrix of logical values (true and false) is computed, where true indicates the presence of a row-maximum. Then the column indices of the true values are extracted into a vector. Finally, the mode of that vector is computed (maximum number of maxima), along with all values that are the most frequent (desired column indices).

!        % Implicit input. Transpose
tv       % Duplicate. Concatenate vertically. This forces next function (max)
% to work along columns even if input is a row vector
X>       % Maximum of each column (gives row vector)
!        % Transpose into column vector
G        % Push input again
=        % Test for equality, with broadcast. Gives matrix of true and false
5#f      % Column indices of true values, as a column vector
FTT#XM   % Mode of that vector, and all values that occur maximum number of times
% Implicit display

# Pyth, 2019 17 bytes

1 byte thanks to @Suever.

1 byte thanks to @Jakube.

Test suite.

Output is 0-indexed.

Order is reversed.

### All inputs

[[7,93],[69,35],[77,30]]
[[7,93],[69,35],[77,77]]
[[1,2,3,4],[5,6,7,8]]
[[16,2,3,13],[5,11,10,8],[9,7,6,12]]
[[1,1,1,1]]
[[25,6,13,25]]
[[1],[2],[3],[4]]
[[100]]

### All outputs

[[2], [0]]

[[2], [0, 1]]

[[2], [3]]

[[1], [0, 1, 3]]

[[1], [0, 1, 2, 3]]

[[1], [0, 3]]

[[4], [0]]

[[1], [0]]

## How it works

Q   Yield input.
L       For each array in input (as d):
eSd      Yield maximum of d.
x          Yield the 0-indexed indices of the maximum in d.
s          Flatten.
S           Sort.
r         8  Run-length encoding.
Now the array is:
[number of maxima in column, index of column]
for all the columns
.MhZ             Yield the sub-arrays whose first element is maximum.
The first element of each sub-array
is "number of maxima in column".
Now the array is:
[number of maxima in column, index of column]
for all the required columns
C                 Transpose.
Now the array is:
[[number of maxima in each column],
[index of each required column]]
Note that every element in the
first sub-array is the same.
{M                  Deduplicate each.

# CJam, 3835 31 bytes

2 bytes fewer thanks to @FryAmTheEggMan, with help also from @quartata. Thanks also to @Dennis for removing 4 more bytes.

q~_::e>.f=:.+_:e>_@f{=U):Ua*~}p

Input is of the form

[[7 93] [69 35] [77 77]]

Output is an array of 1-based column indices and a number.

Try it online!

• q~_::e>.f=:.+_:e>_@f{=U):Ua*~}p saves a few bytes. Turning it into a code block would save 1 more. – Dennis May 28 '16 at 5:25
• @Dennis Thanks! Now I need to understand what {=U):Ua*~} does... – Luis Mendo May 28 '16 at 10:26

# Husk, 181619 16 bytes

SṠ·e;¥▲mΣTmṠom=▲

Try it online!

Probably my best use of combinators in Husk.

-2 bytes from Jo King.

+3 bytes after satisfying first test case.

-3 bytes from Jo King(again!)

## Explanation

§eṠoW=▲o;▲mΣTmṠom=▲
m      map each row to the following:
Ṡ     hook: Ṡ f g x = f (g x) x
m=  map each element of the row by equality
▲ to the maximum of the row
T       transpose the matrix
mΣ        map each column to its sum
§                   fork: § f g h x = f (g x) (h x)
e                  create two element list from:
Ṡ                 hook: Ṡ f g x = f (g x) x
oW=              indices of elements wich equal
▲             maximum, and:
o;▲          number of maxima

FDSeRmq),AsDSe
R%

Try it here!

# Python 2, 106 bytes

x=map(sum,zip(*[map(max(r).__eq__,r)for r in input()]))
m=max(x);print[i for i,n in enumerate(x)if n==m],m

Input is a 2D list of floats, output is a pair: a list of 0-based indices and an integer.

Test it on Ideone.

# Julia, 54 bytes

f(x,m=maximum,t=sum(x.==m(x,2),1))=find(t.==m(t)),m(t)

Input is a matrix, output is a pair: a list of 1-based indices and the maximal number of maxima.

Try it online!

# 05AB1E, 14 (or 12) bytes

Outputs in the format [[1-indexed columns-list], maxima].

If it's allowed to have items 0 in the columns-list which we ignore, it could be 2 bytes less by removing the 0K:

Explanation:

ε               # Map each row in the (implicit) input-matrix:
Z              #  Get the maximum of the row (without popping)
Q             #  Check for each value in this row if its equal to the row-maximum
}ø           # After the map: zip/transpose the matrix; swapping rows/columns
O          # Take the sum of each inner list (the truthy/falsey values of the columns)
Z         # Get the maximum column-sum (without popping)
©        # Store it in the register (without popping)
Q       # Check for each column-sum if its equal to this maximum
ƶ      # Multiply each truthy/falsey value in the list by its 1-based index
0K    # Remove all 0s
®‚  # Pair the resulting list with the maximum we stored in the register
# (and output the result implicitly)

## JavaScript (ES6), 111 bytes

a=>[m=Math.max(...a=a[0].map((_,i)=>a.map(a=>c+=a[i]==Math.min(...a),c=0)|c)),[...a.keys()].filter(i=>a[i]==m)]

Returns an array of two elements; the first is the maximum count of maxima, the second is the array of zero-indexed columns with that count.

# Octave, 47 46 bytes

@(r){m=max(s=sum(r==max(r,0,2),1)),find(s==m)}

This creates an anonymous function that automatically assigns itself to ans and can be run using ans([1 2 3; 4 5 6]). It returns a two-element cell array where the first element is the maximum number of maxima and the second is the 1-based index of the columns containing these maxima.

All test cases

# Python 3, 142 bytes

The algorithm here is basically, go through each row and increase the score of columns that have the max of that row. Then find the max of the scores and find the columns that have that max score and return them. Columns are 1-indexed. I tried one-lining this into a lambda, but with generating the scores column by column it was 153 bytes.

def f(r):
s=[0]*len(r[0]);e=enumerate
for x in r:
for i,j in e(x):
s[i]+=(0,1)[j==max(x)]
m=max(s);return[i+1for i,j in e(s)if j==m],m

## Test Cases

x=[[7, 93],
[69, 35],
[77, 30]]

print(f(x)) #=>    [[1], 2]

x=[[ 7, 93],
[69, 35],
[77, 77]]

print(f(x)) #=>    [[1 2], 2]

x=[[1,  2,   3,  4],
[5,  6,  7,  8]]

print(f(x)) #=>    [[4], 2]

x=[[16,  2,  3, 13],
[5, 11, 10,  8],
[9,  7, 6, 12]]

print(f(x)) #=>    [[1  2  4], 1]

x=[[1,  1,  1,  1]]

print(f(x)) #=>    [[1  2  3  4], 1]

x=[[25,   6,  13,  25]]

print(f(x)) #=>    [[1  4], 1]

x=[[1],
[2],
[3],
[4]]

print(f(x)) #=>    [[1], 4]

x=[[100]]

print(f(x)) #=>    [[1], 1]

## Clojure, 150 bytes

(fn[M](let[F(dissoc(frequencies(mapcat(fn[r](map-indexed #(if(=(apply max r)%2)%)r))M))nil)m(apply max(vals F))][(map first(filter #(#{m}(% 1))F))m]))

Man that is long, I've got a feeling this could be simplified a lot. At least it produces the correct output.

[(f [[ 7  93][69  35][77  30]])
(f [[ 7  93][69  35][77  77]])
(f [[16   2   3  13][5  11  10   8][9   7   6  12]])]

[[(0) 2] [(1 0) 2] [(0 1 3) 1]]