Medium

You work in a company that prints and publishes books. You are responsible for designing the page numbering mechanism in the printer. You know how many digits a printer can print with the leftover ink. Now you want to write a function to determine what the last page of the book is that you can number given the current page and numberOfDigits left. A page is considered numbered if it has the full number printed on it (e.g. if we are working with page 102 but have ink only for two digits then this page will not be considered numbered).

It's guaranteed that you can number the current page, and that you can't number the last one in the book.

Example

  • For current = 1 and numberOfDigits = 5, the output should be
    pagesNumberingWithInk(current, numberOfDigits) = 5.

    The following numbers will be printed: 1, 2, 3, 4, 5.

  • For current = 21 and numberOfDigits = 5, the output should be
    pagesNumberingWithInk(current, numberOfDigits) = 22.

    The following numbers will be printed: 21, 22.

  • For current = 8 and numberOfDigits = 4, the output should be
    pagesNumberingWithInk(current, numberOfDigits) = 10.

    The following numbers will be printed: 8, 9, 10.

Input/Output

  • [execution time limit] 0.5 seconds (c)

  • [input] integer current

    A positive integer, the number on the current page which is not yet printed.

    Guaranteed constraints:
    1 ≤ current ≤ 1000.

  • [input] integer numberOfDigits

    A positive integer, the number of digits which your printer can print.

    Guaranteed constraints:
    1 ≤ numberOfDigits ≤ 1000.

  • [output] integer

    • The last printed page 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 pagesNumberingWithInk(int current,int numberOfDigits)
{
	int digit=current<10?1:current<100?2:current<1000?3:4;

	while(digit<=numberOfDigits)
	{
		numberOfDigits-=digit;
		current++;
		digit=current<10?1:current<100?2:current<1000?3:4;
	}

	return current-1;
}
728x90

'Codesignal' 카테고리의 다른 글

<Codesignal> Weak Numbers  (0) 2020.04.18
<Codesignal> Comfortable Numbers  (0) 2020.04.17
<Codesignal> Square Digits Sequence  (0) 2020.04.15
<Codesignal> Is Sum of Consecutive 2  (0) 2020.04.15
<Codesignal> Is Power?  (0) 2020.04.15

Medium

Consider a sequence of numbers a0, a1, ..., an, in which an element is equal to the sum of squared digits of the previous element. The sequence ends once an element that has already been in the sequence appears again.

Given the first element a0, find the length of the sequence.

Example

  • For a0 = 16, the output should be
    squareDigitsSequence(a0) = 9.

    Here's how elements of the sequence are constructed:

    • a0 = 16
    • a1 = 1^2 + 6^2 = 37
    • a2 = 3^2 + 7^2 = 58
    • a3 = 5^2 + 8^2 = 89
    • a4 = 8^2 + 9^2 = 145
    • a5 = 1^2 + 4^2 + 5^2 = 42
    • a6 = 4^2 + 2^2 = 20
    • a7 = 2^2 + 0^2 = 4
    • a8 = 4^2 = 16, which has already occurred before (a0)

    Thus, there are 9 elements in the sequence.

  • For a0 = 103, the output should be
    squareDigitsSequence(a0) = 4.

    The sequence goes as follows: 103 -> 10 -> 1 -> 1, 4 elements altogether.

Input/Output

  • [execution time limit] 0.5 seconds (c)

  • [input] integer a0

    First element of a sequence, positive integer.

    Guaranteed constraints:
    1 ≤ a0 ≤ 10^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 squareDigitsSequence(int a0)
{
	int count=0, temp=a0, max=a0;
	bool existed[100000]={false, };

	do
	{
		int N=0;

		existed[temp]=true;
		while(temp>0)
		{
			N+=(temp%10)*(temp%10);
			temp/=10;
		}

		temp=N;
		max=max>temp?max:temp;
	}
	while(!existed[temp]);

	for(int i=0;i<=max;i++)
		count+=existed[i];

	return count+1;
}
728x90

'Codesignal' 카테고리의 다른 글

