The problem my code solves is listed below. I know my code can be improved, it takes a while to run when bigger arrays are entered as input.
"Problem - Given an array of integers, sum consecutive even numbers and consecutive odd numbers. Repeat the process while it can be done and return the length of the final array."
def sum_groups(arr)
z = arr.size
x = []
i = 0
until i == z
arr = arr.chunk{|x| x.even?}.map{|x, y| y}.map{|x| x.inject(&:+)}
x << arr
i += 1
end
x[-1].size
end
An example input -
For arr = [2, 1, 2, 2, 6, 5, 0, 2, 0, 5, 5, 7, 7, 4, 3, 3, 9]
The result should be 6.
[2, 1, 10, 5, 30, 15] - Value of numbers in final array.
The length of final array is 6
Any review and help would be appreciated to make this code more efficient.