Easy

A boy is walking a long way from school to his home. To make the walk more fun he decides to add up all the numbers of the houses that he passes by during his walk. Unfortunately, not all of the houses have numbers written on them, and on top of that the boy is regularly taking turns to change streets, so the numbers don't appear to him in any particular order.

At some point during the walk the boy encounters a house with number 0 written on it, which surprises him so much that he stops adding numbers to his total right after seeing that house.

For the given sequence of houses determine the sum that the boy will get. It is guaranteed that there will always be at least one 0 house on the path.

Example

For inputArray = [5, 1, 2, 3, 0, 1, 5, 0, 2], the output should be
houseNumbersSum(inputArray) = 11.

The answer was obtained as 5 + 1 + 2 + 3 = 11.

Input/Output

  • [execution time limit] 0.5 seconds (c)

  • [input] array.integer inputArray

    Guaranteed constraints:
    5 ≤ inputArray.length ≤ 10,
    0 ≤ inputArray[i] ≤ 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

// 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 houseNumbersSum(arr_integer inputArray)
{
	int sum=0;

	for(int i=0;inputArray.arr[i]!=0;i++)
		sum+=inputArray.arr[i];

	return sum;
}
728x90

'Codesignal' 카테고리의 다른 글

<Codesignal> Alphabet Subsequence  (0) 2020.05.04
<Codesignal> House of Cats  (0) 2020.05.04
<Codesignal> Numbers of Clans  (0) 2020.05.03
<Codesignal> Numbers Grouping  (0) 2020.05.02
<Codesignal> Most Frequent Digit Sum  (0) 2020.04.30

Medium

Let's call two integers A and B friends if each integer from the array divisors is either a divisor of both A and B or neither A nor B. If two integers are friends, they are said to be in the same clan. How many clans are the integers from 1 to k, inclusive, broken into?

Example

For divisors = [2, 3] and k = 6, the output should be
numberOfClans(divisors, k) = 4.

The numbers 1 and 5 are friends and form a clan, 2 and 4 are friends and form a clan, and 3 and 6 do not have friends and each is a clan by itself. So the numbers 1 through 6 are broken into 4 clans.

Input/Output

  • [execution time limit] 0.5 seconds (c)

  • [input] array.integer divisors

    A non-empty array of positive integers.

    Guaranteed constraints:
    2 ≤ divisors.length < 10,
    1 ≤ divisors[i] ≤ 10.

  • [input] integer k

    A positive integer.

    Guaranteed constraints:
    5 ≤ k ≤ 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

// 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 numberOfClans(arr_integer divisors,int k)
{
	int count=0;
	arr_boolean numbers=alloc_arr_boolean(k+1);
	for(int i=0;i<=k;i++)
		numbers.arr[i]=false;

	for(int i=1;i<=k;i++)
		if(numbers.arr[i]==false)
		{
			count++;

			for(int j=i+1;j<=k;j++)
			{
				bool isSame=true;

				for(int d=0;d<divisors.size;d++)
					if(i%divisors.arr[d]==0 && j%divisors.arr[d]!=0 || i%divisors.arr[d]!=0 && j%divisors.arr[d]==0)
					{
						isSame=false;
						break;
					}

				if(isSame)
					numbers.arr[j]=true;
			}
		}

	return count;
}
728x90

'Codesignal' 카테고리의 다른 글

<Codesignal> House of Cats  (0) 2020.05.04
<Codesignal> House Numbers Sum  (0) 2020.05.03
<Codesignal> Numbers Grouping  (0) 2020.05.02
<Codesignal> Most Frequent Digit Sum  (0) 2020.04.30
<Codesignal> Create Anagram  (0) 2020.04.19

Easy

You are given an array of integers that you want distribute between several groups. The first group should contain numbers from 1 to 10^4, the second should contain those from 10^4 + 1 to 2 * 10^4, ..., the 100th one should contain numbers from 99 * 10^4 + 1 to 10^6 and so on.

All the numbers will then be written down in groups to the text file in such a way that:

  • the groups go one after another;
  • each non-empty group has a header which occupies one line;
  • each number in a group occupies one line.

Calculate how many lines the resulting text file will have.

Example

For a = [20000, 239, 10001, 999999, 10000, 20566, 29999], the output should be
numbersGrouping(a) = 11.

The numbers can be divided into 4 groups:

  • 239 and 10000 go to the 1st group (1 ... 10^4);
  • 10001 and 20000 go to the second 2nd (10^4 + 1 ... 2 * 10^4);
  • 20566 and 29999 go to the 3rd group (2 * 10^4 + 1 ... 3 * 10^4);
  • groups from 4 to 99 are empty;
  • 999999 goes to the 100th group (99 * 10^4 + 1 ... 10^6).

Thus, there will be 4 groups (i.e. four headers) and 7 numbers, so the file will occupy 4 + 7 = 11 lines.

Input/Output

  • [execution time limit] 0.5 seconds (c)

  • [input] array.integer a

    Guaranteed constraints:
    1 ≤ a.length ≤ 10^5,
    1 ≤ a[i] ≤ 10^9.

  • [output] integer

    • The number of lines needed to store the grouped numbers.

