Connection Status:
Competition Arena > TaroFriends
SRM 613 · 2013-12-22 · by Witaliy · Brute Force
Class Name: TaroFriends
Return Type: int
Method Name: getNumber
Arg Types: (vector<int>, int)
Problem Statement

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.

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
0)
{-3, 0, 1}
3
Returns: 3

The difference 3 is obtained if the cats move from {-3,0,1} to {0,-3,-2}.

1)
{4, 7, -7}
5
Returns: 4

The difference 4 is obtained if the cats move from {4,7,-7} to {-1,2,-2}.

2)
{-100000000, 100000000}
100000000
Returns: 0
3)
{3, 7, 4, 6, -10, 7, 10, 9, -5}
7
Returns: 7
4)
{-4, 0, 4, 0}
4
Returns: 4

Submissions are judged against all 244 archived test cases, of which 5 are shown here. Case numbers match the judge’s.

Coding Area

Language: C++17 · define a public class TaroFriends with a public method int getNumber(vector<int> coordinates, int X) · 244 test cases · 2 s / 256 MB per case

Submitting as anonymous