Array max value

When I set an array k = [3,5,6,0,1,4,5,6,9,1]

And I used the k.max can know the max value is 9

How do I get the number “8” (as k[8] = 9)??

Thanks,

On Jul 21, 2011, at 2:27 AM, CC Chen wrote:

When I set an array k = [3,5,6,0,1,4,5,6,9,1]

And I used the k.max can know the max value is 9

How do I get the number “8” (as k[8] = 9)??

k.index(k.max) will work. It’s not completely optimal, but it’s concise
and asymptotically optimal.

Michael E. wrote in post #1012048:

On Jul 21, 2011, at 2:27 AM, CC Chen wrote:

When I set an array k = [3,5,6,0,1,4,5,6,9,1]

And I used the k.max can know the max value is 9

How do I get the number “8” (as k[8] = 9)??

k.index(k.max) will work. It’s not completely optimal, but it’s concise
and asymptotically optimal.

:slight_smile:

Here’s another approach which is a tad more involved and iterates the
array only once:

irb(main):001:0> k = [3,5,6,0,1,4,5,6,9,1]
=> [3, 5, 6, 0, 1, 4, 5, 6, 9, 1]
irb(main):002:0> k.each_with_index.max_by {|e,i| e}.last
=> 8

Kind regards

robert

On Thu, Jul 21, 2011 at 3:27 PM, CC Chen [email protected] wrote:

When I set an array k = [3,5,6,0,1,4,5,6,9,1]

And I used the k.max can know the max value is 9

How do I get the number “8” (as k[8] = 9)??

What if you have an array like this [3,5,6,0,1,4,5,6,9,1,9] ?
Do you want 8 or 10 or both?

If you want both you could try something like this.

per = [3,5,6,0,1,4,5,6,9,1,9]

so_my_jacket, wont_open, now = [], per.max, per.size # because,
(0…now).zip(per){|is_stuck, shut| so_my_jacket << is_stuck if
shut==wont_open}

p so_my_jacket #> [8, 10]

OR

k = [3,5,6,0,1,4,5,6,9,1,9]
m,s,r = k.max,k.size,[]
(0…s).zip(k){|x,i| r << x if i == m}
p r #> [8, 10]

Harry

@esha_d you walk the n-element array once for the collect call. And
within each collect call you walk the array via k.max. That is O(n^2)
if my vague recollection of uni serves me. Each call to k.max requires
a walk of the array, unless the interpreter used caches k.max after the
first walk.

The way I see this working (please check me):
k.each_with_index creates an enumerator so no looping there. But collect
walks the array (with the embedded k.max). Then compact must walk the
result of collect to strip out the nil’s.

Given we know k doesn’t change over the collect loop we can pull the
k.max out.
k_max = k.max;
Then you loop over a k.size array once for the initial k.max, once for
the collect, and a last time for the compact.

It is possible to write the loop to walk the k array once with an each.
And loop over the result set once to strip out the indexes.

a = [9, 9, 6, 0, 1, 4, 5, 6, 9, 1, 9]
b = []
a.each_with_index do |e,i|
cur_max = (b.empty?) ? e : b[0][1]
if e > cur_max
b = [[i,e]]
elsif e == cur_max
b.push [i,e]
end
end
p b.collect {|e| e[0] }

Algorithmically both approaches are O(n), other than the k.max thing.
However, it is clear that one is 3n versus n+m (where m is the result
set size).

-LLeo

Try following :

irb(main):035:0> k = [9, 9, 6, 0, 1, 4, 5, 6, 9, 1, 9]
=> [9, 9, 6, 0, 1, 4, 5, 6, 9, 1, 9]

irb(main):034:0> k.each_with_index.collect{|t,i| i if t ==
k.max}.compact
=> [0, 1, 8, 10]

Esha

On Thu, Jul 21, 2011 at 11:30 AM, Harry K. [email protected]
wrote:

per = [3,5,6,0,1,4,5,6,9,1,9]

so_my_jacket, wont_open, now = [], per.max, per.size # because,
(0…now).zip(per){|is_stuck, shut| so_my_jacket << is_stuck if
shut==wont_open}

p so_my_jacket #> [8, 10]

Excellent example. :slight_smile:

On Thu, Jul 21, 2011 at 3:27 PM, CC Chen [email protected] wrote:

When I set an array k = [3,5,6,0,1,4,5,6,9,1]

And I used the k.max can know the max value is 9

How do I get the number “8” (as k[8] = 9)??

k = [3,5,6,0,1,4,5,6,9,1,9]

m,s = k.max,k.size
p (0…s).select{|x| k[x]==m}

Not as easy to make a goofy story out of this one :slight_smile:

Harry

@Harry Thanks. I am new to ruby. I look at these kinds of things to help
my understanding. My “solution” was very strained and I didn’t like it.

-LLeo