Easy

Given two arrays of integers a and b, obtain the array formed by the elements of a followed by the elements of b.

Example

For a = [2, 2, 1] and b = [10, 11], the output should be
concatenateArrays(a, b) = [2, 2, 1, 10, 11].

Input/Output

  • [execution time limit] 0.5 seconds (c)

  • [input] array.integer a

    Guaranteed constraints:
    1 ≤ a.length ≤ 10,
    1 ≤ a[i] ≤ 15.

  • [input] array.integer b

    Guaranteed constraints:
    0 ≤ b.length ≤ 10,
    1 ≤ b[i] ≤ 15.

  • [output] array.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;
// }
//
//
arr_integer concatenateArrays(arr_integer a,arr_integer b)
{
	arr_integer c=alloc_arr_integer(a.size+b.size);

	for(int i=0;i<c.size;i++)
		c.arr[i]=i<a.size?a.arr[i]:b.arr[i-a.size];

	return c;
}
728x90

'Codesignal' 카테고리의 다른 글

<Codesignal> Is Smooth?  (0) 2020.04.14
<Codesignal> Remove Array Part  (0) 2020.04.14
<Codesignal> First Reverse Try  (0) 2020.04.14
<Codesignal> Create Array  (0) 2020.04.14
<Codesignal> Count Black Cells  (0) 2020.04.14

Easy

Reversing an array can be a tough task, especially for a novice programmer. Mary just started coding, so she would like to start with something basic at first. Instead of reversing the array entirely, she wants to swap just its first and last elements.

Given an array arr, swap its first and last elements and return the resulting array.

Example

For arr = [1, 2, 3, 4, 5], the output should be
firstReverseTry(arr) = [5, 2, 3, 4, 1].

Input/Output

  • [execution time limit] 0.5 seconds (c)

  • [input] array.integer arr

    Guaranteed constraints:
    0 ≤ arr.length ≤ 50,
    -10^4 ≤ arr[i] ≤ 10^4.

  • [output] array.integer

    • Array arr with its first and its last elements swapped.

