id/email
password
forgot password | create account
about | help | code help+videos | done | prefs
CodingBat code practice

 

jebbert@volusia.k12.fl.us > oct29SLHLsumAlternating
prev  |  next  |  chance

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)

public int oct29SLHLsumAlternating(int[] nums) { }

Editor font size %:
Shorter output


Forget It! -- delete my code for this problem

Progress graphs:
 Your progress graph for this problem
 Random user progress graph for this problem
 Random Epic Progress Graph

Java Help

Misc Code Practice

Difficulty: 290

Copyright Nick Parlante 2017 - privacy