Welcome to the Treehouse Community

Want to collaborate on code errors? Have bugs you need feedback on? Looking for an extra set of eyes on your latest project? Get support with fellow developers, designers, and programmers of all backgrounds and skill levels here with the Treehouse Community! While you're at it, check out some resources Treehouse students have shared here.

Looking to learn something new?

Treehouse offers a seven day free trial for new students. Get access to thousands of hours of content and join thousands of Treehouse students and alumni in the community today.

Start your free trial

Java

Tough Sample Problem for Java

Hey guys;

I've run into a sample problem that I'm really interested in solving, but to no avail. Me and my friends were trying to solve it for a few days but still can't seem to get it. I was wondering if someone in the community could help.

The question:

" Zero That Out

Problem Description

Your boss has asked you to add up a sequence of positive numbers to determine how much money your company made last year.

Unfortunately, your boss reads out numbers incorrectly from time to time.

Fortunately, your boss realizes when an incorrect number is read and says “zero”, meaning “ignore the current last number.”

Unfortunately, your boss can make repeated mistakes, and says “zero” for each mistake.

For example, your boss may say “One, three, five, four, zero, zero, seven, zero, zero, six”, which means the total is 7 as explained in the following chart:

Boss statement(s) Current numbers Explanation “One, three, five, four” “zero, zero“ “seven” “zero, zero” “six” 1, 3, 5, 4 1, 3 1, 3, 7 1 1, 6 Record the first four numbers. Ignore the last two numbers. Record the number 7 at the end of our list. Ignore the last two numbers. We have read all numbers, and the total is 7. At any point, your boss will have said at least as many positive numbers as “zero” statements. If all positive numbers have been ignored, the sum is zero.

Write a program that reads the sequence of boss statements and computes the correct sum.

Input Specification

The first line of input contains the integer K (1 ≤ K ≤ 100 000) which is the number of integers (including “zero”) your boss will say. On each of the next K lines, there will either be one integer between 1 and 100 (inclusive), or the integer 0.

Output Specification

The output is one line, containing the integer which is the correct sum of the integers read, taking the “zero” statements into consideration. You can assume that the output will be an integer in the range 0 and 1 000 000 (inclusive).

Sample Input 1 4 3 0 4 0

Output for Sample Input 1

0

Sample Input 2

10

1

3

5

4

0

0

7

0

0

6

Output for Sample Input 2

7 "

Thanks so much guys!

Nick

1 Answer

Hi Nicholas,

I'm not sure of the exact code in Java, but could you not just have an array of your original numbers, then run them through a foreach statement and put them one by one into a new array except if it's a zero, then just remove the last item from the array. Once you have your final second array, just add up all the numbers in the array to get your final answer.

Here's my solution in JavaScript. I'm sure there is a more elegant way of doing it, but it works. :)

Hey Ryan,

Thanks for the answer, but I think the answer needs to solely be in Java, rather than JavaScript, but this is an interesting yet simple way to go about it :)

Nick