Sha256: 6e7d29802778bab18204f6cac0f298697dbe5c98170258f2f4ea1c929c342f3b

Contents?: true

Size: 374 Bytes

Versions: 1

Compression:

Stored size: 374 Bytes

Contents

def maximize_array(array, k)
  # Ordenar o array do menor ao maior
  sort_array = array.sort
  sum = 0
  i = 0

  while k > 0
    if (sort_array[i] >= 0)
      k = 0
    else
      sort_array[i] = (-1) * sort_array[i]
     k -= 1
    end
    i += 1
  end

  sort_array.each do |j|
    sum += j
  end
  
  return sum
  
end

array = [-2,0,5,-1,2]
puts maximize_array(array,4)

Version data entries

1 entries across 1 versions & 1 rubygems

Version Path
algoritmos-0.1.0 lib/MaximizeArray.rb