├── README.md └── cpp ├── SieveOfEratosthenes.cpp ├── areSimilar.cpp ├── arrayKthGreatest.cpp ├── arrayMode.cpp ├── bishopAndPawn.cpp ├── candles.cpp ├── checkPalindrome.cpp ├── circleOfNumbers.cpp ├── countSumOfTwoRepresentations.cpp ├── cyclicString.cpp ├── differentSubstrings_2.cpp ├── factorialTrailingZeros.cpp ├── firstNotDivisible.cpp ├── fixedPointsPermutation.cpp ├── fractionDivision.cpp ├── integerToStringOfFixedWidth.cpp ├── inversePermutation_3.cpp ├── isPower.cpp ├── lastDigit.cpp ├── makeArrayConsecutive.cpp ├── partialSort.cpp ├── phoneCall.cpp ├── powerRecursive.cpp ├── primeFactors2.cpp ├── regularBracketSequence1.cpp ├── removeDuplicateStrings.cpp ├── simplifyPath.cpp ├── smallestMultiple.cpp ├── swapArrayHalves.cpp └── visitsOnCircularRoad.cpp /README.md: -------------------------------------------------------------------------------- 1 | # codefights 2 | repo for my codefight solo solutions 3 | -------------------------------------------------------------------------------- /cpp/SieveOfEratosthenes.cpp: -------------------------------------------------------------------------------- 1 | /* 2 | Given integer n, output sorted array of all prime numbers between 1 and n, inclusive. 3 | 4 | Example 5 | 6 | for n=9 output should be [2, 3, 5, 7] 7 | 8 | [input] integer n 9 | 10 | positive integer 11 | [output] array.integer 12 | */ 13 | 14 | 15 | std::vector eratosthenesSieve(int n) { 16 | 17 | std::vector primes; 18 | std::vector mayBePrime(n + 1, true); 19 | 20 | for (int i = 2; i <= n; i++) { 21 | if (mayBePrime[i]) { 22 | primes.push_back(i); 23 | for (int j = i; i * j <= n; j++) { 24 | mayBePrime[i * j] = false; 25 | } 26 | } 27 | } 28 | 29 | std::vector result; 30 | for (int i = 0; i < primes.size(); i++) { 31 | result.push_back(primes[i]); 32 | } 33 | 34 | return result; 35 | } 36 | -------------------------------------------------------------------------------- /cpp/areSimilar.cpp: -------------------------------------------------------------------------------- 1 | /* 2 | Two arrays are called similar if one can be obtained from the other by swapping at most one pair of elements. 3 | 4 | Given two arrays, check whether they are similar. 5 | 6 | Example 7 | 8 | for [1, 2, 3] and [1, 2, 3] the answer should be true 9 | for [1, 2, 3] and [2, 1, 3] the answer should be true 10 | for [1, 2, 3] and [3, 1, 2] the answer should be false 11 | [input] array.integer A 12 | 13 | array of integers 14 | [input] array.integer B 15 | 16 | array of integers of the same length as A 17 | [output] boolean 18 | 19 | true if A and B are similar, false otherwise 20 | */ 21 | 22 | bool areSimilar(std::vector A, std::vector B) { 23 | 24 | for (int i = 0; i < A.size(); i++) { 25 | for (int j = i; j < A.size(); j++) { 26 | bool equal = true; 27 | int tmp = A[j]; // int tmp = A[i] 28 | A[j] = A[i]; // A[i] = A[j] 29 | A[j] = tmp; 30 | for (int k = 0; k < A.size(); k++) { 31 | if (A[k] != B[k]) { 32 | equal = false; 33 | break; 34 | } 35 | } 36 | if (equal) { 37 | return true; 38 | } 39 | A[j] = A[i]; 40 | A[i] = tmp; 41 | } 42 | } 43 | return false; 44 | } 45 | -------------------------------------------------------------------------------- /cpp/arrayKthGreatest.cpp: -------------------------------------------------------------------------------- 1 | /* 2 | Given array of integers, find its k-th greatest element. 3 | 4 | Example 5 | 6 | for array=[19, 32, 11, 23] and k=3 output should be 19 7 | 8 | [input] array.integer inputArray 9 | 10 | array of pairwise distinct integers 11 | [input] integer k 12 | 13 | positive integer (not exceeding array length) 14 | [output] integer 15 | 16 | k-th greatest element of array 17 | */ 18 | 19 | int arrayKthGreatest(std::vector inputArray, int k) { 20 | 21 | for (int i = 0; i < k; i++) { 22 | int indexOfMaximum = i, 23 | tmp = inputArray[i]; 24 | 25 | for (int j = i + 1; j < inputArray.size(); j++) { 26 | if (inputArray[j] > inputArray[indexOfMaximum]) { 27 | indexOfMaximum = j; 28 | } 29 | } 30 | 31 | inputArray[i] = inputArray[indexOfMaximum]; 32 | inputArray[indexOfMaximum] = tmp; 33 | } 34 | 35 | return inputArray[k - 1]; 36 | } 37 | -------------------------------------------------------------------------------- /cpp/arrayMode.cpp: -------------------------------------------------------------------------------- 1 | /* 2 | Mode is the number which appears most often in a set of numbers. 3 | 4 | Given array of integers, find its mode. 5 | 6 | Example 7 | 8 | for [1, 3, 3, 3, 1] output should be 3 9 | for [1, 3, 2, 1] output should be 1 10 | [input] array.integer sequence 11 | 12 | unsorted array of integers each from 1 to 10 inclusive 13 | [output] integer 14 | 15 | the mode of sequence. It is guaranteed that there is an unambiguous answer for the given data. 16 | */ 17 | 18 | int arrayMode(std::vector sequence) { 19 | std::vector cnt; 20 | int answer = 0; 21 | 22 | for (int i = 0; i < 10; i++) { 23 | cnt.push_back(0); 24 | } 25 | for (int i = 0; i < sequence.size(); i++) { 26 | cnt.at(sequence.at(i) - 1)++; 27 | if (cnt.at(sequence.at(i) - 1) > cnt.at(answer)) { 28 | answer = sequence.at(i) - 1; 29 | } 30 | } 31 | return answer + 1; 32 | } 33 | -------------------------------------------------------------------------------- /cpp/bishopAndPawn.cpp: -------------------------------------------------------------------------------- 1 | /* 2 | Given the positions of a white bishop and a black pawn on the standard chess board, determine whether the bishop can capture the pawn in one move. 3 | 4 | Example 5 | 6 | for "A1" and "C3" output should be true, 7 | for "H1" and "H3" output should be false, 8 | [input] string cell1 9 | 10 | coordinates of the white bishop 11 | [input] string cell2 12 | 13 | coordinates of the black pawn 14 | [output] boolean 15 | */ 16 | 17 | bool bishopAndPawn(std::string cell1, std::string cell2) { 18 | struct Parser { 19 | int getX(char pos) { 20 | return pos - 'A'; 21 | } 22 | 23 | int getY(char pos) { 24 | return pos - '1'; 25 | } 26 | }; 27 | 28 | Parser myParser; 29 | int x1 = myParser.getX(cell1[0]), 30 | y1 = myParser.getY(cell1[1]), 31 | x2 = myParser.getX(cell2[0]), 32 | y2 = myParser.getY(cell2[1]); 33 | 34 | if (x1 + y2 == x2 + y1 || x1 + y1 == x2 + y2) { 35 | return true; 36 | } 37 | 38 | return false; 39 | } 40 | -------------------------------------------------------------------------------- /cpp/candles.cpp: -------------------------------------------------------------------------------- 1 | /* 2 | When candles finish burning they leave leftovers that can be combined to make a new candle. Assume that it's possible to make 3 | a single new candle from B leftovers for some integer B. 4 | 5 | Given the initial number of burning candles A and the integer B, find the total number of candles that we can burn. 6 | 7 | Example 8 | 9 | If you had 5 initial candles and could make a new one from 2 leftovers then you can burn 9 = 5 + 2 + 1 + 1 candles in total. 10 | */ 11 | 12 | int candles(int A, int B) { 13 | int burned = 0; 14 | int leftowers = 0; 15 | while (A > 0) { 16 | burned += A; 17 | leftowers += A; 18 | A = leftowers / B; 19 | leftowers %= B; 20 | } 21 | return burned; 22 | } 23 | 24 | 25 | -------------------------------------------------------------------------------- /cpp/checkPalindrome.cpp: -------------------------------------------------------------------------------- 1 | /* 2 | Given the string, check if it is a palindrome. 3 | 4 | Example 5 | 6 | for 'aabaa' the output should be true 7 | For 'abac' the output should be false 8 | [input] string inputString 9 | 10 | non-empty string consisting of lowercase characters 11 | [output] boolean 12 | 13 | true if inputString is a palindrome, false otherwise 14 | */ 15 | 16 | bool checkPalindrome(std::string inputString) { 17 | 18 | bool isPalindrome = true; 19 | for (int i = 0; i < inputString.size(); i++) { 20 | if (inputString[i] != inputString[(int)inputString.size() - i - 1]) { 21 | isPalindrome = false; 22 | return false; 23 | } 24 | } 25 | 26 | return isPalindrome; 27 | } 28 | -------------------------------------------------------------------------------- /cpp/circleOfNumbers.cpp: -------------------------------------------------------------------------------- 1 | /* 2 | Consider integer numbers from 0 to n-1 written down along the circle in such a way that the distance between any two neighbouring numbers is equal (note that 0 and n-1 are neighbouring, too). 3 | 4 | Given n and firstNumber, find the number which is written in the radially opposite position to firstNumber. 5 | 6 | Example 7 | 8 | for n=10, firstNumber=2 output should be 7 9 | 10 | [input] integer n 11 | 12 | a positive even integer n 13 | [input] integer firstNumber 14 | 15 | an integer from 0 to n-1 16 | [output] integer 17 | */ 18 | 19 | int circleOfNumbers(int n, int firstNumber) { 20 | return (firstNumber + n / 2)%n; 21 | } 22 | -------------------------------------------------------------------------------- /cpp/countSumOfTwoRepresentations.cpp: -------------------------------------------------------------------------------- 1 | /* 2 | 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. 3 | 4 | Example 5 | 6 | for n=6, l=2, r=4 output should be 2 7 | 8 | [input] integer n 9 | 10 | a positive integer 11 | [input] integer l 12 | 13 | a positive integer 14 | [input] integer r 15 | 16 | a positive integer 17 | [output] integer 18 | */ 19 | 20 | int countSumOfTwoRepresentations(int n, int l, int r) { 21 | int result = 0; 22 | 23 | for (int a = l; a <= r; a++) { 24 | for (int b = a; b <= r; b++) { 25 | if (a + b == n) { 26 | result++; 27 | } 28 | } 29 | } 30 | 31 | return result; 32 | } 33 | -------------------------------------------------------------------------------- /cpp/cyclicString.cpp: -------------------------------------------------------------------------------- 1 | /* 2 | We call a string cyclic if it can be obtained by concatenating another string to itself many times (for example, s2 = "abcabcabcabc..." is cyclic since it can be obtained from s1 = "abc" in such a way). 3 | 4 | Given some substring of a cyclic string s, find the length of the smallest possible string that can be concatenated to itself many times to obtain s. 5 | 6 | Example 7 | 8 | for "cabca" output should be 3 9 | 10 | [input] string s1 11 | 12 | non-empty string 13 | [output] integer 14 | */ 15 | int cyclicString(std::string s1) { 16 | 17 | for (int answer = 1; answer < s1.size(); answer++) { 18 | bool correct = true; 19 | for (int position = 0; position+answer < s1.size(); position++) { 20 | if (s1[position] != s1[position + answer]) { 21 | correct = false; 22 | break; 23 | } 24 | } 25 | if (correct) { 26 | return answer; 27 | } 28 | } 29 | return s1.size(); 30 | } 31 | -------------------------------------------------------------------------------- /cpp/differentSubstrings_2.cpp: -------------------------------------------------------------------------------- 1 | /* 2 | Given a string, find the number of different non-empty substrings in it. 3 | 4 | Example 5 | 6 | for 'abac' the output should be 9 7 | 8 | [input] string inputStr 9 | 10 | non-empty string 11 | [output] integer 12 | 13 | */ 14 | 15 | int differentSubstrings(std::string inputStr) { 16 | 17 | std::vector substrings; 18 | int result = 1; 19 | 20 | for (int i = 0; i < inputStr.size(); i++) { 21 | for (int j = i + 1; j <= inputStr.size(); j++) { 22 | substrings.push_back(inputStr.substr(i, j - i)); 23 | } 24 | } 25 | std::sort(substrings.begin(), substrings.end()); 26 | for (int i = 1; i < substrings.size(); i++) { 27 | if (substrings[i] != substrings[i - 1]) { 28 | result++; 29 | } 30 | } 31 | 32 | return result; 33 | } 34 | -------------------------------------------------------------------------------- /cpp/factorialTrailingZeros.cpp: -------------------------------------------------------------------------------- 1 | /* 2 | Given an integer n, find the number of trailing zeros in the decimal representation of n! (the exclamation mark means factorial). 3 | 4 | Example 5 | 6 | for n = 10 output should be 2 7 | 8 | [input] integer n 9 | 10 | a positive integer 11 | [output] integer 12 | */ 13 | int factorialTrailingZeros(int n) { 14 | int result = 0; 15 | for (int i = 5; i <= n; i += 5) { 16 | int number = i; 17 | while (number % 5 == 0) { 18 | number /= 5; 19 | result++; 20 | } 21 | } 22 | return result; 23 | } 24 | -------------------------------------------------------------------------------- /cpp/firstNotDivisible.cpp: -------------------------------------------------------------------------------- 1 | /* 2 | [input] array.integer divisors 3 | 4 | array of positive integers, each greater than 1 5 | [input] integer start 6 | 7 | positive integer 8 | [output] integer 9 | 10 | the smallest integer not less than start which is not divisible by any integer from divisors 11 | */ 12 | 13 | int firstNotDivisible(std::vector divisors, int start) { 14 | 15 | for (int answer = start; ; answer++) { 16 | bool correct = true; 17 | for (int i = 1; i < divisors.size(); i++) { 18 | if (answer % divisors[i] == 0) { 19 | correct = false; 20 | break; 21 | } 22 | } 23 | if (correct) { 24 | return answer; 25 | } 26 | } 27 | } 28 | -------------------------------------------------------------------------------- /cpp/fixedPointsPermutation.cpp: -------------------------------------------------------------------------------- 1 | /* 2 | Find the number of fixed points in a permutation of [1,2,3,...,n] for some n, where fixed points are elements in the permutation that didn't change their original position. 3 | 4 | Example 5 | 6 | for [1, 3, 2, 4, 5, 6] output should be 4, since 1,4,5 and 6 stayed in the same positions. 7 | 8 | [input] array.integer permutation 9 | 10 | array of length n representing some permutation of integers from 1 to n. 11 | [output] integer 12 | 13 | the number of fixed points in permutation 14 | */ 15 | 16 | int fixedPointsPermutation(std::vector permutation) { 17 | 18 | int result = 0; 19 | 20 | for (int i = 0; i < permutation.size(); i++) { 21 | if (permutation[i] == i + 1) { 22 | result++; 23 | } 24 | } 25 | 26 | return result; 27 | } 28 | -------------------------------------------------------------------------------- /cpp/fractionDivision.cpp: -------------------------------------------------------------------------------- 1 | /* 2 | We call a fraction reduced if its numerator and denominator are relatively prime. Implement a function that can divide two fractions and produce a reduced fraction. 3 | 4 | Example 5 | 6 | for [2, 3] and [5, 6] output should be [4, 5] 7 | 8 | [input] array.integer A 9 | 10 | array A of length 2 representing fraction A[0] / A[1] 11 | [input] array.integer B 12 | 13 | array B of length 2 representing fraction B[0] / B[1] 14 | [output] array.integer 15 | 16 | ratio of A and B as a reduced fraction 17 | */ 18 | std::vector fractionDivision(std::vector A, std::vector B) { 19 | 20 | struct Helper { 21 | int gcdEuclid(int a, int b) { 22 | if (a == 0) { 23 | return b; 24 | } 25 | return gcdEuclid(b % a, a); 26 | } 27 | }; 28 | Helper h; 29 | 30 | std::vector C(2); 31 | C[0] = A[0] * B[1], C[1] = A[1] * B[0]; 32 | int gcd = h.gcdEuclid(C[0], C[1]); 33 | 34 | C[0] /= gcd; 35 | C[1] /= gcd; 36 | 37 | return C; 38 | } 39 | 40 | -------------------------------------------------------------------------------- /cpp/integerToStringOfFixedWidth.cpp: -------------------------------------------------------------------------------- 1 | /* 2 | 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. 3 | 4 | Example 5 | 6 | IntegerToStringOfFixedWidth(1234, 2) = "34" 7 | IntegerToStringOfFixedWidth(1234, 4) = "1234" 8 | IntegerToStringOfFixedWidth(1234, 5) = "01234" 9 | [input] integer number 10 | 11 | a non-negative integer 12 | [input] integer width 13 | 14 | a positive integer representing the desired length 15 | [output] string 16 | 17 | the modified version of number as described above 18 | */ 19 | 20 | std::string integerToStringOfFixedWidth(int number, int width) { 21 | 22 | std::string result = ""; 23 | 24 | for (int i = 0; i < width; i++) { 25 | result += '0'; 26 | } 27 | 28 | int position = width-1; 29 | while (number && position >= 0) { 30 | result[position] = '0' + number % 10; 31 | number /= 10; 32 | position -= 1; 33 | } 34 | 35 | return result; 36 | } 37 | -------------------------------------------------------------------------------- /cpp/inversePermutation_3.cpp: -------------------------------------------------------------------------------- 1 | /* 2 | An inverse permutation is a permutation in which each number and the number of the place which it occupies are exchanged. 3 | 4 | Example 5 | 6 | inversePermutation([1, 3, 4, 2]) = [1, 4, 2, 3] 7 | 8 | [input] array.integer permutation 9 | 10 | non-empty array representing a permutation of integers from 1 to some n 11 | [output] array.integer 12 | 13 | the inverse permutation of permutation 14 | */ 15 | 16 | std::vector inversePermutation(std::vector permutation) { 17 | std::vector result(permutation.size()); 18 | for (int i = 0; i < permutation.size(); i++) { 19 | result[permutation[i] - 1] = i + 1; 20 | } 21 | return result; 22 | } 23 | -------------------------------------------------------------------------------- /cpp/isPower.cpp: -------------------------------------------------------------------------------- 1 | /* 2 | 3 | */ 4 | 5 | bool isPower(int n) { 6 | 7 | if (n == 1) { 8 | return true; 9 | } 10 | 11 | for (int i = 2; i < n; i++) { 12 | if (n % i == 0) { 13 | int tmp = n; 14 | while (n % i==0) { 15 | n /= i; 16 | } 17 | if (n == 1) { 18 | return true; 19 | } 20 | n = tmp; 21 | } 22 | } 23 | return false; 24 | } 25 | -------------------------------------------------------------------------------- /cpp/lastDigit.cpp: -------------------------------------------------------------------------------- 1 | /* 2 | For given integers A and B, find the last digit of A^B. 3 | 4 | Example 5 | 6 | for A = 2 and B = 5 output should be 2 since 2^5=32. 7 | 8 | [input] integer a 9 | 10 | positive integer 11 | [input] integer b 12 | 13 | non-negative integer 14 | [output] integer 15 | 16 | last digit of A^B 17 | */ 18 | 19 | 20 | int lastDigit(int a, int b) { 21 | 22 | int result = 1; 23 | for (int i = 0; i < b; i++) { 24 | result = (result * a) % 10; 25 | } 26 | return result; 27 | } 28 | -------------------------------------------------------------------------------- /cpp/makeArrayConsecutive.cpp: -------------------------------------------------------------------------------- 1 | /* 2 | iven an array of integers, we need to fill in the "holes" such that it contains all the integers from some range. 3 | 4 | Example 5 | 6 | makeArrayConsecutive([6, 2, 3, 8]) = [4, 5, 7] 7 | 8 | [input] array.integer sequence 9 | 10 | array of distinct integers 11 | [output] array.integer 12 | 13 | a sorted array of integers of minimal possible length such that its union with sequence contains every integer from an interval [L, R] (for some L, R) and no other integers. 14 | */ 15 | 16 | std::vector makeArrayConsecutive(std::vector sequence) { 17 | 18 | std::vector answer; 19 | int current_position = 0; 20 | 21 | sort(sequence.begin(), sequence.end()); 22 | for (int i = sequence[0]; i < sequence[(int)sequence.size() - 1]; i++) { 23 | if (sequence[current_position] != i) { 24 | answer.push_back(i); 25 | } 26 | else { 27 | current_position++; 28 | } 29 | } 30 | 31 | return answer; 32 | } 33 | -------------------------------------------------------------------------------- /cpp/partialSort.cpp: -------------------------------------------------------------------------------- 1 | /* 2 | Define partial sort of array as follows: first k elements of the result should be exactly the same as they will be in the sorted array and the rest elements should go in the same order as they occur in the original array. 3 | 4 | Apply a partial sort to a given array. 5 | 6 | Example 7 | 8 | partialSort([4, 3, 1, 2], 2) = [1, 2, 4, 3] 9 | 10 | [input] array.integer input 11 | 12 | unsorted array of distinct positive integers (each less than 109) 13 | [input] integer k 14 | 15 | non-negative integer 16 | [output] array.integer 17 | 18 | partially sorted input (for a given k) 19 | */ 20 | 21 | std::vector partialSort(std::vector input, int k) { 22 | std::vector answer; 23 | int infinity = int(1e9); 24 | 25 | for (int i = 0; i < k; i++) { 26 | int index = 0; 27 | for (int j = 0; j < input.size(); j++) { 28 | if (input[j] < input[index]) { 29 | index = j; 30 | } 31 | } 32 | answer.push_back(input[index]); 33 | input[index] = infinity; 34 | } 35 | for (int i = 0; i < input.size(); i++) { 36 | if (input[i] != infinity) { 37 | answer.push_back(input[i]); 38 | } 39 | } 40 | 41 | return answer; 42 | } 43 | -------------------------------------------------------------------------------- /cpp/phoneCall.cpp: -------------------------------------------------------------------------------- 1 | /* 2 | Some phone usage rate may be described as follows: 3 | 4 | first minute of a talk costs min1 cents, 5 | each minute from the 2nd up to 10th (inclusive) costs min2_10 cents 6 | each minute after 10th costs min11 cents. 7 | You have S cents on your account before the call. What is the duration of the longest call (in minutes) you can have? 8 | 9 | Example 10 | 11 | for min1 = 3, min2_10 = 1, min11 = 2, S = 20 output should be 14 12 | 13 | [input] integer min1 14 | 15 | positive integer 16 | [input] integer min2_10 17 | 18 | positive integer 19 | [input] integer min11 20 | 21 | positive integer 22 | [input] integer S 23 | 24 | positive integer 25 | [output] integer 26 | */ 27 | 28 | int phoneCall(int min1, int min2_10, int min11, int S) { 29 | 30 | if (S < min1) { 31 | return 0; 32 | } 33 | for (int i = 2; i <= 10; i++) { 34 | if (S < min1 + min2_10 * (i-1)) { 35 | return i - 1; 36 | } 37 | } 38 | return 10 + (S - min1 - min2_10 * 9) / min11; 39 | } 40 | -------------------------------------------------------------------------------- /cpp/powerRecursive.cpp: -------------------------------------------------------------------------------- 1 | /* 2 | Find the value of base raised to the power of exponent (base exponent). 3 | 4 | [input] integer base 5 | 6 | [input] integer exponent 7 | 8 | positive integer 9 | [output] integer 10 | */ 11 | 12 | int powerRecursive(int base, int exponent) { 13 | if (exponent == 0) { 14 | return 1; 15 | } 16 | else{ 17 | return base * powerRecursive(base, exponent - 1); 18 | } 19 | } 20 | 21 | -------------------------------------------------------------------------------- /cpp/primeFactors2.cpp: -------------------------------------------------------------------------------- 1 | std::vector primeFactors2(int n) { 2 | std::vector factors; 3 | int divisor = 2; 4 | 5 | while (n != 1) { 6 | if (n % divisor == 0) { 7 | factors.push_back(divisor); 8 | } 9 | while (n % divisor == 0) { 10 | n /= divisor; 11 | } 12 | divisor++; 13 | } 14 | 15 | return factors; 16 | } 17 | -------------------------------------------------------------------------------- /cpp/regularBracketSequence1.cpp: -------------------------------------------------------------------------------- 1 | /* 2 | Bracket sequence is called regular if it is possible to insert some numbers and signs into the sequence in such way that new sequence will represent correct arithmetic expression. 3 | 4 | For a string consisting of '(', ')' only determine whether it is regular bracket sequence or not. 5 | 6 | [input] string sequence 7 | 8 | [output] boolean 9 | 10 | true if the bracket sequence is regular, false otherwise 11 | */ 12 | bool regularBracketSequence1(std::string sequence) { 13 | 14 | int balance = 0; 15 | for (int i = 0; i < sequence.size(); i++) { 16 | if (sequence[i] == '(') { 17 | balance++; 18 | } 19 | else { 20 | balance--; 21 | } 22 | if (balance < 0) { 23 | return false; 24 | } 25 | } 26 | if (balance) { 27 | return false; 28 | } 29 | return true; 30 | } 31 | -------------------------------------------------------------------------------- /cpp/removeDuplicateStrings.cpp: -------------------------------------------------------------------------------- 1 | /* 2 | Lexicographic order means that the strings are arranged in a similar fashion as they are presumed to appear in a dictionary. 3 | 4 | Remove all duplicates from an already sorted array of strings. 5 | 6 | Example 7 | 8 | for ["a", "a", "ab", "ab", "abc"] output should be ["a", "ab", "abc"] 9 | 10 | [input] array.string inputArray 11 | 12 | non-empty sorted (in lexicographical order) array of strings with possible duplicates 13 | [output] array.string 14 | 15 | sorted (in lexicographical order) array consisting of all the strings from the inputArray except duplicates 16 | */ 17 | 18 | std::vector removeDuplicateStrings(std::vector inputArray) { 19 | std::vector result; 20 | for (int i = 0; i < inputArray.size(); i++) { 21 | if (i + 1 < inputArray.size() && inputArray[i] == inputArray[i + 1]) { 22 | continue; 23 | } 24 | result.push_back(inputArray[i]); 25 | } 26 | return result; 27 | } 28 | -------------------------------------------------------------------------------- /cpp/simplifyPath.cpp: -------------------------------------------------------------------------------- 1 | /* 2 | Given an absolute path for a file (Unix-style), simplify it. 3 | 4 | Example 5 | 6 | "/home/a/./x/../b//c/" -> "/home/a/b/c" 7 | 8 | [input] string path 9 | 10 | line containing some path 11 | [output] string 12 | 13 | simplified path 14 | */ 15 | std::string simplifyPath(std::string path) { 16 | 17 | /** 18 | * The 'split' function below can be considered as a 19 | * library function, it is guaranteed that it is 20 | * is written correctly. 21 | * The function returns a vector of the words in the given 22 | * string, using 'sep' as the delimiter. 23 | */ 24 | struct Helper { 25 | std::vector split(std::string line, char sep) { 26 | std::vector res; 27 | int start = 0; 28 | int end = line.find(sep); 29 | while (end != std::string::npos) { 30 | res.push_back(line.substr(start, end - start)); 31 | start = end + 1; 32 | end = line.find(sep, start); 33 | } 34 | res.push_back(line.substr(start, end)); 35 | return res; 36 | } 37 | }; 38 | Helper h; 39 | 40 | std::vector parts = h.split(path, '/'); 41 | std::vector simplified(parts.size()); 42 | int length = 0; 43 | for (int i = 0; i < parts.size(); i++) { 44 | std::string part = parts[i]; 45 | if (part == "." || part == "" || part == "..") { 46 | if (part == "..") { 47 | length--; 48 | } 49 | continue; 50 | } 51 | simplified[length++] = part; 52 | } 53 | 54 | if (length <= 0) { 55 | return "/"; 56 | } 57 | 58 | std::string result = ""; 59 | for (int i = 0; i < length; i++) { 60 | result += "/" + simplified[i]; 61 | } 62 | 63 | return result; 64 | } 65 | -------------------------------------------------------------------------------- /cpp/smallestMultiple.cpp: -------------------------------------------------------------------------------- 1 | /* 2 | Find the smallest integer that is divisible by all integers on a given interval [left, right]. 3 | 4 | Example 5 | 6 | smallestMultiple(2, 4) = 12 7 | 8 | [input] integer left 9 | 10 | left bound, positive integer 11 | [input] integer right 12 | 13 | right bound, left <= right 14 | [output] integer 15 | 16 | */ 17 | 18 | int smallestMultiple(int left, int right) { 19 | 20 | for (int candidate = 1; ; candidate++) { 21 | int correct = true; 22 | for (int divisor = left; divisor <= right; divisor++) { 23 | if (candidate % divisor == 0) { 24 | correct = false; 25 | break; 26 | } 27 | } 28 | if (correct) { 29 | return candidate; 30 | } 31 | } 32 | } 33 | -------------------------------------------------------------------------------- /cpp/swapArrayHalves.cpp: -------------------------------------------------------------------------------- 1 | /* 2 | Swap two halves of a given array. 3 | 4 | Example 5 | 6 | for [1, 3, 2, 1] output should be [2, 1, 1, 3] 7 | 8 | [input] array.integer inputArray 9 | 10 | array of integers of even length L 11 | [output] array.integer 12 | 13 | array consisting of last L/2 elements of the given inputArray followed by its first L/2 elements 14 | 15 | */ 16 | 17 | 18 | std::vector swapArrayHalves(std::vector inputArray) { 19 | 20 | for (int i = 0; i < inputArray.size()/2; i++) { 21 | int tmp = inputArray[i]; 22 | inputArray[i] = inputArray[i + inputArray.size() / 2]; 23 | inputArray[i + inputArray.size() / 2] = tmp; 24 | } 25 | return inputArray; 26 | } 27 | -------------------------------------------------------------------------------- /cpp/visitsOnCircularRoad.cpp: -------------------------------------------------------------------------------- 1 | /* 2 | There are N houses in a village on a circular road numbered from 1 to N starting from some house in clockwise order. It takes one minute to get from one house to any of two adjacent houses and there are no other roads in this village besides the circular one. Find the minimal time required to make all visits in the desired order starting from house 1 3 | 4 | Example 5 | 6 | visitsOnCircularRoad(4, [1, 3, 2, 3, 1]) = 6 7 | 8 | [input] integer N 9 | 10 | number of houses, positive integer 11 | [input] array.integer visitsOrder 12 | 13 | array of integers (each from 1 to N, inclusive) representing the order in which you have to visit the houses 14 | [output] integer 15 | */ 16 | 17 | int visitsOnCircularRoad(int N, std::vector visitsOrder) { 18 | 19 | int current = 1, 20 | res = 0; 21 | for (int i = 0; i < visitsOrder.size(); i++) { 22 | res += std::min(std::abs(visitsOrder[i] - current), 23 | N - std::abs(visitsOrder[i] - current)); 24 | current = visitsOrder[i]; 25 | } 26 | return res; 27 | } 28 | 29 | --------------------------------------------------------------------------------