Easy

A little boy is studying arithmetics. He has just learned how to add two integers, written one below another, column by column. But he always forgets about the important part - carrying.

Given two integers, your task is to find the result which the little boy will get.

Note: The boy had learned from this site, so feel free to check it out too if you are not familiar with column addition.

Example

For param1 = 456 and param2 = 1734, the output should be
additionWithoutCarrying(param1, param2) = 1180.

    456

  1734

+_____

  1180

The boy performs the following operations from right to left:

  • 6 + 4 = 10 but he forgets about carrying the 1 and just writes down the 0 in the last column
  • 5 + 3 = 8
  • 4 + 7 = 11 but he forgets about the leading 1 and just writes down 1 under 4 and 7.
  • There is no digit in the first number corresponding to the leading digit of the second one, so the boy imagines that 0 is written before 456. Thus, he gets 0 + 1 = 1.

Input/Output

  • [execution time limit] 0.5 seconds (c)

  • [input] integer param1

    A non-negative integer.

    Guaranteed constraints:
    0 ≤ param1 < 10^5.

  • [input] integer param2

    A non-negative integer.

    Guaranteed constraints:
    0 ≤ param2 < 6 · 10^4.

  • [output] integer

    • The result that the little boy will get by using column addition without carrying.

[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 additionWithoutCarrying(int param1,int param2)
{
	int answer=0, N=0;

	while(pow(10,++N)<(param1>param2?param1:param2));
	N--;

	while(N>=0)
	{
		answer+=(int)pow(10,N)*((param1/(int)pow(10,N)+param2/(int)pow(10,N))%10);
		N--;
	}

	return answer;
}
728x90

'Codesignal' 카테고리의 다른 글

<Codesignal> Increase Number Roundness  (0) 2020.04.12
<Codesignal> Apple Boxes  (0) 2020.04.12
<Codesignal> Lineup  (0) 2020.04.12
<Codesignal> Magical Well  (0) 2020.04.12
<Codesignal> Count Sum of Two Representations 2  (0) 2020.04.12

Easy

To prepare his students for an upcoming game, the sports coach decides to try some new training drills. To begin with, he lines them up and starts with the following warm-up exercise: when the coach says 'L', he instructs the students to turn to the left. Alternatively, when he says 'R', they should turn to the right. Finally, when the coach says 'A', the students should turn around.

Unfortunately some students (not all of them, but at least one) can't tell left from right, meaning they always turn right when they hear 'L' and left when they hear 'R'. The coach wants to know how many times the students end up facing the same direction.

Given the list of commands the coach has given, count the number of such commands after which the students will be facing the same direction.

Example

For commands = "LLARL", the output should be
lineUp(commands) = 3.

Let's say that there are 4 students, and the second one can't tell left from right. In this case, only after the second, third and fifth commands will the students face the same direction.

Input/Output

  • [execution time limit] 0.5 seconds (c)

  • [input] string commands

    String consisting of characters 'L', 'R' and 'A' only.

    Guaranteed constraints:
    0 ≤ commands.length ≤ 35.

  • [output] integer

    • The number of commands after which students face the same direction.

[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 lineUp(char *commands)
{
	int count=0, samedirection=0;

	for(int i=0;i<strlen(commands);i++)
	{
		count+=commands[i]!='A';
		samedirection+=count%2==0;
	}

	return samedirection;
}
728x90

'Codesignal' 카테고리의 다른 글

<Codesignal> Apple Boxes  (0) 2020.04.12
<Codesignal> Addition Without Carrying  (0) 2020.04.12
<Codesignal> Magical Well  (0) 2020.04.12
<Codesignal> Count Sum of Two Representations 2  (0) 2020.04.12
<Codesignal> Least Factorial  (0) 2020.04.12

Easy

You are standing at a magical well. It has two positive integers written on it: a and b. Each time you cast a magic marble into the well, it gives you a * b dollars and then both a and b increase by 1. You have n magic marbles. How much money will you make?

Example

For a = 1, b = 2, and n = 2, the output should be
magicalWell(a, b, n) = 8.

You will cast your first marble and get $2, after which the numbers will become 2 and 3. When you cast your second marble, the well will give you $6. Overall, you'll make $8. So, the output is 8.

Input/Output

  • [execution time limit] 0.5 seconds (c)

  • [input] integer a

    Guaranteed constraints:
    1 ≤ a ≤ 2000.

  • [input] integer b

    Guaranteed constraints:
    1 ≤ b ≤ 2000.

  • [input] integer n

    The number of magic marbles in your possession, a non-negative integer.

    Guaranteed constraints:
    0 ≤ n ≤ 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 magicalWell(int a,int b,int n)
{
	int money=0;

	for(int i=0;i<n;i++,a++,b++)
		money+=a*b;

	return money;
}
728x90

'Codesignal' 카테고리의 다른 글

<Codesignal> Addition Without Carrying  (0) 2020.04.12
<Codesignal> Lineup  (0) 2020.04.12
<Codesignal> Count Sum of Two Representations 2  (0) 2020.04.12
<Codesignal> Least Factorial  (0) 2020.04.12
<Codesignal> Second-Rightmost Zero Bit  (0) 2020.04.12

Easy

Given integers n, l and r, find the number of ways to represent n as a sum of two integers A and B such that l ≤ A ≤ B ≤ r.

Example

For n = 6, l = 2, and r = 4, the output should be
countSumOfTwoRepresentations2(n, l, r) = 2.

There are just two ways to write 6 as A + B, where 2 ≤ A ≤ B ≤ 4: 6 = 2 + 4 and 6 = 3 + 3.

Input/Output

  • [execution time limit] 0.5 seconds (c)

  • [input] integer n

    A positive integer.

    Guaranteed constraints:
    5 ≤ n ≤ 10^9.

  • [input] integer l

    A positive integer.

    Guaranteed constraints:
    1 ≤ l ≤ r.

  • [input] integer r

    A positive integer.

    Guaranteed constraints:
    l ≤ r ≤ 10^9,
    r - l ≤ 10^6.

  • [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 countSumOfTwoRepresentations2(int n,int l,int r)
{
	int count=0;

	for(int A=l;A<=r;A++)
		for(int B=A;B<=r;B++)
			if(A+B==n)
			{
				count++;
				break;
			}
			else if(A+B>n)
				break;

	return count;
}
728x90

'Codesignal' 카테고리의 다른 글

<Codesignal> Lineup  (0) 2020.04.12
<Codesignal> Magical Well  (0) 2020.04.12
<Codesignal> Least Factorial  (0) 2020.04.12
<Codesignal> Second-Rightmost Zero Bit  (0) 2020.04.12
<Codesignal> Mirror Bits  (0) 2020.04.09

Easy

Given an integer n, find the minimal k such that

  • k = m! (where m! = 1 * 2 * ... * m) for some integer m;
  • k >= n.

In other words, find the smallest factorial which is not less than n.

Example

For n = 17, the output should be
leastFactorial(n) = 24.

17 < 24 = 4! = 1 * 2 * 3 * 4, while 3! = 1 * 2 * 3 = 6 < 17).

Input/Output

  • [execution time limit] 0.5 seconds (c)

  • [input] integer n

    A positive integer.

    Guaranteed constraints:
    1 ≤ n ≤ 120.

  • [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 leastFactorial(int n)
{
	int factorial=1;

	for(int i=1;factorial<n;i++)
		factorial*=i;

	return factorial;
}
728x90

'Codesignal' 카테고리의 다른 글

<Codesignal> Magical Well  (0) 2020.04.12
<Codesignal> Count Sum of Two Representations 2  (0) 2020.04.12
<Codesignal> Second-Rightmost Zero Bit  (0) 2020.04.12
<Codesignal> Mirror Bits  (0) 2020.04.09
<Codesignal> Range Bit Count  (0) 2020.04.07

Easy

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

Presented with the integer n, find the 0-based position of the second rightmost zero bit in its binary representation (it is guaranteed that such a bit exists), counting from right to left.

Return the value of 2^position_of_the_found_bit.

Example

For n = 37, the output should be
secondRightmostZeroBit(n) = 8.

3710 = 1001012. The second rightmost zero bit is at position 3 (0-based) from the right in the binary representation of n.
Thus, the answer is 2^3 = 8.

Input/Output

  • [execution time limit] 0.5 seconds (cpp)

  • [input] integer n

    Guaranteed constraints:
    4 ≤ n ≤ 2^30.

  • [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 secondRightmostZeroBit(int n) {
  return ~(n|~n&(n+1))&((n|~n&(n+1))+1);
}
728x90

'Codesignal' 카테고리의 다른 글

<Codesignal> Count Sum of Two Representations 2  (0) 2020.04.12
<Codesignal> Least Factorial  (0) 2020.04.12
<Codesignal> Mirror Bits  (0) 2020.04.09
<Codesignal> Range Bit Count  (0) 2020.04.07
<Codesignal> Array Packing  (0) 2020.04.07

Easy

Reverse the order of the bits in a given integer.

Example

  • For a = 97, the output should be
    mirrorBits(a) = 67.

    97 equals to 1100001 in binary, which is 1000011 after mirroring, and that is 67 in base 10.

  • For a = 8, the output should be
    mirrorBits(a) = 1.

Input/Output

  • [execution time limit] 0.5 seconds (c)

  • [input] integer a

    Guaranteed constraints:
    5 ≤ a ≤ 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 mirrorBits(int a)
{
	int N=0, *array=NULL, count=0;

	while(a>=(1<<++N));
	array=(int *)calloc(N,sizeof(int));

	for(int i=0;i<N;i++)
		array[N-i-1]=((a&(1<<i))!=0);
	a=0;

	for(int i=0;i<N;i++)
		a+=(int)pow(2,i)*array[i];

	free(array);
	return a;
}
728x90

'Codesignal' 카테고리의 다른 글

<Codesignal> Least Factorial  (0) 2020.04.12
<Codesignal> Second-Rightmost Zero Bit  (0) 2020.04.12
<Codesignal> Range Bit Count  (0) 2020.04.07
<Codesignal> Array Packing  (0) 2020.04.07
<Codesignal> Kill K-th Bit  (0) 2020.04.07

Easy

You are given two numbers a and b where 0 ≤ a ≤ b. Imagine you construct an array of all the integers from a to b inclusive. You need to count the number of 1s in the binary representations of all the numbers in the array.

Example

For a = 2 and b = 7, the output should be
rangeBitCount(a, b) = 11.

Given a = 2 and b = 7 the array is: [2, 3, 4, 5, 6, 7]. Converting the numbers to binary, we get [10, 11, 100, 101, 110, 111], which contains 1 + 2 + 1 + 2 + 2 + 3 = 11 1s.

Input/Output

  • [execution time limit] 0.5 seconds (c)

  • [input] integer a

    Guaranteed constraints:
    0 ≤ a ≤ b.

  • [input] integer b

    Guaranteed constraints:
    a ≤ b ≤ 10.

  • [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 rangeBitCount(int a,int b)
{
	int count=0;

	for(int i=a;i<=b;i++)
		for(int j=0;j<5;j++)
			count+=(i&(1<<j))!=0;

	return count;
}
728x90

'Codesignal' 카테고리의 다른 글

<Codesignal> Second-Rightmost Zero Bit  (0) 2020.04.12
<Codesignal> Mirror Bits  (0) 2020.04.09
<Codesignal> Array Packing  (0) 2020.04.07
<Codesignal> Kill K-th Bit  (0) 2020.04.07
<Codesignal> Metro Card  (0) 2020.04.07

+ Recent posts