2

Using the Ruby language, have the function ArrayAdditionI(arr) take the array of numbers stored in arr and return the string true if any combination of numbers in the array can be added up to equal the largest number in the array, otherwise return the string false. For example: if arr contains [4, 6, 23, 10, 1, 3] the output should return true because 4 + 6 + 10 + 3 = 23. The array will not be empty, will not contain all the same elements, and may contain negative numbers.

Could someone please explain to me why this code starts at 'i=2' and not 'i=0'?

def ArrayAdditionI(arr)     
  i = 2

    while i < arr.length
     return true if arr.combination(i).map{|comb| comb.inject(:+)}.include?(arr.max)
     i += 1
   end

 false  

end

ArrayAdditionI(STDIN.gets) 

Correct me if I'm wrong but with i=2, the while loop will iterate [2..4] and then stop. But does this allow for all the potential combinations?...=> code works, so obviously it does but I'm just not seeing it.

Yu Hao
  • 119,891
  • 44
  • 235
  • 294
Zarley
  • 51
  • 2
  • 2
    `i` is used as an argument for `combination(i)`. I suggest to read the docs about the [`combination method`](http://ruby-doc.org/core-2.2.0/Array.html#method-i-combination). What combinations do you think might be missed by this method/loop? – spickermann Jun 29 '15 at 03:59
  • Thanks @spickermann, but I asked why start at '2'... not what is 'i'? – Zarley Jun 29 '15 at 04:02
  • `i` starts at `2` **because** it's not the index, it's used in `arr.combination(i)`. Try `arr.combination(0).to_a` and `arr.combination(1).to_a` and see what the result is. – Yu Hao Jun 29 '15 at 04:05
  • @Zarley: because combinations with zero or only one element do not make sense to sum up (at least not in the context of your exercise). – spickermann Jun 29 '15 at 04:05
  • Ahh.. ok. That makes sense now. Thanks! – Zarley Jun 29 '15 at 04:05
  • It's obvious why `i` cannot be zero: the sum of elements in an empty array is not defined. If `i=1` were permitted, and the max value in the array were `m`, the problem would have the trivial solution `[m]`. btw, `include?(arr.max)` is excruciatingly inefficient, as it calculated for every combination. It should be calculated once, at the beginning, and assigned to a variable. Also, `arr.combination(i).find {|comb| comb.inject(:+)} == mx }` would be preferable. – Cary Swoveland Jun 29 '15 at 05:13

3 Answers3

1

i is not the index of the array it the number of elements that is being used to create a combination. So if the max number in the array can be made with the sum of just two elements it stops if not it tries three and so on.

Yu Hao
  • 119,891
  • 44
  • 235
  • 294
osman
  • 2,335
  • 18
  • 25
1

array.combination(i) returns all possible combination of elements in an array with length i.

For example if ar=[4, 6, 23, 10]

then array.combination(2).to_a returns [[4,6],[4,23],[4,10],[6,23],[6,10],[23,10]]

So basically you need to find sum in your program and getting sum requires combinations of length two (You need to operands in all possible combination ). Hence you don't start with i=0 or i=1.

Stormvirux
  • 879
  • 3
  • 17
  • 34
0

You can not give it an empty array,so 0 leads to false. If you have 1 element in an array,it is also meaningless. So I guess 2 is a starting point which makes this test meaningful.

Wubin Ouyang
  • 777
  • 1
  • 7
  • 9