collapse
Category: Programming
Author: Stuart Reges
Book Chapter: 7.2
Problem: collapse
Write a static method collapse that takes an array of integers as an argument and that returns a new array that contains the result of collapsing the original list by replacing each successive pair of integers with the sum of the pair. For example, if a variable called "list" stores this sequence of values: (7, 2, 8, 9, 4, 13, 7, 1, 9, 10) Then the following call: collapse(list); Should return a new array containing the following values: (9, 17, 17, 8, 19) The first pair from the original list is collapsed into 9 (7 + 2), the second pair is collapsed into 17 (8 + 9), the third pair is collapsed into 17 (4 + 13) and so on. If the list stores an odd number of elements, the final element is not collapsed. For example, if the list had been: (1, 2, 3, 4, 5) Then the call on collapse would produce the following list: (3, 7, 5) with the 5 at the end of the list unchanged. Keep in mind that your method is to return a new array of appropriate length that you construct. Your method should not change the array that is passed as a parameter. Write your solution to collapse below.