Medium

There are some people and cats in a house. You are given the number of legs they have all together. Your task is to return an array containing every possible number of people that could be in the house sorted in ascending order. It's guaranteed that each person has 2 legs and each cat has 4 legs.

Example

  • For legs = 6, the output should be
    houseOfCats(legs) = [1, 3].

    There could be either 1 cat and 1 person (4 + 2 = 6) or 3 people (2 * 3 = 6).

  • For legs = 2, the output should be
    houseOfCats(legs) = [1].

    There can be only 1 person.

Input/Output

  • [execution time limit] 0.5 seconds (c)

  • [input] integer legs

    The total number of legs in the house. It's guaranteed,that this number is even.

    Guaranteed constraints:
    0 ≤ legs < 50.

  • [output] array.integer

    • Every possible number of people that can be in the house.

[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;
// }
//
//
arr_integer houseOfCats(int legs)
{
	arr_integer array=alloc_arr_integer(legs/4+1);

	for(int i=0;i<array.size;i++)
		array.arr[i]=(legs%4)/2+2*i;

	return array;
}
728x90

'Codesignal' 카테고리의 다른 글

<Codesignal> Minimal Number of Coins  (0) 2020.05.04
<Codesignal> Alphabet Subsequence  (0) 2020.05.04
<Codesignal> House Numbers Sum  (0) 2020.05.03
<Codesignal> Numbers of Clans  (0) 2020.05.03
<Codesignal> Numbers Grouping  (0) 2020.05.02

+ Recent posts