Easy

Ticket numbers usually consist of an even number of digits. A ticket number is considered lucky if the sum of the first half of the digits is equal to the sum of the second half.

Given a ticket number n, determine if it's lucky or not.

Example

  • For n = 1230, the output should be
    isLucky(n) = true;
  • For n = 239017, the output should be
    isLucky(n) = false.

Input/Output

  • [execution time limit] 0.5 seconds (c)

  • [input] integer n

    A ticket number represented as a positive integer with an even number of digits.

    Guaranteed constraints:
    10 ≤ n < 10^6.

  • [output] boolean

    • true if n is a lucky ticket number, 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 isLucky(int n)
{
	int front=0, back=0;
	char number[8]={'\0', };

	sprintf(number,"%d", n);

	for(int i=0;i<strlen(number)/2;i++)
	{
		front+=number[i]-'0';
		back+=number[strlen(number)-1-i]-'0';
	}

	return front==back;
}
728x90

'Codesignal' 카테고리의 다른 글

<Codesignal> reverseInParentheses  (0) 2020.04.05
<Codesignal> Sort by Height  (0) 2020.04.05
<Codesignal> commonCharacterCount  (0) 2020.04.05
<Codesignal> All Longest Strings  (0) 2020.04.05
<Codesignal> matrixElementsSum  (0) 2020.04.05

Easy

Given two strings, find the number of common characters between them.

Example

For s1 = "aabcc" and s2 = "adcaa", the output should be
commonCharacterCount(s1, s2) = 3.

Strings have 3 common characters - 2 "a"s and 1 "c".

