forked from mirandaio/codingbat
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathsum67.java
More file actions
22 lines (17 loc) · 688 Bytes
/
sum67.java
File metadata and controls
22 lines (17 loc) · 688 Bytes
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
/* Return the sum of the numbers in the array, except ignore sections of
* numbers starting with a 6 and extending to the next 7 (every 6 will be
* followed by at least one 7). Return 0 for no numbers.
*/
public int sum67(int[] nums) {
int sum = 0;
boolean inRange = false;
for(int i = 0; i < nums.length; i++) {
if(nums[i] == 6)
inRange = true;
if(!inRange)
sum += nums[i];
if(inRange && nums[i] == 7)
inRange = false;
}
return sum;
}