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

 

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

Write a method that finds the total of all the values in an integer array, but each value is either added to the total or subtracted from the total based on a Boolean array. If the i-th position in the Boolean array is true, then add the i-th number in the integer array to the total. However, if the i-th position in the Boolean array is false, subtract the i-th number in the integer array from the total. As a pre-condition, you may assume that the length of the integer array is the same as the length of the Boolean array. Both arrays WILL be the same length, but they could otherwise have any length, including a length of zero. Look at the test data for examples.


dec18_2017_APSLHL_addOrSubtract([4], [true]) → 4
dec18_2017_APSLHL_addOrSubtract([5, 7, 3, 2], [true, true, false, true]) → 11
dec18_2017_APSLHL_addOrSubtract([5, 7, 3, 2], [false, false, false, false]) → -17

...Save, Compile, Run (ctrl-enter)

public int dec18_2017_APSLHL_addOrSubtract(int[] nums, boolean[] add) { }

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