Question: Write a method named stretch that accepts as a parameter an ArrayList of integers, and modifies it to be twice as large, replacing every integer

Write a method named stretch that accepts as a parameter an ArrayList of integers, and modifies it to be twice as large, replacing every integer with a pair of integers, each half the original. If a number in the original list is odd, then the first number in the new pair should be one higher than the second so that the sum equals the original number. For example, if a variable named v refers to a list storing the values {18, 7, 4, 24, 11}, the call of stretch(v); should change v to contain {9, 9, 4, 3, 2, 2, 12, 12, 6, 5}. (The number 18 is stretched into the pair 9, 9, the number 7 is stretched into 4, 3, the number 4 is stretched into 2, 2, the number 24 is stretched into 12, 12, and 11 is stretched into 6, 5.)

Make sure to just write one method, not a whole class.

Step by Step Solution

There are 3 Steps involved in it

1 Expert Approved Answer
Step: 1 Unlock blur-text-image
Question Has Been Solved by an Expert!

Get step-by-step solutions from verified subject matter experts

Step: 2 Unlock
Step: 3 Unlock

Students Have Also Explored These Related Databases Questions!