Problem Statement
Cat Taro likes to play with his cat friends. Each of his friends currently sits on some coordinate on a straight line that goes from the left to the right. When Taro gives a signal, each of his friends must move exactly X units to the left or to the right.
You are given an int[] coordinates and the int X. For each i, the element coordinates[i] represents the coordinate of the i-th cat before the movement. Return the smallest possible difference between the positions of the leftmost cat and the rightmost cat after the movement.
Definition
- Class:
- TaroFriends
- Method:
- getNumber
- Parameters:
- int[], int
- Returns:
- int
- Method signature:
- int getNumber(int[] coordinates, int X)
- (be sure your method is public)
Constraints
- coordinates will contain between 1 and 50 elements, inclusive.
- Each element of coordinates will be between -100,000,000 and 100,000,000, inclusive.
- X will be between 0 and 100,000,000, inclusive.
Examples
{-3, 0, 1}3
Returns: 3
The difference 3 is obtained if the cats move from {-3,0,1} to {0,-3,-2}.{4, 7, -7}5
Returns: 4
The difference 4 is obtained if the cats move from {4,7,-7} to {-1,2,-2}.{-100000000, 100000000}100000000
Returns: 0
{3, 7, 4, 6, -10, 7, 10, 9, -5}7
Returns: 7
{-4, 0, 4, 0}4
Returns: 4
{7}0
Returns: 0