Easy

Ratiorg got statues of different sizes as a present from CodeMaster for his birthday, each statue having an non-negative integer size. Since he likes to make things perfect, he wants to arrange them from smallest to largest so that each statue will be bigger than the previous one exactly by 1. He may need some additional statues to be able to accomplish that. Help him figure out the minimum number of additional statues needed.

Example

For statues = [6, 2, 3, 8], the output should be
makeArrayConsecutive2(statues) = 3.

Ratiorg needs statues of sizes 4, 5 and 7.

Input/Output

  • [execution time limit] 0.5 seconds (c)

  • [input] array.integer statues

    An array of distinct non-negative integers.

    Guaranteed constraints:
    1 ≤ statues.length ≤ 10,
    0 ≤ statues[i] ≤ 20.

  • [output] integer

    • The minimal number of statues that need to be added to existing statues such that it contains every integer size from an interval [L, R] (for some L, R) and no other sizes.

[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 makeArrayConsecutive2(arr_integer statues)
{
	int min=21, max=-1;

	for(int i=0;i<statues.size;i++)
	{
		min=statues.arr[i]<min?statues.arr[i]:min;
		max=statues.arr[i]>max?statues.arr[i]:max;
	}

	return max-min+1-statues.size;
}
728x90

'Codesignal' 카테고리의 다른 글

<Codesignal> Is Sum of Consecutive 2  (0) 2020.04.15
<Codesignal> Is Power?  (0) 2020.04.15
<Codesignal> Replace Middle  (0) 2020.04.14
<Codesignal> Is Smooth?  (0) 2020.04.14
<Codesignal> Remove Array Part  (0) 2020.04.14

+ Recent posts