Question: Given a set of four numbers representing a circular array we can test to see if the absolute values of the differences between each adjoining
Given a set of four numbers representing a circular array we can test to see if the absolute values of the differences between each adjoining element will eventually coalesce into a single value.
For example, consider the set of four numbers: {1, 10, 8, 3}.
We can repeatedly take the absolute values of the differences of the adjoining numbers. Since this is a circular array, calculate the value of the last element in the new array as the last number minus the first number.
For example, starting with {1, 10, 8, 3}
The first iteration will produce {9, 2, 5, 2} calculated as |1 - 10|, |10 - 8|, |8 - 3|, |3 - 1| . The second iteration will produce {7, 3, 3, 7} calculated as |9 - 2|, |2 - 5|, |5 - 2|, |2 - 9|. The third iteration will produce {4, 0, 4, 0} calculated as |7 - 3|, |3 3|, |3 - 7|, |7 7|. The fourth iteration will produce {4, 4, 4, 4} calculated as |4 - 0|, |0 - 4|, |4 - 0|, |0 - 4|.
We can say the original set {1, 10, 8, 3} required 4 iterations to reach a common number (all values are 4).
In this problem you must read multiple test cases from a text file named numbers.txt.
Each line in this file will contain exactly 4 numbers separated by a single space.
Each number will be >= 0 and <= 10000.
For each set of 4 numbers, determine how many iterations are required before the sequence of the differences coalesces into a single value.
Although there are 7 lines in the test file below, do NOT assume all test files will have 7 lines of input. The files used by the judges may have more or fewer lines.
Test Data:
| Input (numbers.txt) | Output |
|---|---|
| 1 0 0 0 | 3 iterations were required for 1 0 0 0 |
| 0 1 4 11 | 5 iterations were required for 0 1 4 11 |
| 0 0 0 2 | 3 iterations were required for 0 0 0 2 |
| 34 12 19 28 | 4 iterations were required for 34 12 19 28 |
| 100 200 321 345 | 4 iterations were required for 100 200 321 345 |
| 7 2 7 2 | 1 iterations were required for 7 2 7 2 |
| 9934 8543 812 1001 | 3 iterations were required for 9934 8543 812 1001 |
Step by Step Solution
There are 3 Steps involved in it
Get step-by-step solutions from verified subject matter experts
