sets/lib/sets/mighty_set.rb
def sum_items(array)
      result_array = Array.new

      index = 0
      loop do
        break if index >= array.size

        # Number should be negative
        # Otherwise, just store it; it could be used to calculate
        # the greatest sum
        number = array[index]
        if number >= 0

          result_array << number

          index += 1
          next
        end

        # We know that the number is negative
        negative_number = number

        # Search the subset of numbers to the left
        previous_sum = 0
        found_previous_sum = false
        result_array.reverse.each do |previous_number|
          break if previous_number < 0

          previous_sum += previous_number
          if previous_sum >= -negative_number
            found_previous_sum = true
            break
          end
        end

        # Numbers to the left do not satisfy condition
        unless found_previous_sum
          result_array << negative_number

          index += 1
          next
        end

        # Search the subset of numbers to the right
        sum = 0
        found_sum = false
        (index+1..array.size-1).each do |look_ahead_index|
          next_number = array[look_ahead_index]
          sum += next_number

          if sum >= -negative_number
            # For the next iteration, discard the numbers
            # that have already been counted and aggregated
            found_sum = true
            index = look_ahead_index + 1
            result_array << negative_number + sum

            break
          end
        end

        # Number to the right do not satisfy condition
        unless found_sum
          result_array << negative_number

          index += 1
          next
        end
      end

      # Return the input array if all items that could be aggregated have been,
      # and the array cannot be simplified further
      simplified_result_array = simplify_array(result_array)
      array == result_array ? array : sum_items(simplified_result_array)
    end
# Recursive function
# Find a negative number surrounded by two positive numbers,
# one on the left (number1) and one on the right (number2)
# Then replace this trio and the following subsets IF
# The absolute value of the sum of a contiguous subset
# of numbers (starting from number1) to the left is greater than
# the absolute value of the negative number
# AND
# the absolute value of a contiguous subset of numbers (starting
# from number2) to the right is greater than the absolute
# value of the negative number
Recursive function
Find a negative number surrounded by two positive numbers,
one on the left (number1) and one on the right (number2)
Then replace this trio and the following subsets IF
The absolute value of the sum of a contiguous subset
of numbers (starting from number1) to the left is greater than
the absolute value of the negative number
AND
the absolute value of a contiguous subset of numbers (starting
from number2) to the right is greater than the absolute
value of the negative number
Recursive function Find a negative number surrounded by two positive numbers one on the left ( number1 ) and one on the right ( number2 ) Then replace this trio and the following subsets IF The absolute value of the sum of a contiguous subset of numbers ( starting from number1 ) to the left is greater than the absolute value of the negative number AND the absolute value of a contiguous subset of numbers ( starting from number2 ) to the right is greater than the absolute value of the negative number
def sum_items(array)
      result_array = Array.new
      index = 0
      loop do
        break if index >= array.size
        number = array[index]
        if number >= 0
          result_array << number
          index += 1
          next
        end
        negative_number = number
        previous_sum = 0
        found_previous_sum = false
        result_array.reverse.each do |previous_number|
          break if previous_number < 0
          previous_sum += previous_number
          if previous_sum >= -negative_number
            found_previous_sum = true
            break
          end
        end
        unless found_previous_sum
          result_array << negative_number
          index += 1
          next
        end
        sum = 0
        found_sum = false
        (index+1..array.size-1).each do |look_ahead_index|
          next_number = array[look_ahead_index]
          sum += next_number
          if sum >= -negative_number
            found_sum = true
            index = look_ahead_index + 1
            result_array << negative_number + sum
            break
          end
        end
        unless found_sum
          result_array << negative_number
          index += 1
          next
        end
      end
      simplified_result_array = simplify_array(result_array)
      array == result_array ? array : sum_items(simplified_result_array)
    end
def sum_items ( array ) result_array = Array . new index = 0 loop do break if index >= array . size number = array [ index ] if number >= 0 result_array << number index += 1 next end negative_number = number previous_sum = 0 found_previous_sum = false result_array . reverse . each do | previous_number | break if previous_number < 0 previous_sum += previous_number if previous_sum >= - negative_number found_previous_sum = true break end end unless found_previous_sum result_array << negative_number index += 1 next end sum = 0 found_sum = false ( index + 1 .. array . size - 1 ) . each do | look_ahead_index | next_number = array [ look_ahead_index ] sum += next_number if sum >= - negative_number found_sum = true index = look_ahead_index + 1 result_array << negative_number + sum break end end unless found_sum result_array << negative_number index += 1 next end end simplified_result_array = simplify_array ( result_array ) array == result_array ? array : sum_items ( simplified_result_array ) end
Recursive function
Find a negative number surrounded by two positive numbers,
one on the left (number1) and one on the right (number2)
Then replace this trio and the following subsets IF
The absolute value of the sum of a contiguous subset
of numbers (starting from number1) to the left is greater than
the absolute value of the negative number
AND
the absolute value of a contiguous subset of numbers (starting
from number2) to the right is greater than the absolute
value of the negative number
Recursive function Find a negative number surrounded by two positive numbers one on the left ( number1 ) and one on the right ( number2 ) Then replace this trio and the following subsets IF The absolute value of the sum of a contiguous subset of numbers ( starting from number1 ) to the left is greater than the absolute value of the negative number AND the absolute value of a contiguous subset of numbers ( starting from number2 ) to the right is greater than the absolute value of the negative number
# Number should be negative # Otherwise, just store it; it could be used to calculate # the greatest sum # We know that the number is negative # Search the subset of numbers to the left # Numbers to the left do not satisfy condition # Search the subset of numbers to the right # For the next iteration, discard the numbers # that have already been counted and aggregated # Number to the right do not satisfy condition # Return the input array if all items that could be aggregated have been, # and the array cannot be simplified further
array