2

Is the space complexity of this algorithm is O(n^3) cubic space complexity ? As: list is an array, subs is an array + one array created with map.

Is the time complexity is O(n^4) biquadratic? As: two times loop on list + one time map + one time sum

def largest_contiguous_subsum(list)
  subs = []

  list.each_index do |idx1|
    (idx1..list.length - 1).each do |idx2|
      subs << list[idx1..idx2]
    end
  end

  subs.map(&:sum).max
end

I am currently trying to understand Big O concept

Daria
  • 29
  • 1
  • 1
    No, it's still quadratic: you have 2 nested loops - 1st time is `each` inside `each_index`, 2nd time is `sum` inside `map` - and their complexities should be added, not multiplied. – Konstantin Strukov Dec 29 '22 at 12:49

0 Answers0