about | help | code help+videos | done | prefs |
Write a method that returns the "alternating sum" of an array of integers. The "alternating sum" means that you only continue adding numbers as long as the numbers go in the pattern up-down-up-down-up-down-etc. For example, {5,7,3,6,8,10} produces the sum of 21 because you only add 5+7+3+6 since the 8 breaks the up-down pattern (the 8 should have been less than 6 to continue the pattern). As a pre-condition, you may assume that the array of integers will contain at least two integers. Also, the pattern must start with an 'up', so the array {5,3,7,2} would only return 5 since the pattern is broken when you go DOWN from 5 to 3. Look at the test data for additional insight. oct29SLHLsumAlternating([-15, 2, -3, 15]) → -1 oct29SLHLsumAlternating([3, 20, 19, 40, 2, 5]) → 89 oct29SLHLsumAlternating([7, 7, 7, 7]) → 7 ...Save, Compile, Run (ctrl-enter) |
Progress graphs:
Your progress graph for this problem
Random user progress graph for this problem
Random Epic Progress Graph
Difficulty: 290
Copyright Nick Parlante 2017 - privacy