PROBLEM STATEMENT
You work at a plant that manufactures pencils. There is an ideal pencil length
that maximizes the chances the pencil will break, forcing customers to buy a
new pencil. Your company aims to make all pencils precisely that length.
However, some do-gooder has been messing with your machinery, making pencils
that deviate from the spec. You want to determine how close you have come to
the spec in the past month. To do this you first calculate deviations as the
difference between the ideal length, and the actual length of each pencil. The
values are sometimes negative, as some pencils are longer than the ideal
length, and some are shorter. However, you don't care about this, you just
want to know how close you have gotten to the spec.
Your task is to write a method that takes a int[] of deviations and calculates
which of these deviates the least (is closest to 0).
Your method should return the magnitude (absolute value) of the deviation.
DEFINITION
Class: Pencil
Parameters: int[]
Returns: int
Method signature (be sure your method is public): int bestPencil(int[]
deviations);
TopCoder will ensure the validity of the inputs. Inputs are valid if all of
the following criteria are met:
- each element of deviations is in the range -1000 to 1000 (inclusive).
- deviations contains between 1 and 50 elements (inclusive).
EXAMPLES
deviations = {-5,-2,-1,6,7}
-1 is closest to 0, so the method should return 1.
deviations = {0,1,6,7}
0 is closest to 0, so the method should return 0.
deviations = {-2,-1,1,6,7}
-1 and 1 are both 1 away from 0, so we return 1.