Medium

You have a long strip of paper with integers written on it in a single line from left to right. You wish to cut the paper into exactly three pieces such that each piece contains at least one integer and the sum of the integers in each piece is the same. You cannot cut through a number, i.e. each initial number will unambiguously belong to one of the pieces after cutting. How many ways can you do it?

It is guaranteed that the sum of all elements in the array is divisible by 3.

Example

For a = [0, -1, 0, -1, 0, -1], the output should be
threeSplit(a) = 4.

Here are all possible ways:

  • [0, -1] [0, -1] [0, -1]
  • [0, -1] [0, -1, 0] [-1]
  • [0, -1, 0] [-1, 0] [-1]
  • [0, -1, 0] [-1] [0, -1]

Input/Output

  • [execution time limit] 0.5 seconds (c)

  • [input] array.integer a

    Guaranteed constraints:
    5 ≤ a.length ≤ 10^4,
    -10^8 ≤ a[i] ≤ 10^8.

  • [output] integer

    • It's guaranteed that for the given test cases the answer always fits signed 32-bit integer type.

[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 threeSplit(arr_integer a)
{
	long int sum=0, sum1=0, count=0;

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

	for(int i=0;i<a.size-2;i++)
	{
		long int sum2=0;
		sum1+=a.arr[i];
		if(sum1!=sum/3)
			continue;
		for(int j=i+1;j<a.size-1;j++)
		{
			sum2+=a.arr[j];
			if(sum1!=sum2)
				continue;

			count+=sum==sum1+2*sum2;
		}
	}

	return count;    
}
728x90

'Codesignal' 카테고리의 다른 글

<Codesignal> Reflect String  (0) 2020.05.24
<Codesignal> Character Parity  (0) 2020.05.24
<Codesignal> Ada Number  (0) 2020.05.23
<Codesignal> Integer to String of Fixed width  (0) 2020.05.08
<Codesignal> Timed Reading  (0) 2020.05.04

Medium

Consider two following representations of a non-negative integer:

  1. A simple decimal integer, constructed of a non-empty sequence of digits from 0 to 9;
  2. An integer with at least one digit in a base from 2 to 16 (inclusive), enclosed between # characters, and preceded by the base, which can only be a number between 2 and 16 in the first representation. For digits from 10 to 15 characters a, b, ..., f and A, B, ..., F are used.

Additionally, both representations may contain underscore (_) characters; they are used only as separators for improving legibility of numbers and can be ignored while processing a number.

Your task is to determine whether the given string is a valid integer representation.

Note: this is how integer numbers are represented in the programming language Ada.

Example

  • For line = "123_456_789", the output should be
    adaNumber(line) = true;
  • For line = "16#123abc#", the output should be
    adaNumber(line) = true;
  • For line = "10#123abc#", the output should be
    adaNumber(line) = false;
  • For line = "10#10#123ABC#", the output should be
    adaNumber(line) = false;
  • For line = "10#0#", the output should be
    adaNumber(line) = true;
  • For line = "10##", the output should be
    adaNumber(line) = false.

Input/Output

  • [execution time limit] 0.5 seconds (c)

  • [input] string line

    A non-empty string.

    Guaranteed constraints:
    2 ≤ line.length ≤ 30.

  • [output] boolean

    • true if line is a valid integer representation, 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 adaNumber(char *line)
{
	int i, j, N=strlen(line), base=0, count=0;
	bool case1=true;

	for(i=N-1;i>=0;i--)
		if(line[i]=='_')
		{
			for(j=i;j<N;j++)
				line[j]=line[j+1];
			line[N--]='\0';
		}

	if(N==0)
		return false;

	for(i=0;i<N;i++)
		if(!(isdigit(line[i])))
		{
			case1=false;
			break;
		}

	if(case1)
		return true;

	if(line[0]=='#')
		return false;
	else if(line[N-1]!='#')
		return false;

	for(i=0;i<N;i++)
		if(line[i]>='A' && line[i]<='F')
			line[i]+=32;
		else if(line[i]=='#')
			count++;

	if(count!=2)
		return false;

	i=0;
	for(i=0;line[i]!='#';i++)
	{
		if(!isdigit(line[i]))
			return false;
		base*=10;
		base+=line[i]-'0';
	}
	if(base<2 || base>16)
		return false;

	if(N-i<=2)
		return false;

	while(!isalnum(line[++i]))
		if(!(isalnum(line[i]) || line[i]!='#'))
			return false;

	for(;i<N-1;i++)
	{
		if(line[i]>='a' && line[i]<='f')
			line[i]-='a'-10;
		else if(isdigit(line[i]))
			line[i]-='0';
		else
			return false;

		if(line[i]>=base)
			return false;
	}

	return true;
}
728x90

'Codesignal' 카테고리의 다른 글

<Codesignal> Character Parity  (0) 2020.05.24
<Codesignal> Three Split  (0) 2020.05.24
<Codesignal> Integer to String of Fixed width  (0) 2020.05.08
<Codesignal> Timed Reading  (0) 2020.05.04
<Codesignal> Switch Lights  (0) 2020.05.04

Easy

Given a positive integer number and a certain length, we need to modify the given number to have a specified length. We are allowed to do that either by cutting out leading digits (if the number needs to be shortened) or by adding 0s in front of the original number.

Example

  • For number = 1234 and width = 2, the output should be
    integerToStringOfFixedWidth(number, width) = "34";
  • For number = 1234 and width = 4, the output should be
    integerToStringOfFixedWidth(number, width) = "1234";
  • For number = 1234 and width = 5, the output should be
    integerToStringOfFixedWidth(number, width) = "01234".

Input/Output

  • [execution time limit] 0.5 seconds (c)

  • [input] integer number

    A non-negative integer.

    Guaranteed constraints:
    0 ≤ number ≤ 10^9.

  • [input] integer width

    A positive integer representing the desired length.

    Guaranteed constraints:
    1 ≤ width ≤ 50.

  • [output] string

    • The modified version of number as described above.

[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

char *integerToStringOfFixedWidth(int number,int width)
{
	char *str=malloc(sizeof(char)*(width+1));

	for(int i=width-1;i>=0;i--,number/=10)
		str[i]=number%10+'0';

	return str;
}
728x90

'Codesignal' 카테고리의 다른 글

<Codesignal> Three Split  (0) 2020.05.24
<Codesignal> Ada Number  (0) 2020.05.23
<Codesignal> Timed Reading  (0) 2020.05.04
<Codesignal> Switch Lights  (0) 2020.05.04
<Codesignal> Minimal Number of Coins  (0) 2020.05.04

Medium

Timed Reading is an educational tool used in many schools to improve and advance reading skills. A young elementary student has just finished his very first timed reading exercise. Unfortunately he's not a very good reader yet, so whenever he encountered a word longer than maxLength, he simply skipped it and read on.

Help the teacher figure out how many words the boy has read by calculating the number of words in the text he has read, no longer than maxLength.
Formally, a word is a substring consisting of English letters, such that characters to the left of the leftmost letter and to the right of the rightmost letter are not letters.

Example

For maxLength = 4 and
text = "The Fox asked the stork, 'How is the soup?'",
the output should be
timedReading(maxLength, text) = 7.

The boy has read the following words: "The", "Fox", "the", "How", "is", "the", "soup".

Input/Output

  • [execution time limit] 0.5 seconds (c)

  • [input] integer maxLength

    A positive integer, the maximum length of the word the boy can read.

    Guaranteed constraints:
    1 ≤ maxLength ≤ 10.

  • [input] string text

    A non-empty string of English letters and punctuation marks.

    Guaranteed constraints:
    3 ≤ text.length ≤ 110.

  • [output] integer

    • The number of words the boy has read.

[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 timedReading(int maxLength,char *text)
{
	int count=0;

	for(int i=0;i<strlen(text);i++)
		if(isalpha(text[i]))
		{
			int length=0;

			while(isalpha(text[i]))
			{
				i++;
				length++;
			}

			count+=length<=maxLength;
			continue;
		}

	return count;
}
728x90

'Codesignal' 카테고리의 다른 글

<Codesignal> Ada Number  (0) 2020.05.23
<Codesignal> Integer to String of Fixed width  (0) 2020.05.08
<Codesignal> Switch Lights  (0) 2020.05.04
<Codesignal> Minimal Number of Coins  (0) 2020.05.04
<Codesignal> Alphabet Subsequence  (0) 2020.05.04

Medium

N candles are placed in a row, some of them are initially lit. For each candle from the 1st to the Nth the following algorithm is applied: if the observed candle is lit then states of this candle and all candles before it are changed to the opposite. Which candles will remain lit after applying the algorithm to all candles in the order they are placed in the line?

Example

  • For a = [1, 1, 1, 1, 1], the output should be
    switchLights(a) = [0, 1, 0, 1, 0].

    Check out the image below for better understanding:

  • For a = [0, 0], the output should be
    switchLights(a) = [0, 0].

    The candles are not initially lit, so their states are not altered by the algorithm.

Input/Output

  • [execution time limit] 0.5 seconds (c)

  • [input] array.integer a

    Initial situation - array of zeros and ones of length N, 1 means that the corresponding candle is lit.

    Guaranteed constraints:
    2 ≤ a.length ≤ 5000.

  • [output] array.integer

    • Situation after applying the algorithm - array in the same format as input with the same length.

[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 switchLights(arr_integer a)
{
	for(int i=0;i<a.size;i++)
		if(a.arr[i]!=0)
			for(int j=0;j<=i;j++)
				a.arr[j]++;
	for(int i=0;i<a.size;i++)
		a.arr[i]%=2;

	return a;
}
728x90

'Codesignal' 카테고리의 다른 글

<Codesignal> Integer to String of Fixed width  (0) 2020.05.08
<Codesignal> Timed Reading  (0) 2020.05.04
<Codesignal> Minimal Number of Coins  (0) 2020.05.04
<Codesignal> Alphabet Subsequence  (0) 2020.05.04
<Codesignal> House of Cats  (0) 2020.05.04

Medium

You find yourself in Bananaland trying to buy a banana. You are super rich so you have an unlimited supply of banana-coins, but you are trying to use as few coins as possible.

The coin values available in Bananaland are stored in a sorted array coins. coins[0] = 1, and for each i (0 < i < coins.length) coins[i] is divisible by coins[i - 1]. Find the minimal number of banana-coins you'll have to spend to buy a banana given the banana's price.

Example

For coins = [1, 2, 10] and price = 28, the output should be
minimalNumberOfCoins(coins, price) = 6.

You have to use 10 twice, and 2 four times.

Input/Output

  • [execution time limit] 0.5 seconds (c)

  • [input] array.integer coins

    The coin values available in Bananaland.

    Guaranteed constraints:
    1 ≤ coins.length ≤ 5,
    1 ≤ coins[i] ≤ 120.

  • [input] integer price

    A positive integer representing the price of the banana.

    Guaranteed constraints:
    8 ≤ price ≤ 250.

  • [output] integer

    • The minimal number of coins you can use to buy the banana.

[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 minimalNumberOfCoins(arr_integer coins,int price)
{
	int N=0;

	for(int i=coins.size-1;i>=0;i--)
		while(coins.arr[i]<=price)
		{
			price-=coins.arr[i];
			N++;
		}

	return N;
}
728x90

'Codesignal' 카테고리의 다른 글

<Codesignal> Timed Reading  (0) 2020.05.04
<Codesignal> Switch Lights  (0) 2020.05.04
<Codesignal> Alphabet Subsequence  (0) 2020.05.04
<Codesignal> House of Cats  (0) 2020.05.04
<Codesignal> House Numbers Sum  (0) 2020.05.03

Easy

Check whether the given string is a subsequence of the plaintext alphabet.

Example

  • For s = "effg", the output should be
    alphabetSubsequence(s) = false;
  • For s = "cdce", the output should be
    alphabetSubsequence(s) = false;
  • For s = "ace", the output should be
    alphabetSubsequence(s) = true;
  • For s = "bxz", the output should be
    alphabetSubsequence(s) = true.

Input/Output

  • [execution time limit] 0.5 seconds (c)

  • [input] string s

    Guaranteed constraints:
    2 ≤ s.length ≤ 15.

  • [output] boolean

    • true if the given string is a subsequence of the alphabet, 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 alphabetSubsequence(char *s)
{
	for(int i=1;i<strlen(s);i++)
		if(s[i]<=s[i-1])
			return false;
	return true;
}
728x90

'Codesignal' 카테고리의 다른 글

<Codesignal> Switch Lights  (0) 2020.05.04
<Codesignal> Minimal Number of Coins  (0) 2020.05.04
<Codesignal> House of Cats  (0) 2020.05.04
<Codesignal> House Numbers Sum  (0) 2020.05.03
<Codesignal> Numbers of Clans  (0) 2020.05.03

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