Medium

You are given an array of up to four non-negative integers, each less than 256.

Your task is to pack these integers into one number M in the following way:

  • The first element of the array occupies the first 8 bits of M;
  • The second element occupies next 8 bits, and so on.

Return the obtained integer M.

Note: the phrase "first bits of M" refers to the least significant bits of M - the right-most bits of an integer. For further clarification see the following example.

Example

For a = [24, 85, 0], the output should be
arrayPacking(a) = 21784.

An array [24, 85, 0] looks like [00011000, 01010101, 00000000] in binary.
After packing these into one number we get 00000000 01010101 00011000 (spaces are placed for convenience), which equals to 21784.

Input/Output

  • [execution time limit] 0.5 seconds (c)

  • [input] array.integer a

    Guaranteed constraints:
    1 ≤ a.length ≤ 4,
    0 ≤ a[i] < 256.

  • [output] integer

[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 arrayPacking(arr_integer a)
{
	int M=0;

	for(int i=0;i<a.size;i++)
		M+=a.arr[i]<<i*8;

	return M;
}
728x90

'Codesignal' 카테고리의 다른 글

<Codesignal> Mirror Bits  (0) 2020.04.09
<Codesignal> Range Bit Count  (0) 2020.04.07
<Codesignal> Kill K-th Bit  (0) 2020.04.07
<Codesignal> Metro Card  (0) 2020.04.07
<Codesignal> Will You?  (0) 2020.04.07

Easy

Implement the missing code, denoted by ellipses. You may not modify the pre-existing code.

In order to stop the Mad Coder evil genius you need to decipher the encrypted message he sent to his minions. The message contains several numbers that, when typed into a supercomputer, will launch a missile into the sky blocking out the sun, and making all the people on Earth grumpy and sad.

You figured out that some numbers have a modified single digit in their binary representation. More specifically, in the given number n the kth bit from the right was initially set to 0, but its current value might be different. It's now up to you to write a function that will change the kth bit of n back to 0.

Example

  • For n = 37 and k = 3, the output should be
    killKthBit(n, k) = 33.

    3710 = 1001012 ~> 1000012 = 3310.

  • For n = 37 and k = 4, the output should be
    killKthBit(n, k) = 37.

    The 4th bit is 0 already (looks like the Mad Coder forgot to encrypt this number), so the answer is still 37.

Input/Output

  • [execution time limit] 0.5 seconds (cpp)

  • [input] integer n

    Guaranteed constraints:
    0 ≤ n ≤ 2^31 - 1.

  • [input] integer k

    The 1-based index of the changed bit (counting from the right).

    Guaranteed constraints:
    1 ≤ k ≤ 31.

  • [output] integer

[C++] Syntax Tips

// Prints help message to the console
// Returns a string
std::string helloWorld(std::string name) {
    std::cout << "This prints to the console when you Run Tests" << std::endl;
    return "Hello, " + name;
}

더보기

Solution

int killKthBit(int n, int k) {
  return n-(n&(int)pow(2,k-1));
}
728x90

'Codesignal' 카테고리의 다른 글

<Codesignal> Range Bit Count  (0) 2020.04.07
<Codesignal> Array Packing  (0) 2020.04.07
<Codesignal> Metro Card  (0) 2020.04.07
<Codesignal> Will You?  (0) 2020.04.07
<Codesignal> Tennis Set  (0) 2020.04.07

Easy

You just bought a public transit card that allows you to ride the Metro for a certain number of days.

Here is how it works: upon first receiving the card, the system allocates you a 31-day pass, which equals the number of days in January. The second time you pay for the card, your pass is extended by 28 days, i.e. the number of days in February (note that leap years are not considered), and so on. The 13th time you extend the pass, you get 31 days again.

You just ran out of days on the card, and unfortunately you've forgotten how many times your pass has been extended so far. However, you do remember the number of days you were able to ride the Metro during this most recent month. Figure out the number of days by which your pass will now be extended, and return all the options as an array sorted in increasing order.

Example

For lastNumberOfDays = 30, the output should be
metroCard(lastNumberOfDays) = [31].

There are 30 days in April, June, September and November, so the next months to consider are May, July, October or December. All of them have exactly 31 days, which means that you will definitely get a 31-days pass the next time you extend your card.

Input/Output

  • [execution time limit] 0.5 seconds (c)

  • [input] integer lastNumberOfDays

    A positive integer, the number of days for which the card was extended the last time.

    Guaranteed constraints:
    lastNumberOfDays = 28 or lastNumberOfDays = 30 or lastNumberOfDays = 31.

  • [output] array.integer

    • An array of positive integers, the possible number of days for which you will extend your pass. The elements of the array can only be equal to 28, 30 or 31 and must be sorted in increasing order.

[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 metroCard(int lastNumberOfDays)
{
	arr_integer days=alloc_arr_integer(lastNumberOfDays>30?3:1);

	if(days.size==1)
		days.arr[0]=31;
	else
	{
		days.arr[0]=28;
		days.arr[1]=30;
		days.arr[2]=31;
	}

	return days;
}
728x90

'Codesignal' 카테고리의 다른 글

<Codesignal> Array Packing  (0) 2020.04.07
<Codesignal> Kill K-th Bit  (0) 2020.04.07
<Codesignal> Will You?  (0) 2020.04.07
<Codesignal> Tennis Set  (0) 2020.04.07
<Codesignal> Arithmetic Expression  (0) 2020.04.07

Easy

Once Mary heard a famous song, and a line from it stuck in her head. That line was "Will you still love me when I'm no longer young and beautiful?". Mary believes that a person is loved if and only if he/she is both young and beautiful, but this is quite a depressing thought, so she wants to put her belief to the test.

Knowing whether a person is young, beautiful and loved, find out if they contradict Mary's belief.

A person contradicts Mary's belief if one of the following statements is true:

  • they are young and beautiful but not loved;
  • they are loved but not young or not beautiful.

Example

  • For young = true, beautiful = true, and loved = true, the output should be
    willYou(young, beautiful, loved) = false.

    Young and beautiful people are loved according to Mary's belief.

  • For young = true, beautiful = false, and loved = true, the output should be
    willYou(young, beautiful, loved) = true.

    Mary doesn't believe that not beautiful people can be loved.

Input/Output

  • [execution time limit] 0.5 seconds (c)

  • [input] boolean young

  • [input] boolean beautiful

  • [input] boolean loved

  • [output] boolean

    • true if the person contradicts Mary's belief, false otherwise.

[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

bool willYou(bool young,bool beautiful,bool loved)
{
	return loved&&!(young&&beautiful) || !loved&&(young&&beautiful);
}
728x90

'Codesignal' 카테고리의 다른 글

<Codesignal> Kill K-th Bit  (0) 2020.04.07
<Codesignal> Metro Card  (0) 2020.04.07
<Codesignal> Tennis Set  (0) 2020.04.07
<Codesignal> Arithmetic Expression  (0) 2020.04.07
<Codesignal> Is Infinite Process?  (0) 2020.04.07

Easy

In tennis, the winner of a set is based on how many games each player wins. The first player to win 6 games is declared the winner unless their opponent had already won 5 games, in which case the set continues until one of the players has won 7 games.

Given two integers score1 and score2, your task is to determine if it is possible for a tennis set to be finished with a final score of score1 : score2.

Example

  • For score1 = 3 and score2 = 6, the output should be
    tennisSet(score1, score2) = true.

    Since player 1 hadn't reached 5 wins, the set ends once player 2 has won 6 games.

  • For score1 = 8 and score2 = 5, the output should be
    tennisSet(score1, score2) = false.

    Since both players won at least 5 games, the set would've ended once one of them won the 7th one.

  • For score1 = 6 and score2 = 5, the output should be
    tennisSet(score1, score2) = false.

    This set will continue until one of these players wins their 7th game, so this can't be the final score.

Input/Output

  • [execution time limit] 0.5 seconds (c)

  • [input] integer score1

    Number of games won by the 1st player, non-negative integer.

    Guaranteed constraints:
    0 ≤ score1 ≤ 10.

  • [input] integer score2

    Number of games won by the 2nd player, non-negative integer.

    Guaranteed constraints:
    0 ≤ score2 ≤ 10.

  • [output] boolean

    • true if score1 : score2 represents a possible score for an ended set, false otherwise.

[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

bool tennisSet(int score1,int score2)
{
	return score1==6&&score2<5||score2==6&&score1<5||score1==7&&score2>4&&score1>score2||score2==7&&score1>4&&score2>score1;
}
728x90

'Codesignal' 카테고리의 다른 글

<Codesignal> Metro Card  (0) 2020.04.07
<Codesignal> Will You?  (0) 2020.04.07
<Codesignal> Arithmetic Expression  (0) 2020.04.07
<Codesignal> Is Infinite Process?  (0) 2020.04.07
<Codesignal> Extra Number  (0) 2020.04.07

Easy

Consider an arithmetic expression of the form a#b=c. Check whether it is possible to replace # with one of the four signs: +, -, * or / to obtain a correct expression.

Example

  • For a = 2, b = 3, and c = 5, the output should be
    arithmeticExpression(a, b, c) = true.

    We can replace # with a + to obtain 2 + 3 = 5, so the answer is true.

  • For a = 8, b = 2, and c = 4, the output should be
    arithmeticExpression(a, b, c) = true.

    We can replace # with a / to obtain 8 / 2 = 4, so the answer is true.

  • For a = 8, b = 3, and c = 2, the output should be
    arithmeticExpression(a, b, c) = false.

    • 8 + 3 = 11 ≠ 2;
    • 8 - 3 = 5 ≠ 2;
    • 8 * 3 = 24 ≠ 2;
    • 8 / 3 = 2.(6) ≠ 2.

    So the answer is false.

Input/Output

  • [execution time limit] 0.5 seconds (c)

  • [input] integer a

    Guaranteed constraints:
    1 ≤ a ≤ 20.

  • [input] integer b

    Guaranteed constraints:
    1 ≤ b ≤ 20.

  • [input] integer c

    Guaranteed constraints:
    0 ≤ c ≤ 20.

  • [output] boolean

    • true if the desired replacement is possible, false otherwise.

[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

bool arithmeticExpression(int a,int b,int c)
{
	return a+b==c||a-b==c||a*b==c||a/b==c&&a%b==0;
}
728x90

'Codesignal' 카테고리의 다른 글

<Codesignal> Will You?  (0) 2020.04.07
<Codesignal> Tennis Set  (0) 2020.04.07
<Codesignal> Is Infinite Process?  (0) 2020.04.07
<Codesignal> Extra Number  (0) 2020.04.07
<Codesignal> Reach Next Level  (0) 2020.04.07

Easy

Given integers a and b, determine whether the following pseudocode results in an infinite loop

while a is not equal to b do

       increase a by 1                  

       decrease b by 1                 

Assume that the program is executed on a virtual machine which can store arbitrary long numbers and execute forever.

Example

  • For a = 2 and b = 6, the output should be
    isInfiniteProcess(a, b) = false;
  • For a = 2 and b = 3, the output should be
    isInfiniteProcess(a, b) = true.

Input/Output

  • [execution time limit] 0.5 seconds (c)

  • [input] integer a

    Guaranteed constraints:
    0 ≤ a ≤ 20.

  • [input] integer b

    Guaranteed constraints:
    0 ≤ b ≤ 20.

  • [output] boolean

    • true if the pseudocode will never stop, false otherwise.

[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

bool isInfiniteProcess(int a,int b)
{
	return a>b||abs(a-b)%2==1;
}
728x90

'Codesignal' 카테고리의 다른 글

<Codesignal> Tennis Set  (0) 2020.04.07
<Codesignal> Arithmetic Expression  (0) 2020.04.07
<Codesignal> Extra Number  (0) 2020.04.07
<Codesignal> Reach Next Level  (0) 2020.04.07
<Codesignal> Phone Call  (0) 2020.04.07

Easy

You're given three integers, a, b and c. It is guaranteed that two of these integers are equal to each other. What is the value of the third integer?

Example

For a = 2, b = 7, and c = 2, the output should be
extraNumber(a, b, c) = 7.

The two equal numbers are a and c. The third number (b) equals 7, which is the answer.

Input/Output

  • [execution time limit] 0.5 seconds (c)

  • [input] integer a

    Guaranteed constraints:
    1 ≤ a ≤ 10^9.

  • [input] integer b

    Guaranteed constraints:
    1 ≤ b ≤ 10^9.

  • [input] integer c

    Guaranteed constraints:
    1 ≤ c ≤ 10^9.

  • [output] integer

[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

int extraNumber(int a,int b,int c)
{
	return a==b&&a!=c?c:a==c&&a!=b?b:a;
}
728x90

'Codesignal' 카테고리의 다른 글

<Codesignal> Arithmetic Expression  (0) 2020.04.07
<Codesignal> Is Infinite Process?  (0) 2020.04.07
<Codesignal> Reach Next Level  (0) 2020.04.07
<Codesignal> Phone Call  (0) 2020.04.07
<Codesignal> Late Ride  (0) 2020.04.07

+ Recent posts