<Codesignal> Comfortable Numbers  (0) 2020.04.17
<Codesignal> Pages Numbering With Ink  (0) 2020.04.16
<Codesignal> Is Sum of Consecutive 2  (0) 2020.04.15
<Codesignal> Is Power?  (0) 2020.04.15
<Codesignal> Make Array Consecutive 2  (0) 2020.04.15

Medium

Find the number of ways to express n as sum of some (at least two) consecutive positive integers.

Example

  • For n = 9, the output should be
    isSumOfConsecutive2(n) = 2.

    There are two ways to represent n = 9: 2 + 3 + 4 = 9 and 4 + 5 = 9.

  • For n = 8, the output should be
    isSumOfConsecutive2(n) = 0.

    There are no ways to represent n = 8.

Input/Output

  • [execution time limit] 0.5 seconds (c)

  • [input] integer n

    A positive integer.

    Guaranteed constraints:
    1 ≤ n ≤ 10^4.

  • [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 isSumOfConsecutive2(int n)
{
	int count=0;

	for(int i=1;i<=n/2;i++)
	{
		int sum=0;

		for(int j=i;sum<n;j++)
			sum+=j;
		count+=sum==n;
	}

	return count;
}
728x90

'Codesignal' 카테고리의 다른 글

<Codesignal> Pages Numbering With Ink  (0) 2020.04.16
<Codesignal> Square Digits Sequence  (0) 2020.04.15
<Codesignal> Is Power?  (0) 2020.04.15
<Codesignal> Make Array Consecutive 2  (0) 2020.04.15
<Codesignal> Replace Middle  (0) 2020.04.14

Easy

Determine if the given number is a power of some non-negative integer.

Example

  • For n = 125, the output should be
    isPower(n) = true;
  • For n = 72, the output should be
    isPower(n) = false.

Input/Output

  • [execution time limit] 0.5 seconds (c)

  • [input] integer n

    A positive integer.

    Guaranteed constraints:
    1 ≤ n ≤ 400.

  • [output] boolean

    • true if n can be represented in the form a^b (a to the power of b) where a and b are some non-negative integers and b ≥ 2, 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 isPower(int n)
{
	if(n==1)
		return true;

	for(int i=2;i<=20;i++)
		for(int j=2;(int)pow(i,j)<=n;j++)
			if((int)pow(i,j)==n)
				return true;

	return false;
}
728x90

'Codesignal' 카테고리의 다른 글

<Codesignal> Square Digits Sequence  (0) 2020.04.15
<Codesignal> Is Sum of Consecutive 2  (0) 2020.04.15
<Codesignal> Make Array Consecutive 2  (0) 2020.04.15
<Codesignal> Replace Middle  (0) 2020.04.14
<Codesignal> Is Smooth?  (0) 2020.04.14

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

Easy

We define the middle of the array arr as follows:

  • if arr contains an odd number of elements, its middle is the element whose index number is the same when counting from the beginning of the array and from its end;
  • if arr contains an even number of elements, its middle is the sum of the two elements whose index numbers when counting from the beginning and from the end of the array differ by one.

Given array arr, your task is to find its middle, and, if it consists of two elements, replace those elements with the value of middle. Return the resulting array as the answer.

Example

  • For arr = [7, 2, 2, 5, 10, 7], the output should be
    replaceMiddle(arr) = [7, 2, 7, 10, 7].

    The middle consists of two elements, 2 and 5. These two elements should be replaced with their sum, i.e. 7.

  • For arr = [-5, -5, 10], the output should be
    replaceMiddle(arr) = [-5, -5, 10].

    The middle is defined as a single element -5, so the initial array with no changes should be returned.

Input/Output

  • [execution time limit] 0.5 seconds (c)

  • [input] array.integer arr

    The given array.

    Guaranteed constraints:
    2 ≤ arr.length ≤ 10^4,
    -10^3 ≤ arr[i] ≤ 10^3.

  • [output] array.integer

    • arr with its middle replaced by a single element, or the initial array if the middle consisted of a single element to begin with.

[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 replaceMiddle(arr_integer arr)
{
	arr_integer returnArray=alloc_arr_integer(arr.size-1);

	if(arr.size%2==1)
		return arr;

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

	return returnArray;
}
728x90

'Codesignal' 카테고리의 다른 글

<Codesignal> Is Power?  (0) 2020.04.15
<Codesignal> Make Array Consecutive 2  (0) 2020.04.15
<Codesignal> Is Smooth?  (0) 2020.04.14
<Codesignal> Remove Array Part  (0) 2020.04.14
<Codesignal> Concatenate Arrays  (0) 2020.04.14

Easy

We define the middle of the array arr as follows:

  • if arr contains an odd number of elements, its middle is the element whose index number is the same when counting from the beginning of the array and from its end;
  • if arr contains an even number of elements, its middle is the sum of the two elements whose index numbers when counting from the beginning and from the end of the array differ by one.

An array is called smooth if its first and its last elements are equal to one another and to the middle. Given an array arr, determine if it is smooth or not.

Example

  • For arr = [7, 2, 2, 5, 10, 7], the output should be
    isSmooth(arr) = true.

    The first and the last elements of arr are equal to 7, and its middle also equals 2 + 5 = 7. Thus, the array is smooth and the output is true.

  • For arr = [-5, -5, 10], the output should be
    isSmooth(arr) = false.

    The first and middle elements are equal to -5, but the last element equals 10. Thus, arr is not smooth and the output is false.

Input/Output

  • [execution time limit] 0.5 seconds (c)

  • [input] array.integer arr

    The given array.

    Guaranteed constraints:
    2 ≤ arr.length ≤ 10^5,
    -10^9 ≤ arr[i] ≤ 10^9.

  • [output] boolean

    • true if arr is smooth, 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

// 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;
// }
//
//
bool isSmooth(arr_integer arr)
{
	return arr.size%2==1?(arr.arr[arr.size/2]==arr.arr[0]&&arr.arr[0]==arr.arr[arr.size-1]):(arr.arr[arr.size/2]+arr.arr[arr.size/2-1]==arr.arr[0]&&arr.arr[0]==arr.arr[arr.size-1]);
}
728x90

'Codesignal' 카테고리의 다른 글

<Codesignal> Make Array Consecutive 2  (0) 2020.04.15
<Codesignal> Replace Middle  (0) 2020.04.14
<Codesignal> Remove Array Part  (0) 2020.04.14
<Codesignal> Concatenate Arrays  (0) 2020.04.14
<Codesignal> First Reverse Try  (0) 2020.04.14

Easy

Remove a part of a given array between given 0-based indexes l and r (inclusive).

Example

For inputArray = [2, 3, 2, 3, 4, 5], l = 2, and r = 4, the output should be
removeArrayPart(inputArray, l, r) = [2, 3, 5].

Input/Output

  • [execution time limit] 0.5 seconds (c)

  • [input] array.integer inputArray

    Guaranteed constraints:
    2 ≤ inputArray.length ≤ 10^4,
    -10^5 ≤ inputArray[i] ≤ 10^5.

  • [input] integer l

    Left index of the part to be removed (0-based).

    Guaranteed constraints:
    0 ≤ l ≤ r.

  • [input] integer r

    Right index of the part to be removed (0-based).

    Guaranteed constraints:
    l ≤ r < inputArray.length.

  • [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 removeArrayPart(arr_integer inputArray,int l,int r)
{
	arr_integer returnArray=alloc_arr_integer(inputArray.size-r+l-1);

	for(int i=0;i<returnArray.size;i++)
		returnArray.arr[i]=i<l?inputArray.arr[i]:inputArray.arr[i+r-l+1];

	return returnArray;
}
728x90

'Codesignal' 카테고리의 다른 글

<Codesignal> Replace Middle  (0) 2020.04.14
<Codesignal> Is Smooth?  (0) 2020.04.14
<Codesignal> Concatenate Arrays  (0) 2020.04.14
<Codesignal> First Reverse Try  (0) 2020.04.14
<Codesignal> Create Array  (0) 2020.04.14

+ Recent posts