[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 numbersGrouping(arr_integer a)
{
	arr_boolean group=alloc_arr_boolean(100001);
	int count=0;

	for(int i=0;i<group.size;i++)
		group.arr[i]=false;

	for(int i=0;i<a.size;i++)
		group.arr[(a.arr[i]-1)/10000]=true;

	for(int i=0;i<group.size;i++)
		count+=group.arr[i]==true;

	return count+a.size;
}
728x90

'Codesignal' 카테고리의 다른 글

<Codesignal> House Numbers Sum  (0) 2020.05.03
<Codesignal> Numbers of Clans  (0) 2020.05.03
<Codesignal> Most Frequent Digit Sum  (0) 2020.04.30
<Codesignal> Create Anagram  (0) 2020.04.19
<Codesignal> Is Substitution Cipher?  (0) 2020.04.19

Medium

A step(x) operation works like this: it changes a number x into x - s(x), where s(x) is the sum of x's digits. You like applying functions to numbers, so given the number n, you decide to build a decreasing sequence of numbers: n, step(n), step(step(n)), etc., with 0 as the last element.

Building a single sequence isn't enough for you, so you replace all elements of the sequence with the sums of their digits (s(x)). Now you're curious as to which number appears in the new sequence most often. If there are several answers, return the maximal one.

Example

  • For n = 88, the output should be
    mostFrequentDigitSum(n) = 9.

    • Here is the first sequence you built: 88, 72, 63, 54, 45, 36, 27, 18, 9, 0;
    • And here is s(x) for each of its elements: 16, 9, 9, 9, 9, 9, 9, 9, 9, 0.

    As you can see, the most frequent number in the second sequence is 9.

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

    • At first you built the following sequence: 8, 0
    • s(x) for each of its elements is: 8, 0

    As you can see, the answer is 8 (it appears as often as 0, but is greater than it).

Input/Output

  • [execution time limit] 0.5 seconds (c)

  • [input] integer n

    Guaranteed constraints:
    1 ≤ n ≤ 10^5.

  • [output] integer

    • The most frequent number in the sequence s(n), s(step(n)), s(step(step(n))), etc.

[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 s(int x)
{
	int sum=0;

	while(x>0)
	{
		sum+=x%10;
		x/=10;
	}

	return sum;
}

int mostFrequentDigitSum(int n)
{
	int list[46]={0, }, max=0;

	while(n>0)
	{
		list[s(n)]++;
		n-=s(n);
	}

	for(int i=0;i<46;i++)
		max=list[i]>max?list[i]:max;

	for(int i=45;i>=0;i--)
		if(list[i]==max)
			return i;
}
728x90

'Codesignal' 카테고리의 다른 글

<Codesignal> Numbers of Clans  (0) 2020.05.03
<Codesignal> Numbers Grouping  (0) 2020.05.02
<Codesignal> Create Anagram  (0) 2020.04.19
<Codesignal> Is Substitution Cipher?  (0) 2020.04.19
<Codesignal> Strings Construction  (0) 2020.04.19

Medium

You are given two strings s and t of the same length, consisting of uppercase English letters. Your task is to find the minimum number of "replacement operations" needed to get some anagram of the string t from the string s. A replacement operation is performed by picking exactly one character from the string s and replacing it by some other character.

Example

  • For s = "AABAA" and t = "BBAAA", the output should be
    createAnagram(s, t) = 1;
  • For s = "OVGHK" and t = "RPGUC", the output should be
    createAnagram(s, t) = 4.

Input/Output

  • [execution time limit] 0.5 seconds (c)

  • [input] string s

    Guaranteed constraints:
    5 ≤ s.length ≤ 35.

  • [input] string t

    Guaranteed constraints:
    t.length = s.length.

  • [output] integer

    • The minimum number of replacement operations needed to get an anagram of the string t from the string s.

[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 createAnagram(char *s,char *t)
{
	int alphabet_s[26]={0, }, alphabet_t[26]={0, }, count=0;

	for(int i=0;i<strlen(s);i++)
	{
		alphabet_s[s[i]-'A']++;
		alphabet_t[t[i]-'A']++;
	}

	for(int i=0;i<26;i++)
		count+=abs(alphabet_s[i]-alphabet_t[i]);

	return count/2;
}
728x90

'Codesignal' 카테고리의 다른 글

<Codesignal> Numbers Grouping  (0) 2020.05.02
<Codesignal> Most Frequent Digit Sum  (0) 2020.04.30
<Codesignal> Is Substitution Cipher?  (0) 2020.04.19
<Codesignal> Strings Construction  (0) 2020.04.19
<Codesignal> HTML End Tag By Start Tag  (0) 2020.04.19

Medium

A ciphertext alphabet is obtained from the plaintext alphabet by means of rearranging some characters. For example "bacdef...xyz" will be a simple ciphertext alphabet where a and b are rearranged.

A substitution cipher is a method of encoding where each letter of the plaintext alphabet is replaced with the corresponding (i.e. having the same index) letter of some ciphertext alphabet.

Given two strings, check whether it is possible to obtain them from each other using some (possibly, different) substitution ciphers.

Example

  • For string1 = "aacb" and string2 = "aabc", the output should be
    isSubstitutionCipher(string1, string2) = true.

    Any ciphertext alphabet that starts with acb... would make this transformation possible.

  • For string1 = "aa" and string2 = "bc", the output should be
    isSubstitutionCipher(string1, string2) = false.

Input/Output

  • [execution time limit] 0.5 seconds (c)

  • [input] string string1

    A string consisting of lowercase English characters.

    Guaranteed constraints:
    1 ≤ string1.length ≤ 10.

  • [input] string string2

    A string consisting of lowercase English characters of the same length as string1.

    Guaranteed constraints:
    string2.length = string1.length.

  • [output] boolean

[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 isSubstitutionCipher(char *string1,char *string2)
{
	char cipher1[27]={'\0', }, cipher2[27]={'\0', };

	for(int i=0;i<strlen(string1);i++)
	{
		if(cipher1[string1[i]-'a']=='\0')
			cipher1[string1[i]-'a']=string2[i];
		else
			if(cipher1[string1[i]-'a']!=string2[i])
				return false;
		if(cipher2[string2[i]-'a']=='\0')
			cipher2[string2[i]-'a']=string1[i];
		else
			if(cipher2[string2[i]-'a']!=string1[i])
				return false;
	}

	return true;
}
728x90

'Codesignal' 카테고리의 다른 글

<Codesignal> Most Frequent Digit Sum  (0) 2020.04.30
<Codesignal> Create Anagram  (0) 2020.04.19
<Codesignal> Strings Construction  (0) 2020.04.19
<Codesignal> HTML End Tag By Start Tag  (0) 2020.04.19
<Codesignal> Find Email Domain  (0) 2020.04.19

Medium

Given two strings a and b, both consisting only of lowercase English letters, your task is to calculate how many strings equal to a can be constructed using only letters from the string b? Each letter can be used only once and in one string only.

Example

  • For a = "abc" and b = "abccba", the output should be stringsConstruction(a, b) = 2.

    We can construct 2 strings a = "abc" using only letters from the string b.

  • For a = "ab" and b = "abcbcb", the output should be stringsConstruction(a, b) = 1.

Input/Output

  • [execution time limit] 0.5 seconds (c)

  • [input] string a

    String to construct, containing only lowercase English letters.

    Guaranteed constraints:
    1 ≤ a.length ≤ 10^5.

  • [input] string b

    String containing needed letters, containing only lowercase English letters.

    Guaranteed constraints:
    1 ≤ b.length ≤ 10^5.

  • [output] integer

    • The number of strings a that can be constructed from the string b.

[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 stringsConstruction(char *a,char *b)
{
	long long int alphabet_a[26]={0, }, alphabet_b[26]={0, }, min=1000000;

	for(int i=0;i<strlen(a);i++)
		alphabet_a[a[i]-'a']++;
	for(int i=0;i<strlen(b);i++)
		if(alphabet_a[b[i]-'a']!=0)
			alphabet_b[b[i]-'a']++;

	for(int i=0;i<26;i++)
		if(alphabet_a[i]!=0)
			min=alphabet_b[i]/alphabet_a[i]<min?alphabet_b[i]/alphabet_a[i]:min;

	return min;
}
728x90

'Codesignal' 카테고리의 다른 글

<Codesignal> Create Anagram  (0) 2020.04.19
<Codesignal> Is Substitution Cipher?  (0) 2020.04.19
<Codesignal> HTML End Tag By Start Tag  (0) 2020.04.19
<Codesignal> Find Email Domain  (0) 2020.04.19
<Codesignal> Is Case-Insensitive Palindrome?  (0) 2020.04.19

Easy

You are implementing your own HTML editor. To make it more comfortable for developers you would like to add an auto-completion feature to it.

Given the starting HTML tag, find the appropriate end tag which your editor should propose.

Example

  • For startTag = "<button type='button' disabled>", the output should be
    htmlEndTagByStartTag(startTag) = "</button>";
  • For startTag = "<i>", the output should be
    htmlEndTagByStartTag(startTag) = "</i>".

Input/Output

  • [execution time limit] 0.5 seconds (c)

  • [input] string startTag

    Guaranteed constraints:
    3 ≤ startTag.length ≤ 75.

  • [output] string

[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 *htmlEndTagByStartTag(char *startTag)
{
	char *str=(char *)calloc(100,sizeof(char));

	str[0]='<';
	str[1]='/';

	for(int i=1;!isspace(startTag[i]) && startTag[i]!='>';i++)
		str[i+1]=startTag[i];
	str[strlen(str)]='>';

	return str;
}
728x90

'Codesignal' 카테고리의 다른 글

<Codesignal> Is Substitution Cipher?  (0) 2020.04.19
<Codesignal> Strings Construction  (0) 2020.04.19
<Codesignal> Find Email Domain  (0) 2020.04.19
<Codesignal> Is Case-Insensitive Palindrome?  (0) 2020.04.19
<Codesignal> Is Tandem Repeat?  (0) 2020.04.19

+ Recent posts