[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 firstReverseTry(arr_integer arr)
{
	if(arr.size==0)
		return arr;

	int temp=arr.arr[0];
	arr.arr[0]=arr.arr[arr.size-1];
	arr.arr[arr.size-1]=temp;

	return arr;
}
728x90

'Codesignal' 카테고리의 다른 글

<Codesignal> Remove Array Part  (0) 2020.04.14
<Codesignal> Concatenate Arrays  (0) 2020.04.14
<Codesignal> Create Array  (0) 2020.04.14
<Codesignal> Count Black Cells  (0) 2020.04.14
<Codesignal> Candles  (0) 2020.04.13

Easy

Given an integer size, return array of length size filled with 1s.

Example

For size = 4, the output should be
createArray(size) = [1, 1, 1, 1].

Input/Output

  • [execution time limit] 0.5 seconds (c)

  • [input] integer size

    A positive integer.

    Guaranteed constraints:
    1 ≤ size ≤ 1000.

  • [output] array.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;
// }
//
//
arr_integer createArray(int size)
{
	arr_integer returnArray=alloc_arr_integer(size);

	for(int i=0;i<size;i++)
		returnArray.arr[i]=1;

	return returnArray;
}
728x90

'Codesignal' 카테고리의 다른 글

<Codesignal> Concatenate Arrays  (0) 2020.04.14
<Codesignal> First Reverse Try  (0) 2020.04.14
<Codesignal> Count Black Cells  (0) 2020.04.14
<Codesignal> Candles  (0) 2020.04.13
<Codesignal> Rounders  (0) 2020.04.13

Medium

Imagine a white rectangular grid of n rows and m columns divided into two parts by a diagonal line running from the upper left to the lower right corner. Now let's paint the grid in two colors according to the following rules:

  • A cell is painted black if it has at least one point in common with the diagonal;
  • Otherwise, a cell is painted white.

Count the number of cells painted black.

Example

  • For n = 3 and m = 4, the output should be
    countBlackCells(n, m) = 6.

    There are 6 cells that have at least one common point with the diagonal and therefore are painted black.

  • For n = 3 and m = 3, the output should be
    countBlackCells(n, m) = 7.

    7 cells have at least one common point with the diagonal and are painted black.

Input/Output

  • [execution time limit] 0.5 seconds (c)

  • [input] integer n

    The number of rows.

    Guaranteed constraints:
    1 ≤ n ≤ 10^5.

  • [input] integer m

    The number of columns.

    Guaranteed constraints:
    1 ≤ m ≤ 10^5.

  • [output] integer

    • The number of black cells.

[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 gcd(int x,int y)
{
	if(x<y)
	{
		int temp=x;
		x=y;
		y=temp;
	}

	while(y>0)
	{
		int temp=x%y;
		x=y;
		y=temp;
	}

	return x;
}

int countBlackCells(int n,int m)
{
	return n+m+gcd(n,m)-2;
}
728x90

'Codesignal' 카테고리의 다른 글

<Codesignal> First Reverse Try  (0) 2020.04.14
<Codesignal> Create Array  (0) 2020.04.14
<Codesignal> Candles  (0) 2020.04.13
<Codesignal> Rounders  (0) 2020.04.13
<Codesignal> Increase Number Roundness  (0) 2020.04.12

Easy

When a candle finishes burning it leaves a leftover. makeNew leftovers can be combined to make a new candle, which, when burning down, will in turn leave another leftover.

You have candlesNumber candles in your possession. What's the total number of candles you can burn, assuming that you create new candles as soon as you have enough leftovers?

Example

For candlesNumber = 5 and makeNew = 2, the output should be
candles(candlesNumber, makeNew) = 9.

Here is what you can do to burn 9 candles:

  • burn 5 candles, obtain 5 leftovers;
  • create 2 more candles, using 4 leftovers (1 leftover remains);
  • burn 2 candles, end up with 3 leftovers;
  • create another candle using 2 leftovers (1 leftover remains);
  • burn the created candle, which gives another leftover (2 leftovers in total);
  • create a candle from the remaining leftovers;
  • burn the last candle.

Thus, you can burn 5 + 2 + 1 + 1 = 9 candles, which is the answer.

Input/Output

  • [execution time limit] 0.5 seconds (c)

  • [input] integer candlesNumber

    The number of candles you have in your possession.

    Guaranteed constraints:
    1 ≤ candlesNumber ≤ 15.

  • [input] integer makeNew

    The number of leftovers that you can use up to create a new candle.

    Guaranteed constraints:
    2 ≤ makeNew ≤ 5.

  • [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 candles(int candlesNumber,int makeNew)
{
	int leftover=candlesNumber, count=candlesNumber;

	while(leftover>=makeNew)
	{
		count++;
		leftover-=(makeNew-1);
	}

	return count;
}
728x90

'Codesignal' 카테고리의 다른 글

<Codesignal> Create Array  (0) 2020.04.14
<Codesignal> Count Black Cells  (0) 2020.04.14
<Codesignal> Rounders  (0) 2020.04.13
<Codesignal> Increase Number Roundness  (0) 2020.04.12
<Codesignal> Apple Boxes  (0) 2020.04.12

Easy

We want to turn the given integer into a number that has only one non-zero digit using a tail rounding approach. This means that at each step we take the last non 0 digit of the number and round it to 0 or to 10. If it's less than 5 we round it to 0 if it's larger than or equal to 5 we round it to 10 (rounding to 10 means increasing the next significant digit by 1). The process stops immediately once there is only one non-zero digit left.

Example

  • For n = 15, the output should be
    rounders(n) = 20;

  • For n = 1234, the output should be
    rounders(n) = 1000.

    1234 -> 1230 -> 1200 -> 1000.

  • For n = 1445, the output should be
    rounders(n) = 2000.

    1445 -> 1450 -> 1500 -> 2000.

Input/Output

  • [execution time limit] 0.5 seconds (c)

  • [input] integer n

    A positive integer.

    Guaranteed constraints:
    1 ≤ value ≤ 10^8.

  • [output] integer

    • The rounded number.

[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 rounders(int n)
{
	int N=0;

	while(pow(10,N)<n)
	{
		if((n/(int)pow(10,N))%10>=5 && n/(int)pow(10,N+1)!=0)
			n+=(int)pow(10,N+1);
		n-=n%(int)pow(10,N);
		N++;
	}

	return n;
}
728x90

'Codesignal' 카테고리의 다른 글

<Codesignal> Count Black Cells  (0) 2020.04.14
<Codesignal> Candles  (0) 2020.04.13
<Codesignal> Increase Number Roundness  (0) 2020.04.12
<Codesignal> Apple Boxes  (0) 2020.04.12
<Codesignal> Addition Without Carrying  (0) 2020.04.12

Easy

Define an integer's roundness as the number of trailing zeroes in it.

Given an integer n, check if it's possible to increase n's roundness by swapping some pair of its digits.

Example

  • For n = 902200100, the output should be
    increaseNumberRoundness(n) = true.

    One of the possible ways to increase roundness of n is to swap digit 1 with digit 0 preceding it: roundness of 902201000 is 3, and roundness of n is 2.

    For instance, one may swap the leftmost 0 with 1.

  • For n = 11000, the output should be
    increaseNumberRoundness(n) = false.

    Roundness of n is 3, and there is no way to increase it.

Input/Output

  • [execution time limit] 0.5 seconds (c)

  • [input] integer n

    A positive integer.

    Guaranteed constraints:
    100 ≤ n ≤ 10^9.

  • [output] boolean

    • true if it's possible to increase n's roundness, 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 increaseNumberRoundness(int n)
{
	while(n%10==0)
		n/=10;

	while(n>0)
	{
		if(n%10==0)
			return true;
		n/=10;
	}

	return false;
}
728x90

'Codesignal' 카테고리의 다른 글

<Codesignal> Candles  (0) 2020.04.13
<Codesignal> Rounders  (0) 2020.04.13
<Codesignal> Apple Boxes  (0) 2020.04.12
<Codesignal> Addition Without Carrying  (0) 2020.04.12
<Codesignal> Lineup  (0) 2020.04.12

Easy

You have k apple boxes full of apples. Each square box of size m contains m × m apples. You just noticed two interesting properties about the boxes:

  1. The smallest box is size 1, the next one is size 2,..., all the way up to size k.
  2. Boxes that have an odd size contain only yellow apples. 

          Boxes that have an even size contain only red apples.         

Your task is to calculate the difference between the number of red apples and the number of yellow apples.

Example

For k = 5, the output should be
appleBoxes(k) = -15.

There are 1 + 3 * 3 + 5 * 5 = 35 yellow apples and 2 * 2 + 4 * 4 = 20 red apples, making the answer 20 - 35 = -15.

Input/Output

  • [execution time limit] 0.5 seconds (c)

  • [input] integer k

    A positive integer.

    Guaranteed constraints:
    1 ≤ k ≤ 40.

  • [output] integer

    • The difference between the two types of apples.

[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 appleBoxes(int k)
{
	int apple=0;

	for(int i=1;i<=k;i++)
		apple+=i%2==0?i*i:-i*i;

	return apple;
}
728x90

'Codesignal' 카테고리의 다른 글

<Codesignal> Rounders  (0) 2020.04.13
<Codesignal> Increase Number Roundness  (0) 2020.04.12
<Codesignal> Addition Without Carrying  (0) 2020.04.12
<Codesignal> Lineup  (0) 2020.04.12
<Codesignal> Magical Well  (0) 2020.04.12

+ Recent posts