17
\$\begingroup\$

Given a non-empty matrix of non-negative integers, answer which unique rows contribute most to the sum total of elements in the matrix.

Answer by any reasonable indication, for example a mask of the unique rows order of appearance (or sort order), or indices (zero- or one- based) of those, or a submatrix consisting of the rows (in any order) or some kind of dictionary construct… — but do explain it!

Examples

[[1,2,3],[2,0,4],[6,3,0],[2,0,4],[6,3,0],[2,0,4]]:

The unique rows are [1,2,3], [2,0,4], and [6,3,0] each respectively contributing 6, 6, and 9 each time they occur. However, they occur once, thrice and twice respectively, so all of their respective occurrences contribute 6, 18, and 18 to the total (42), so the latter two rows are the ones that contribute most. Valid answers are therefore:

[false,true,true] mask in appearance/sort order or
[1,2]/[2,3] zero/one-based indices of the above or
[[2,0,4],[6,3,0]] the actual rows


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

[false,false,true](appearance order) / [false,true,false](sort order)
[2]/[3](appearance order) / [1]/[2](sort order)
[[2,3]]

\$\endgroup\$

20 Answers 20

5
\$\begingroup\$

Jelly, 5 bytes

Ġị§§M

Try it online!

1-based indices of sorted unique elements of input.

\$\endgroup\$
  • 3
    \$\begingroup\$ Woha, that is short. \$\endgroup\$ – Adám Dec 31 '18 at 16:44
4
\$\begingroup\$

Pyth, 9 bytes

-1 thanks to FryAmTheEggman!

