Easy

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:

Input/Output

  • [execution time limit] 0.5 seconds (c)

  • [input] array.integer inputArray

    Non-empty array of positive integers.

    Guaranteed constraints:
    2 ≤ inputArray.length ≤ 1000,
    1 ≤ inputArray[i] ≤ 1000.

  • [output] integer

    • The desired length.

[C] Syntax Tips

// Prints help message to the console
// Returns a string
char * helloWorld(char * name) {
    char * answer = malloc(strlen(name) + 8);
    printf("This prints to the console when you Run Tests");
    strcpy(answer, "Hello, ");
    strcat(answer, name);
    return answer;
}

더보기

Solution

// Arrays are already defined with this interface:
// typedef struct arr_##name {
//   int size;
//   type *arr;
// } arr_##name;
//
// arr_##name alloc_arr_##name(int len) {
//   arr_##name a = {len, len > 0 ? malloc(sizeof(type) * len) : NULL};
//   return a;
// }
//
//
int avoidObstacles(arr_integer inputArray)
{
	int max=0, N=1;

	for(int i=0;i<inputArray.size;i++)
		max=inputArray.arr[i]>max?inputArray.arr[i]:max;

	while(N++)
	{
		bool noObstacle=true;
		for(int i=0;i<=max;i+=N)
		{
			for(int j=0;j<inputArray.size;j++)
				if(i==inputArray.arr[j])
				{
					noObstacle=false;
					break;
				}
			if(!noObstacle)
				break;
		}
		if(noObstacle)
			return N;
	}
}
728x90

'Codesignal' 카테고리의 다른 글

<Codesignal> Minesweeper  (0) 2020.04.05
<Codesignal> Box Blur  (0) 2020.04.05
<Codesignal> isIPv4Address  (0) 2020.04.05
<Codesignal> arrayMaximalAdjacentDifference  (0) 2020.04.05
<Codesignal> areEquallyStrong  (0) 2020.04.05

+ Recent posts