반응형

You are given an array of integers representing coordinates of obstacles situated on a straight line.


Assume that you are jumping from the point with coordinate 0 to the right. You are allowed only to make jumps of the same length represented by some integer.


Find the minimal length of the jump enough to avoid all the obstacles.


Example


For inputArray = [5, 3, 6, 7, 9], the output should be

avoidObstacles(inputArray) = 4.


Check out the image below for better understanding:



code>>


int avoidObstacles(std::vector<int> inputArray) {

std::sort(inputArray.begin(), inputArray.end());

int jump = 2;

for (int i = 0; i < inputArray.size(); i++)

{

for (int j = 0; j < inputArray.size(); j++)

{

if (!(inputArray[j] % jump)) jump++;

}

}

return jump;

}

반응형

'알고리즘 > codefights' 카테고리의 다른 글

24>minesweeper  (0) 2017.07.06
23>boxBlur  (0) 2017.07.05
21>isIPv4Address  (0) 2017.07.04
20>arrayMaximalAdjacentDifference  (0) 2017.07.04
19>areEquallyStrong  (0) 2017.07.02

+ Recent posts