{s.MssZ.g

Try it online!

\$\endgroup\$
  • 1
    \$\begingroup\$ The final k isn't necessary. Also .M*sZ/QZ{ seems to be a same-length solution then. \$\endgroup\$ – FryAmTheEggman Dec 31 '18 at 21:12
  • 1
    \$\begingroup\$ @FryAmTheEggman Oh lol how did I forget about auto-complete? Thanks a lot! \$\endgroup\$ – Mr. Xcoder Dec 31 '18 at 21:42
4
\$\begingroup\$

R, 64 bytes

function(M)max(x<-tapply(rowSums(M),apply(M,1,toString),sum))==x

Try it online!

Returns a boolean vector with TRUE/FALSE in sort order (lexicographic).
The unique rows are shown as vector names, so it is easy to identify the most contributing ones.

\$\endgroup\$
3
\$\begingroup\$

Python 3, 153 145 129 bytes

-8 bytes thanks to @Mr. Xcoder!

from itertools import*
def f(l):a=[[sum(map(sum,[*s])),k]for k,s in groupby(sorted(l))];return[v[1]for v in a if v[0]==max(a)[0]]

Try it online!

\$\endgroup\$
2
\$\begingroup\$

Haskell, 60 bytes

import Data.Lists
f x=nub$argmaxes(\e->sum e*countElem e x)x

Returns a list of the rows.

\$\endgroup\$
2
\$\begingroup\$

Charcoal, 25 bytes

IΦθ∧⁼κ⌕θι⁼×№θιΣι⌈Eθ×№θλΣλ

Try it online! Link is to verbose version of code. Default output format is each row element on its own line and rows double-spaced. Explanation:

  θ                         Input array
 Φ                          Filtered where
     κ                      Current index
    ⁼                       Equals
      ⌕                     First index of
        ι                   Current row
       θ                    In input array
   ∧                        Logical And
           №                Count of
             ι              Current row
            θ               In input array
          ×                 Multiplied by
              Σ             Sum of
               ι            Current row
         ⁼                  Equals
                ⌈           Maximum of
                  θ         Input array
                 E          Mapped over rows
                    №       Count of
                      λ     Current row
                     θ      In input array
                   ×        Multiplied by
                       Σ    Sum of
                        λ   Current row
I                           Cast to string
                            Implicitly printed
\$\endgroup\$
2
\$\begingroup\$

Mathematica, 48 bytes

Last[SortBy[Gather[m], Total[Flatten[#]] &]][[1]]

or

TakeLargestBy[Gather[m], Total[#, 2] &, 1][[1, 1]]

where (for example)

m = {{1, 2, 3}, {2, 0, 4}, {7, 9, 5}, {6, 3, 0}, {2, 0, 4}, 
     {6, 3, 0}, {2, 0, 4}, {7, 9, 5}};
\$\endgroup\$
  • 2
    \$\begingroup\$ You can use shorthand and remove whitespace to save bytes: SortBy[Gather@m,Total@*Flatten][[-1,1]] \$\endgroup\$ – Doorknob Jan 1 at 1:56
  • 1
    \$\begingroup\$ This looks like it takes input from a predefined variable, which is not allowed. Submissions have to be full programs or function by default. \$\endgroup\$ – Dennis Jan 2 at 0:52
  • \$\begingroup\$ TakeLargestBy[Gather[m], Total[#, 2] &, 1][[1, 1]] /@ m \$\endgroup\$ – David G. Stork Jan 2 at 0:54
  • \$\begingroup\$ This is not valid; it only returns one of the rows with greatest values rather than all of them. \$\endgroup\$ – lirtosiast Jan 2 at 1:50
1
\$\begingroup\$

JavaScript (ES6), 88 bytes

Outputs an array of Boolean values in appearance order.

m=>m.map(h=o=r=>h=(v=o[r]=~~o[r]+eval(r.join`+`))<h?h:v)&&Object.keys(o).map(x=>o[x]==h)

Try it online!

\$\endgroup\$
1
\$\begingroup\$

Groovy, 76 bytes

{l->m=l.toSet().sort().collect{it.sum()*l.count(it)}
m.collect{it==m.max()}}

Try it online!

Returns as booleans in sort order

\$\endgroup\$
1
\$\begingroup\$

Scala, 63 bytes

l=>{var m=l.distinct.map(n=>n.sum*l.count(n==))
m.map(m.max==)}

Try it online!

Returns booleans in appearance order

\$\endgroup\$
1
\$\begingroup\$

APL (Dyalog Unicode), 12 bytes

(⌈/=⊢)+.×∘⍴⌸

Try it online!

-2 thanks to Adám. -1 thanks to alternate output format.

\$\endgroup\$
  • \$\begingroup\$ Now comes the challenge of explanation… \$\endgroup\$ – Adám Jan 2 at 9:30
1
\$\begingroup\$

Wolfram Language (Mathematica), 42 bytes

#~Position~Max@#&@(Total[#,2]&/@Gather@#)&

Try it online!

\$\endgroup\$
1
\$\begingroup\$

Python 2, 81 78 bytes

lambda a:{u for u in a if a.count(u)*sum(u)==max(a.count(t)*sum(t)for t in a)}

Try it online!

3 bytes thx to Black Owl Kai.

Given a collection of tuples, the output is a set of those tuples having the desired maximal property.

\$\endgroup\$
  • \$\begingroup\$ 78 bytes \$\endgroup\$ – Black Owl Kai Jan 2 at 11:49
  • \$\begingroup\$ @Black Owl Kai: thx! I missed that... \$\endgroup\$ – Chas Brown Jan 2 at 20:29
1
\$\begingroup\$

Japt, 13 11 bytes

-2 bytes from @Shaggy

ü¬®xx
m¥Urw

Try it online!

\$\endgroup\$
0
\$\begingroup\$

C# (Visual C# Interactive Compiler), 126 bytes

n=>{var i=n.OrderBy(a=>a.Sum()*n.Count(a.SequenceEqual));return i.Where((a,b)=>i.Take(b).Count(a.SequenceEqual)<1).Reverse();}

Try it online!

Most of this code is spent taking out all duplicate values, since the default comparer for lists do not compare the values inside the lists. That means that I cannot use Distinct(), GroupBy(), or Contains to filter the list.

\$\endgroup\$
0
\$\begingroup\$

K (ngn/k), 17 bytes

{&a=|/a:+//'x@=x}

Try it online!

{ } function with argument x

=x group - form a dictionary in which the keys are rows and the values are lists of their indices in the matrix

x@ index the original matrix with that. the result is again a dictionary with the rows as keys. the values are multiple copies of the corresponding key

+//' sum until convergence each (acts only on the values; keys remain as they are)

a: assign to a

|/ maximum (of the values)

a=|/a a row-to-boolean dictionary of which rows contribute the most

& "where", i.e. which keys correspond to values of 1

\$\endgroup\$
0
\$\begingroup\$

Japt, 11 10 bytes

ü¬ü_xxÃÌmâ

Run it online

\$\endgroup\$
0
\$\begingroup\$

05AB1E, 10 9 bytes

ês{γOOZQÏ

Try it online or verify all test cases.

Explanation:

ê          # Sort and uniquify the (implicit) input list of lists
           #  i.e. [[2,0,4],[1,2,3],[6,3,0],[2,0,4],[6,3,0],[2,0,4]]
           #   → [[1,2,3],[2,0,4],[6,3,0]]
 s         # Swap so the (implicit) input list of lists is at the top again
  {        # Sort it
           #  i.e. [[2,0,4],[1,2,3],[6,3,0],[2,0,4],[6,3,0],[2,0,4]]
           #   → [[1,2,3],[2,0,4],[2,0,4],[2,0,4],[6,3,0],[6,3,0]]
   γ       # Group the sorted list of lists
           #  i.e. [[1,2,3],[2,0,4],[2,0,4],[2,0,4],[6,3,0],[6,3,0]]
           #   → [[[1,2,3]],[[2,0,4],[2,0,4],[2,0,4]],[[6,3,0],[6,3,0]]]
    O      # Take the sum of each inner-most lists
           #  i.e. [[[1,2,3]],[[2,0,4],[2,0,4],[2,0,4]],[[6,3,0],[6,3,0]]]
           #   → [[6],[6,6,6],[9,9]]
     O     # Take the sum of each inner list
           #  i.e. [[6],[6,6,6],[9,9]] → [6,18,18]
      Z    # Get the max (without popping the list of sums)
           #  i.e. [6,18,18] → 18
       Q   # Check for each if this max is equal to the sum
           #  i.e. [6,18,18] and 18 → [0,1,1]
        Ï  # Filter the uniquified list of lists on truthy values (and output implicitly)
           #  i.e. [[1,2,3],[2,0,4],[6,3,0]] and [0,1,1] → [[2,0,4],[6,3,0]]
\$\endgroup\$
0
\$\begingroup\$

Gaia, 10 bytes

ȯẋ_¦Σ¦:⌉=¦

Try it online!

Since Gaia doesn't accept lists through inputs very easily, this is a function that accepts a list from the top from the top of the stack and leaves the result on top (as masks of sorted order).

ȯ           Sort the list
 ẋ          Split it into runs of the same element (in this case, runs of the same row)
  _¦        Flatten each sublist
    Σ¦      Sum each sublist
      :⌉    Find the maximum sum
        =¦  Compare each sum for equality with the maximum
\$\endgroup\$
0
\$\begingroup\$

J, 16 bytes

[:(=>./)+/^:2/.~

Try it online!

A monadic verb that gives the boolean result in appearance order.

How it works

[:(=>./)+/^:2/.~
             /.~  Self-classify; collect identical rows in appearance order
                  For each collected rows (= a matrix),
        +/^:2       Sum all the elements into one value
[:(=>./)          Compute the boolean vector:
    >./             Is the max of the array
   =                equal to this element?
\$\endgroup\$

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.