logo CodeStepByStep logo

checkSum25

Write a method named checkSum25 that processes a file of integers looking for a value that cannot be formed by summing 2 of the 25 integers before it. Your method accepts a string representing a file name as a parameter where each line contains a single integer. For example, suppose the file named numbers.txt contains the following values (shortened by ...):

1
2
3
...
24
25
26
49
31
80
78
55

If the first 25 numbers are 1..25, then 26 would be a valid next number, as it could be 1 plus 25 (or many other pairs, like 2 and 24). 49 would be a valid next number, as it is the sum of 24 and 25. 31 is a valid next number as it is the sum of 15 and 16 (or other pairs). 80 is a valid next number as it can be formed by 49 + 31. But 78 cannot be formed from any 2 of the prior 25 numbers. So the call of checkSum25("numbers.txt") should return 78.

You may assume that the file exists and is readable, that it follows the format described above, and that there will be at least 26 integers in the file.

(This exercise is based on the Advent of Code 2020, day 9.)

Method: Write a Java method as described, not a complete program or class.

You must log in before you can solve this problem.

Log In

Need help?

Stuck on an exercise? Contact your TA or instructor.

If something seems wrong with our site, please

Is there a problem? Contact us.