Input/Output

  • [execution time limit] 0.5 seconds (c)

  • [input] string s1

    A string consisting of lowercase English letters.

    Guaranteed constraints:
    1 ≤ s1.length < 15.

  • [input] string s2

    A string consisting of lowercase English letters.

    Guaranteed constraints:
    1 ≤ s2.length < 15.

  • [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 commonCharacterCount(char *s1,char *s2)
{
	int alphabet1[26]={0, }, alphabet2[26]={0, }, count=0;

	for(int i=0;i<strlen(s1);i++)
		alphabet1[s1[i]-'a']++;
	for(int i=0;i<strlen(s2);i++)
		alphabet2[s2[i]-'a']++;

	for(int i=0;i<26;i++)
		count+=alphabet1[i]<alphabet2[i]?alphabet1[i]:alphabet2[i];

	return count;
}
728x90

'Codesignal' 카테고리의 다른 글

<Codesignal> Sort by Height  (0) 2020.04.05
<Codesignal> isLucky  (0) 2020.04.05
<Codesignal> All Longest Strings  (0) 2020.04.05
<Codesignal> matrixElementsSum  (0) 2020.04.05
<Codesignal> almostIncreasingSequence  (0) 2020.04.05

Easy

Given an array of strings, return another array containing all of its longest strings.

Example

For inputArray = ["aba", "aa", "ad", "vcd", "aba"], the output should be
allLongestStrings(inputArray) = ["aba", "vcd", "aba"].

Input/Output

  • [execution time limit] 0.5 seconds (c)

  • [input] array.string inputArray

    A non-empty array.

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

  • [output] array.string

    • Array of the longest strings, stored in the same order as in the inputArray.

[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_string allLongestStrings(arr_string inputArray)
{
	arr_string returnStrings;
	int max=0, count=0;

	for(int i=0;i<inputArray.size;i++)
		max=strlen(inputArray.arr[i])>max?strlen(inputArray.arr[i]):max;
	for(int i=0;i<inputArray.size;i++)
		count+=strlen(inputArray.arr[i])==max;

	returnStrings=alloc_arr_string(count);
	for(int i=0;i<returnStrings.size;i++)
		returnStrings.arr[i]=malloc((max+1)*sizeof(char));

	for(int i=inputArray.size-1;i>=0;i--)
		if(strlen(inputArray.arr[i])==max)
			strcpy(returnStrings.arr[--count],inputArray.arr[i]);

	return returnStrings;
}
728x90

'Codesignal' 카테고리의 다른 글

<Codesignal> isLucky  (0) 2020.04.05
<Codesignal> commonCharacterCount  (0) 2020.04.05
<Codesignal> matrixElementsSum  (0) 2020.04.05
<Codesignal> almostIncreasingSequence  (0) 2020.04.05
<Codesignal> shapeArea  (0) 2020.04.05

Easy

After becoming famous, the CodeBots decided to move into a new building together. Each of the rooms has a different cost, and some of them are free, but there's a rumour that all the free rooms are haunted! Since the CodeBots are quite superstitious, they refuse to stay in any of the free rooms, or any of the rooms below any of the free rooms.

Given matrix, a rectangular matrix of integers, where each value represents the cost of the room, your task is to return the total sum of all rooms that are suitable for the CodeBots (ie: add up all the values that don't appear below a 0).

Example

  • For

    matrix = [[0, 1, 1, 2], [0, 5, 0, 0], [2, 0, 3, 3]]

    the output should be
    matrixElementsSum(matrix) = 9.

    There are several haunted rooms, so we'll disregard them as well as any rooms beneath them. Thus, the answer is 1 + 5 + 1 + 2 = 9.

  • For

    matrix = [[1, 1, 1, 0], [0, 5, 0, 1], [2, 1, 3, 10]]

    the output should be
    matrixElementsSum(matrix) = 9.

    Note that the free room in the final column makes the full column unsuitable for bots (not just the room directly beneath it). Thus, the answer is 1 + 1 + 1 + 5 + 1 = 9.

Input/Output

  • [execution time limit] 0.5 seconds (c)

  • [input] array.array.integer matrix

    A 2-dimensional array of integers representing the cost of each room in the building. A value of 0 indicates that the room is haunted.

    Guaranteed constraints:
    1 ≤ matrix.length ≤ 5,
    1 ≤ matrix[i].length ≤ 5,
    0 ≤ matrix[i][j] ≤ 10.

  • [output] integer

    • The total price of all the rooms that are suitable for the CodeBots to live in.

[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 matrixElementsSum(arr_arr_integer matrix)
{
	int sum=0;

	for(int i=0;i<matrix.size;i++)
		for(int j=0;j<matrix.arr[0].size;j++)
			if(matrix.arr[i].arr[j]==0)
				for(int k=i+1;k<matrix.size;k++)
					matrix.arr[k].arr[j]=0;

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

	return sum;
}
728x90

'Codesignal' 카테고리의 다른 글

<Codesignal> commonCharacterCount  (0) 2020.04.05
<Codesignal> All Longest Strings  (0) 2020.04.05
<Codesignal> almostIncreasingSequence  (0) 2020.04.05
<Codesignal> shapeArea  (0) 2020.04.05
<Codesignal> adjacentElementsProduct  (0) 2020.04.05

Easy

Given a sequence of integers as an array, determine whether it is possible to obtain a strictly increasing sequence by removing no more than one element from the array.

Note: sequence a0, a1, ..., an is considered to be a strictly increasing if a0 < a1 < ... < an. Sequence containing only one element is also considered to be strictly increasing.

Example

  • For sequence = [1, 3, 2, 1], the output should be
    almostIncreasingSequence(sequence) = false.

    There is no one element in this array that can be removed in order to get a strictly increasing sequence.

  • For sequence = [1, 3, 2], the output should be
    almostIncreasingSequence(sequence) = true.

    You can remove 3 from the array to get the strictly increasing sequence [1, 2]. Alternately, you can remove 2 to get the strictly increasing sequence [1, 3].

Input/Output

  • [execution time limit] 0.5 seconds (c)

  • [input] array.integer sequence

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

  • [output] boolean

    • Return true if it is possible to remove one element from the array in order to get a strictly increasing sequence, otherwise return false.

[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 almostIncreasingSequence(arr_integer sequence)
{
	bool notIncreased=false;
	int index;
	arr_integer removed=alloc_arr_integer(sequence.size-1);

	for(int i=0;i<sequence.size-1;i++)
		if(sequence.arr[i+1]<=sequence.arr[i])
		{
			notIncreased=true;
			index=i;
			break;
		}
	if(!notIncreased)
		return true;

	for(int i=0;i<removed.size;i++)
		removed.arr[i]=sequence.arr[i<index?i:i+1];
	notIncreased=false;
	for(int i=0;i<removed.size-1;i++)
		if(removed.arr[i+1]<=removed.arr[i])
		{
			notIncreased=true;
			break;
		}
	if(!notIncreased)
		return true;

	for(int i=0;i<removed.size;i++)
		removed.arr[i]=sequence.arr[i<index+1?i:i+1];
	notIncreased=false;
	for(int i=0;i<removed.size-1;i++)
		if(removed.arr[i+1]<=removed.arr[i])
			return false;

	return true;
}
728x90

'Codesignal' 카테고리의 다른 글

<Codesignal> All Longest Strings  (0) 2020.04.05
<Codesignal> matrixElementsSum  (0) 2020.04.05
<Codesignal> shapeArea  (0) 2020.04.05
<Codesignal> adjacentElementsProduct  (0) 2020.04.05
<Codesignal> checkPalindrome  (0) 2020.04.05

Easy

Below we will define an n-interesting polygon. Your task is to find the area of a polygon for a given n.

A 1-interesting polygon is just a square with a side of length 1. An n-interesting polygon is obtained by taking the n - 1-interesting polygon and appending 1-interesting polygons to its rim, side by side. You can see the 1-, 2-, 3- and 4-interesting polygons in the picture below.

Example

  • For n = 2, the output should be
    shapeArea(n) = 5;
  • For n = 3, the output should be
    shapeArea(n) = 13.

Input/Output

  • [execution time limit] 0.5 seconds (c)

  • [input] integer n

    Guaranteed constraints:
    1 ≤ n < 10^4.

  • [output] integer

    • The area of the n-interesting polygon.

[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 shapeArea(int n)
{
	int size=1;

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

	return size;
}
728x90

'Codesignal' 카테고리의 다른 글

<Codesignal> matrixElementsSum  (0) 2020.04.05
<Codesignal> almostIncreasingSequence  (0) 2020.04.05
<Codesignal> adjacentElementsProduct  (0) 2020.04.05
<Codesignal> checkPalindrome  (0) 2020.04.05
<Codesignal> centuryFromYear  (0) 2020.04.05

Easy

Given an array of integers, find the pair of adjacent elements that has the largest product and return that product.

Example

For inputArray = [3, 6, -2, -5, 7, 3], the output should be
adjacentElementsProduct(inputArray) = 21.

7 and 3 produce the largest product.

Input/Output

  • [execution time limit] 0.5 seconds (c)

  • [input] array.integer inputArray

    An array of integers containing at least two elements.

    Guaranteed constraints:
    2 ≤ inputArray.length ≤ 10,
    -1000 ≤ inputArray[i] ≤ 1000.

  • [output] integer

    • The largest product of adjacent elements.

[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 adjacentElementsProduct(arr_integer inputArray)
{
	int max=-1000;

	for(int i=0;i<inputArray.size-1;i++)
		max=inputArray.arr[i]*inputArray.arr[i+1]>max?inputArray.arr[i]*inputArray.arr[i+1]:max;

	return max;
}
728x90

'Codesignal' 카테고리의 다른 글

<Codesignal> almostIncreasingSequence  (0) 2020.04.05
<Codesignal> shapeArea  (0) 2020.04.05
<Codesignal> checkPalindrome  (0) 2020.04.05
<Codesignal> centuryFromYear  (0) 2020.04.05
<Codesignal> add  (0) 2020.04.05

Easy

Given the string, check if it is a palindrome.

Example

  • For inputString = "aabaa", the output should be
    checkPalindrome(inputString) = true;
  • For inputString = "abac", the output should be
    checkPalindrome(inputString) = false;
  • For inputString = "a", the output should be
    checkPalindrome(inputString) = true.

Input/Output

  • [execution time limit] 0.5 seconds (c)

  • [input] string inputString

    A non-empty string consisting of lowercase characters.

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

  • [output] boolean

    • true if inputString is a palindrome, 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 checkPalindrome(char *inputString)
{
	for(int i=0;i<strlen(inputString)/2;i++)
		if(inputString[i]!=inputString[strlen(inputString)-i-1])
			return false;
	return true;
}
728x90

'Codesignal' 카테고리의 다른 글

<Codesignal> almostIncreasingSequence  (0) 2020.04.05
<Codesignal> shapeArea  (0) 2020.04.05
<Codesignal> adjacentElementsProduct  (0) 2020.04.05
<Codesignal> centuryFromYear  (0) 2020.04.05
<Codesignal> add  (0) 2020.04.05

+ Recent posts