├── 2021-Sep-26-task1.java ├── 2021-Sep-26-task2.java ├── 2021-Sep-26-task3.java ├── 2021-Sep-26-task4.java ├── 2021-Sep-27-task1.java ├── 2021-Sep-27-task2.java ├── 2021-Sep-27-task3.java ├── 2021-Sep-27-task4.java └── Arcade └── Intro ├── All Longest Strings.java ├── Make Array Consecutive 2.java ├── Sort by Height.java ├── add.java ├── adjacentElementsProduct.java ├── almostIncreasingSequence.java ├── alternatingSums.java ├── centuryFromYear.java ├── checkPalindrome.java ├── commonCharacterCount.java ├── isLucky.java ├── matrixElementsSum.java ├── reverseInParentheses.java └── shapeArea.java /2021-Sep-26-task1.java: -------------------------------------------------------------------------------- 1 | // You are given an array of integers a and two integers l and r. 2 | // You task is to calculate a boolean array b, 3 | // where b[i] = true if there exists an integer x, 4 | // such that a[i] = (i + 1) * x and l ≤ x ≤ r. 5 | // Otherwise, b[i] should be set to false. 6 | 7 | // Example 8 | 9 | // For a = [8, 5, 6, 16, 5], l = 1, and r = 3, 10 | // the output should be 11 | // boundedRatio(a, l, r) = [false, false, true, false, true]. 12 | 13 | // For a[0] = 8, 14 | // we need to find a value of x such that 1 * x = 8, 15 | // but the only value that would work is x = 8 which doesn't satisfy the boundaries 1 ≤ x ≤ 3, 16 | // so b[0] = false. 17 | // For a[1] = 5, 18 | // we need to find a value of x such that 2 * x = 5, 19 | // but there is no integer value that would satisfy this equation, so b[1] = false. 20 | // For a[2] = 6, 21 | // we can choose x = 2 because 3 * 2 = 6 and 1 ≤ 2 ≤ 3, 22 | // so b[2] = true. 23 | // For a[3] = 16, 24 | // there is no an integer 1 ≤ x ≤ 3, 25 | // such that 4 * x = 16, so b[3] = false. 26 | // For a[4] = 5, 27 | // we can choose x = 1 because 5 * 1 = 5 and 1 ≤ 1 ≤ 3, 28 | // so b[4] = true. 29 | // Input/Output 30 | 31 | // [execution time limit] 3 seconds (java) 32 | 33 | // [input] array.integer a 34 | 35 | // An array of integers. 36 | 37 | // Guaranteed constraints: 38 | // 1 ≤ a.length ≤ 100, 39 | // 1 ≤ a[i] ≤ 106. 40 | 41 | // [input] integer l 42 | 43 | // An integer representing the lower bound for x. 44 | 45 | // Guaranteed constraints: 46 | // 1 ≤ l ≤ 104. 47 | 48 | // [input] integer r 49 | 50 | // An integer representing the upper bound for x. 51 | 52 | // Guaranteed constraints: 53 | // 1 ≤ r ≤ 104, 54 | // l ≤ r. 55 | 56 | // [output] array.boolean 57 | 58 | // A boolean array. 59 | 60 | // Solution 61 | boolean[] boundedRatio(int[] a, int l, int r) { 62 | boolean[] b = new boolean[a.length]; 63 | 64 | for (int i = 0; i < a.length; ++i) { 65 | for (int x = l; x <= r; ++x) { 66 | if (a[i] == (i + 1) * x) { 67 | b[i] = true; 68 | } 69 | } 70 | } 71 | 72 | return b; 73 | } 74 | 75 | // TC: O(a.length * (r - l); SC: O(1) 76 | -------------------------------------------------------------------------------- /2021-Sep-26-task2.java: -------------------------------------------------------------------------------- 1 | // You are given an array of integers a. 2 | // A new array b is generated by rearranging the elements of a in the following way: 3 | 4 | // b[0] is equal to a[0]; 5 | // b[1] is equal to the last element of a; 6 | // b[2] is equal to a[1]; 7 | // b[3] is equal to the second-last element of a; 8 | // b[4] is equal to a[2]; 9 | // b[5] is equal to the third-last element of a; 10 | // and so on. 11 | // Your task is to determine whether the new array b is sorted in strictly ascending order or not. 12 | 13 | // Here is how the process of generating the new array b works: 14 | 15 | // Example 16 | 17 | // For a = [1, 3, 5, 6, 4, 2], the output should be alternatingSort(a) = true. 18 | 19 | // The new array b will look like [1, 2, 3, 4, 5, 6], 20 | // which is in strictly ascending order, so the answer is true. 21 | 22 | // For a = [1, 4, 5, 6, 3], 23 | // the output should be alternatingSort(a) = false. 24 | 25 | // The new array b will look like [1, 3, 4, 6, 5], 26 | // which is not in strictly ascending order, so the answer is false. 27 | 28 | // Input/Output 29 | 30 | // [execution time limit] 3 seconds (java) 31 | 32 | // [input] array.integer a 33 | 34 | // The given array of integers. 35 | 36 | // Guaranteed constraints: 37 | // 1 ≤ a.length ≤ 105, 38 | // -109 ≤ a[i] ≤ 109. 39 | 40 | // [output] boolean 41 | 42 | // A boolean representing whether the new array b will be sorted in strictly ascending order or not. 43 | 44 | // Solution 45 | boolean alternatingSort(int[] a) { 46 | if (a.length < 2) { 47 | return true; 48 | } 49 | 50 | int[] b = new int[a.length]; 51 | int start = 0; 52 | int end = a.length - 1; 53 | 54 | for (int i = 0; i < a.length; ++i) { 55 | b[i] = a[start]; 56 | 57 | if (i > 0 && b[i - 1] >= b[i]) { 58 | return false; 59 | } 60 | 61 | if (i < a.length - 1) { 62 | b[++i] = a[end]; 63 | 64 | if (b[i - 1] >= b[i]) { 65 | return false; 66 | } 67 | } 68 | 69 | ++start; 70 | --end; 71 | } 72 | 73 | return true; 74 | } 75 | 76 | // TC: O(a.length); SC: O(1) 77 | -------------------------------------------------------------------------------- /2021-Sep-26-task3.java: -------------------------------------------------------------------------------- 1 | Given a square matrix of integers m, your task is to rearrange its numbers in the following way: 2 | 3 | First, sort its values in ascending order of how frequently the number occurs in m. In the case of a tie, sort the equally frequent numbers by their values, in ascending order. 4 | Second, place the sorted numbers diagonally, starting from the bottom right corner, like this: 5 | element ordering 6 | Example 7 | 8 | For 9 | 10 | m = [[ 1, 4, -2], 11 | [-2, 3, 4], 12 | [ 3, 1, 3]] 13 | the output should be 14 | 15 | sortMatrixByOccurrences(m) = [[3, 3, 4], 16 | [3, 4, 1], 17 | [1, -2, -2]] 18 | First we look at the frequency of each number: 19 | 20 | Number 1 occurs 2 times; 21 | Number -2 occurs 2 times; 22 | Number 3 occurs 3 times; 23 | Number 4 occurs 2 times. 24 | Because numbers 1, -2, and 4 occur the same number of times, we sort them by their values in ascending order. Number 3 occurs the most numbers of times, so it goes after all other numbers. Finally, after sorting we get the following array: [-2, -2, 1, 1, 4, 4, 3, 3, 3] 25 | 26 | After sorting, the numbers should be placed diagonally starting from the bottom right corner, as follows: 27 | 28 | [[3, 3, 4], 29 | [3, 4, 1], 30 | [1, -2, -2]] 31 | Input/Output 32 | 33 | [execution time limit] 3 seconds (java) 34 | 35 | [input] array.array.integer m 36 | 37 | A square matrix of integers. 38 | 39 | Guaranteed constraints: 40 | 1 ≤ m.length ≤ 40, 41 | m[i].length = m.length, 42 | -1000 ≤ m[i][j] ≤ 1000. 43 | 44 | [output] array.array.integer 45 | 46 | The matrix m rearranged according to the specifications above. 47 | 48 | -------------------------------------------------------------------------------- /2021-Sep-26-task4.java: -------------------------------------------------------------------------------- 1 | Given an array of integers a and a set of queries of the form [l, r, x], your task is to calculate the number of occurrences of the number x in the inclusive subarray a[l..r] (0-based), for each query. Return the sum of the answers for all queries as the result. 2 | 3 | Example 4 | 5 | For a = [1, 2, 1, 3, 1, 2, 1] and 6 | 7 | queries = [ 8 | [1, 3, 3], 9 | [0, 4, 1], 10 | [2, 5, 2], 11 | [5, 6, 1] 12 | ] 13 | the output should be countOnSubarrays(a, queries) = 6. 14 | 15 | The answer to the first query is 1: the number 3 appears 1 time in the subarray [2, 1, 3]; 16 | The answer to the second query is 3: the number 1 appears 3 times in the subarray [1, 2, 1, 3, 1]; 17 | The answer to the third query is 1: the number 2 appears 1 time in the subarray [1, 3, 1, 2]; 18 | The answer to the fourth query is 1: the number 1 appears 1 time in the subarray [2, 1]. 19 | So the answer is 1 + 3 + 1 + 1 = 6. 20 | Input/Output 21 | 22 | [execution time limit] 3 seconds (java) 23 | 24 | [input] array.integer a 25 | 26 | An array of integers. 27 | 28 | Guaranteed constraints: 29 | 1 ≤ a.length ≤ 104, 30 | 1 ≤ a[i] ≤ 100. 31 | 32 | [input] array.array.integer queries 33 | 34 | An array of queries, where queries[i] contains three numbers: l, r and x. 35 | 36 | Guaranteed constraints: 37 | 3 ≤ queries.length ≤ 105, 38 | queries[i].length = 3, 39 | 0 ≤ queries[i][0] ≤ queries[i][1] < a.length, 40 | 1 ≤ queries[i][2] ≤ 100. 41 | 42 | [output] integer 43 | 44 | The sum of the answers to all queries. 45 | -------------------------------------------------------------------------------- /2021-Sep-27-task1.java: -------------------------------------------------------------------------------- 1 | // Given an integer n and an array a of length n, 2 | // your task is to apply the following mutation to a: 3 | // Array a mutates into a new array b of length n. 4 | // For each i from 0 to n - 1, b[i] = a[i - 1] + a[i] + a[i + 1]. 5 | // If some element in the sum a[i - 1] + a[i] + a[i + 1] does not exist, 6 | // it should be set to 0. For example, b[0] should be equal to 0 + a[0] + a[1]. 7 | 8 | // Example 9 | 10 | // For n = 5 and a = [4, 0, 1, -2, 3], the output should be mutateTheArray(n, a) = [4, 5, -1, 2, 1]. 11 | // b[0] = 0 + a[0] + a[1] = 0 + 4 + 0 = 4 12 | // b[1] = a[0] + a[1] + a[2] = 4 + 0 + 1 = 5 13 | // b[2] = a[1] + a[2] + a[3] = 0 + 1 + (-2) = -1 14 | // b[3] = a[2] + a[3] + a[4] = 1 + (-2) + 3 = 2 15 | // b[4] = a[3] + a[4] + 0 = (-2) + 3 + 0 = 1 16 | // So, the resulting array after the mutation will be [4, 5, -1, 2, 1]. 17 | 18 | // Input/Output 19 | // [execution time limit] 3 seconds (java) 20 | // [input] integer n 21 | // An integer representing the length of the given array. 22 | // Guaranteed constraints: 23 | // 1 ≤ n ≤ 103. 24 | // [input] array.integer a 25 | // An array of integers that needs to be mutated. 26 | // Guaranteed constraints: 27 | // a.length = n, 28 | // -103 ≤ a[i] ≤ 103. 29 | // [output] array.integer 30 | // The resulting array after the mutation. 31 | 32 | int[] mutateTheArray(int n, int[] a) { 33 | if (n == 1) { 34 | return a; 35 | } 36 | 37 | int[] b = new int[n]; 38 | 39 | for (int i = 0; i < n; ++i) { 40 | if (i == 0 || i == n - 1) { 41 | if (i == 0) { 42 | b[i] = 0 + a[i] + a[i + 1]; 43 | } 44 | else { 45 | b[i] = a[i - 1] + a[i] + 0; 46 | } 47 | } 48 | else { 49 | b[i] = a[i - 1] + a[i] + a[i + 1]; 50 | } 51 | } 52 | 53 | return b; 54 | } 55 | 56 | // TC: O(n); SC: O(1) 57 | -------------------------------------------------------------------------------- /2021-Sep-27-task2.java: -------------------------------------------------------------------------------- 1 | // Let's say the string word is an occurrence of the string sequence if sequence contains word as a substring. 2 | // Let's say the string word is a k-occurrence of the string sequence if sequence contains word repeated ktimes as a substring. 3 | // Note that if word is an occurrence of sequence, 4 | // it is a 1-occurrence as well. 5 | // For example, 6 | // if word = "ab" and sequence = "dabcacab", 7 | // then word is a 1-occurrence of sequence but not a 2-occurrence, 8 | // because sequence doesn't contain "abab" as a substring. 9 | // On the other hand, 10 | // the string "ca" is a 2-occurrence of sequence, 11 | // since it contains "caca" as a substring. 12 | // Given a string sequence and an array of strings words, 13 | // your task is to find the maximal value of k for each element, 14 | // such that words[i] is a k-occurrence of sequence. 15 | // Return the k-values as an array of integers of length words.length. 16 | 17 | // Example 18 | 19 | // For sequence = "ababcbabc" and words = ["ab", "babc", "bca"], the output should be maxKOccurrences(sequence, words) = [2, 2, 0]. 20 | // words[0] = "ab" is a 2-occurrence of sequence, because sequence[0..4] = "abab"; 21 | // words[0] = "ab" is not a 3-occurence of sequence, because there is no substring "ababab" in sequence; 22 | // words[1] = "babc" is a 2-occurrence of sequence, because sequence[1..8] = "babcbabc"; 23 | // words[1] = "babc" is not a 3-occurence of sequence, because there is no substring "babcbabcbabc" in sequence; 24 | // words[2] = "bca" is a 0-occurrence of sequence, because there is no substring "bca" in sequence. 25 | 26 | // Input/Output 27 | // [execution time limit] 3 seconds (java) 28 | // [input] string sequence 29 | // A string consisting of lowercase English letters. 30 | // Guaranteed constraints: 31 | // 1 ≤ sequence.length ≤ 100. 32 | // [input] array.string words 33 | // An array of strings, each of which consists of lowercase English letters. 34 | // Guaranteed constraints: 35 | // 1 ≤ words.length ≤ 50, 36 | // 1 ≤ words[i].length ≤ 10. 37 | // [output] array.integer 38 | // Return an array of integers, as described in the description. 39 | 40 | -------------------------------------------------------------------------------- /2021-Sep-27-task3.java: -------------------------------------------------------------------------------- 1 | // You are implementing your own programming language and you've decided to add support for merging strings. 2 | // A typical merge function would take two strings s1 and s2, 3 | // and return the lexicographically smallest result that can be obtained by placing the symbols of s2 between the symbols of s1 4 | // in such a way that maintains the relative order of the characters in each string. 5 | // For example, 6 | // if s1 = "super" and s2 = "tower", 7 | // the result should be merge(s1, s2) = "stouperwer". 8 | 9 | // You'd like to make your language more unique, 10 | // so for your merge function, 11 | // instead of comparing the characters in the usual lexicographical order, 12 | // you'll compare them based on how many times they occur in their respective initial strings (fewer occurrences means the character is considered smaller). 13 | // If the number of occurrences are equal, 14 | // then the characters should be compared in the usual lexicographical way. 15 | // If both number of occurences and characters are equal, 16 | // you should take the characters from the first string to the result. 17 | // Note that occurrences in the initial strings are compared - they do not change over the merge process. 18 | // Given two strings s1 and s2, return the result of the special merge function you are implementing. 19 | 20 | // Example 21 | // For s1 = "dce" and s2 = "cccbd", 22 | // the output should be 23 | // mergeStrings(s1, s2) = "dcecccbd". 24 | // All symbols from s1 goes first, 25 | // because all of them have only 1 occurrence in s1 and c has 3 occurrences in s2. 26 | // For s1 = "super" and s2 = "tower", 27 | // the output should be 28 | // mergeStrings(s1, s2) = "stouperwer". 29 | // Because in both strings all symbols occur only 1 time, 30 | // strings are merged as usual. 31 | // You can find explanation for this example on the image in the description. 32 | 33 | // Input/Output 34 | // [execution time limit] 3 seconds (java) 35 | // [input] string s1 36 | // A string consisting only of lowercase English letters. 37 | // Guaranteed constraints: 38 | // 1 ≤ s1.length ≤ 104. 39 | // [input] string s2 40 | // A string consisting only of lowercase English letters. 41 | // Guaranteed constraints: 42 | // 1 ≤ s2.length ≤ 104. 43 | // [output] string 44 | // The string that results by merging s1 and s2 using your special merge function. 45 | -------------------------------------------------------------------------------- /2021-Sep-27-task4.java: -------------------------------------------------------------------------------- 1 | // Given an array of integers a of even length, 2 | // your task is to split it into two arrays of equal length such that all the numbers are unique in each of them. 3 | // There may be more than one possible answer, 4 | // in which case you may return any of them. 5 | // If there are no possible answers, 6 | // return an empty array. 7 | 8 | // Hint: Count the number of occurrences of each integer in a. 9 | // If there are integers occurring more than twice, 10 | // then there is no solution. 11 | // Next, put the integers occurring twice into both answer arrays. 12 | // Finally, 13 | // put all other numbers in the answer arrays, 14 | // following the condition that they should have equal sizes. 15 | 16 | // Example 17 | 18 | // For a = [2, 1, 2, 3, 3, 4], the output can be divideArray(a) = [[2, 1, 3], [2, 3, 4]]. 19 | // Answers like [[1, 2, 3], [2, 3, 4]] or [[4, 2, 3], [3, 2, 1]]would also be considered correct. 20 | 21 | // For a = [1, 2, 2, 1], the output can be divideArray(a) = [[1, 2], [2, 1]]. 22 | // Again, there are other possible answers. 23 | 24 | // For a = [2, 2, 3, 3, 2, 2], the output should be divideArray(a) = []. 25 | // No matter how we try to split this array, 26 | // there will be at least two 2s in at least one of the resulting arrays. 27 | // So the answer is []. 28 | 29 | // Input/Output 30 | // [execution time limit] 3 seconds (java) 31 | // [input] array.integer a 32 | // An array of integers. It is guaranteed that a has even length. 33 | // Guaranteed constraints: 34 | // 2 ≤ a.length ≤ 104, 35 | // 1 ≤ a[i] ≤ 105. 36 | // [output] array.array.integer 37 | // Return an empty array if there is no solution. 38 | // If a solution exists, 39 | // return an array of two arrays - a distribution of a where each of these two arrays are of equal length and each contains unique elements. 40 | 41 | int[][] divideArray(int[] a) { 42 | int len = a.length; 43 | int[][] ans = new int[2][]; 44 | Map cnt = new HashMap<>(); 45 | Map position = new HashMap<>(); 46 | boolean flag = true; 47 | 48 | for (int i = 0; i < len; ++i) { 49 | cnt.put(a[i], cnt.getOrDefault(a[i], 0) + 1); 50 | 51 | if (cnt.get(a[i]) > 2) { 52 | return new int[2][]; 53 | } 54 | else if (cnt.get(a[i]) == 2) { 55 | if (position.get(a[i]) == 1) { 56 | ans[0][i / 2] = a[i]; 57 | } 58 | else { 59 | ans[1][i / 2] = a[i]; 60 | } 61 | } 62 | else { 63 | if (flag) { 64 | ans[0][i / 2] = a[i]; 65 | position.put(a[i], 0); 66 | } 67 | else { 68 | ans[1][i / 2] = a[i]; 69 | position.put(a[i], 1); 70 | } 71 | } 72 | } 73 | 74 | return ans; 75 | } 76 | -------------------------------------------------------------------------------- /Arcade/Intro/All Longest Strings.java: -------------------------------------------------------------------------------- 1 | // Given an array of strings, 2 | // return another array containing all of its longest strings. 3 | 4 | // Example 5 | 6 | // For inputArray = ["aba", "aa", "ad", "vcd", "aba"], 7 | // the output should be 8 | // allLongestStrings(inputArray) = ["aba", "vcd", "aba"]. 9 | 10 | // Input/Output 11 | 12 | // [execution time limit] 3 seconds (java) 13 | 14 | // [input] array.string inputArray 15 | 16 | // A non-empty array. 17 | 18 | // Guaranteed constraints: 19 | // 1 ≤ inputArray.length ≤ 10, 20 | // 1 ≤ inputArray[i].length ≤ 10. 21 | 22 | // [output] array.string 23 | 24 | // Array of the longest strings, 25 | // stored in the same order as in the inputArray. 26 | 27 | // Solution 28 | String[] allLongestStrings(String[] inputArray) { 29 | if (inputArray.length == 1) { 30 | return inputArray; 31 | } 32 | 33 | int end = 1; 34 | int len = inputArray[0].length(); 35 | 36 | for (int i = 1; i < inputArray.length; ++i) { 37 | if (inputArray[i].length() == len) { 38 | inputArray[end] = inputArray[i]; 39 | ++end; 40 | } 41 | else if (inputArray[i].length() > len) { 42 | inputArray[0] = inputArray[i]; 43 | end = 1; 44 | len = inputArray[i].length(); 45 | } 46 | } 47 | 48 | return Arrays.copyOf(inputArray, end); 49 | } 50 | 51 | // TC: O(n); SC: O(1) 52 | -------------------------------------------------------------------------------- /Arcade/Intro/Make Array Consecutive 2.java: -------------------------------------------------------------------------------- 1 | // Ratiorg got statues of different sizes as a present from CodeMaster for his birthday, 2 | // each statue having an non-negative integer size. Since he likes to make things perfect, 3 | // he wants to arrange them from smallest to largest so that each statue will be bigger than the previous one exactly by 1. 4 | // He may need some additional statues to be able to accomplish that. 5 | // Help him figure out the minimum number of additional statues needed. 6 | 7 | // Example 8 | 9 | // For statues = [6, 2, 3, 8], the output should be 10 | // makeArrayConsecutive2(statues) = 3. 11 | 12 | // Ratiorg needs statues of sizes 4, 5 and 7. 13 | 14 | // Input/Output 15 | 16 | // [execution time limit] 3 seconds (java) 17 | 18 | // [input] array.integer statues 19 | 20 | // An array of distinct non-negative integers. 21 | 22 | // Guaranteed constraints: 23 | // 1 ≤ statues.length ≤ 10, 24 | // 0 ≤ statues[i] ≤ 20. 25 | 26 | // [output] integer 27 | 28 | // The minimal number of statues that need to be added to existing statues 29 | // such that it contains every integer size from an interval [L, R] (for some L, R) and no other sizes. 30 | 31 | // Solution 32 | int makeArrayConsecutive2(int[] statues) { 33 | if (statues.length == 1) { 34 | return 0; 35 | } 36 | 37 | int max = Integer.MIN_VALUE; 38 | int min = Integer.MAX_VALUE; 39 | 40 | for (int i = 0; i < statues.length; ++i) { 41 | max = Math.max(max, statues[i]); 42 | min = Math.min(min, statues[i]); 43 | } 44 | 45 | return max - min + 1 - statues.length; 46 | } 47 | 48 | // TC: O(n); SC: O(1) 49 | -------------------------------------------------------------------------------- /Arcade/Intro/Sort by Height.java: -------------------------------------------------------------------------------- 1 | // Some people are standing in a row in a park. 2 | // There are trees between them which cannot be moved. 3 | // Your task is to rearrange the people by their heights in a non-descending order without moving the trees. 4 | // People can be very tall! 5 | 6 | // Example 7 | 8 | // For a = [-1, 150, 190, 170, -1, -1, 160, 180], 9 | // the output should be 10 | // sortByHeight(a) = [-1, 150, 160, 170, -1, -1, 180, 190]. 11 | 12 | // Input/Output 13 | 14 | // [execution time limit] 3 seconds (java) 15 | 16 | // [input] array.integer a 17 | 18 | // If a[i] = -1, then the ith position is occupied by a tree. 19 | // Otherwise a[i] is the height of a person standing in the ith position. 20 | 21 | // Guaranteed constraints: 22 | // 1 ≤ a.length ≤ 1000, 23 | // -1 ≤ a[i] ≤ 1000. 24 | 25 | // [output] array.integer 26 | 27 | // Sorted array a with all the trees untouched. 28 | 29 | // Solution 30 | int[] sortByHeight(int[] a) { 31 | if (a.length == 1) { 32 | return a; 33 | } 34 | 35 | List b = new ArrayList<>(); 36 | 37 | for (int i = 0; i < a.length; ++i) { 38 | if (a[i] != -1) { 39 | b.add(a[i]); 40 | a[i] = 0; 41 | } 42 | } 43 | 44 | Collections.sort(b); 45 | 46 | int j = 0; 47 | 48 | for (int i = 0; i < a.length; ++i) { 49 | if (a[i] == 0) { 50 | a[i] = b.get(j); 51 | ++j; 52 | } 53 | } 54 | 55 | return a; 56 | } 57 | 58 | // TC: O(nlogn); SC: O(n) 59 | -------------------------------------------------------------------------------- /Arcade/Intro/add.java: -------------------------------------------------------------------------------- 1 | // Write a function that returns the sum of two numbers. 2 | 3 | // Example 4 | 5 | // For param1 = 1 and param2 = 2, the output should be 6 | // add(param1, param2) = 3. 7 | 8 | // Input/Output 9 | 10 | // [execution time limit] 3 seconds (java) 11 | 12 | // [input] integer param1 13 | 14 | // Guaranteed constraints: 15 | // -1000 ≤ param1 ≤ 1000. 16 | 17 | // [input] integer param2 18 | 19 | // Guaranteed constraints: 20 | // -1000 ≤ param2 ≤ 1000. 21 | 22 | // [output] integer 23 | 24 | // The sum of the two inputs. 25 | 26 | // Solution 27 | int add(int param1, int param2) { 28 | return param1 + param2; 29 | } 30 | 31 | // TC: O(1); SC: O(1) 32 | -------------------------------------------------------------------------------- /Arcade/Intro/adjacentElementsProduct.java: -------------------------------------------------------------------------------- 1 | // Given an array of integers, 2 | // find the pair of adjacent elements that has the largest product and return that product. 3 | 4 | // Example 5 | 6 | // For inputArray = [3, 6, -2, -5, 7, 3], 7 | // the output should be 8 | // adjacentElementsProduct(inputArray) = 21. 9 | 10 | // 7 and 3 produce the largest product. 11 | 12 | // Input/Output 13 | 14 | // [execution time limit] 3 seconds (java) 15 | 16 | // [input] array.integer inputArray 17 | 18 | // An array of integers containing at least two elements. 19 | 20 | // Guaranteed constraints: 21 | // 2 ≤ inputArray.length ≤ 10, 22 | // -1000 ≤ inputArray[i] ≤ 1000. 23 | 24 | // [output] integer 25 | 26 | // The largest product of adjacent elements. 27 | 28 | // Solution 29 | int adjacentElementsProduct(int[] inputArray) { 30 | int ans = Integer.MIN_VALUE; 31 | 32 | for (int i = 0; i < inputArray.length - 1; ++i) { 33 | int temp = inputArray[i] * inputArray[i + 1]; 34 | ans = Math.max(ans, temp); 35 | } 36 | 37 | return ans; 38 | } 39 | 40 | // TC: O(n); SC: O(1) 41 | -------------------------------------------------------------------------------- /Arcade/Intro/almostIncreasingSequence.java: -------------------------------------------------------------------------------- 1 | // Given a sequence of integers as an array, 2 | // determine whether it is possible to obtain a strictly increasing sequence by removing no more than one element from the array. 3 | 4 | // Note: sequence a0, a1, ..., an is considered to be a strictly increasing if a0 < a1 < ... < an. 5 | // Sequence containing only one element is also considered to be strictly increasing. 6 | 7 | // Example 8 | 9 | // For sequence = [1, 3, 2, 1], the output should be 10 | // almostIncreasingSequence(sequence) = false. 11 | 12 | // There is no one element in this array that can be removed in order to get a strictly increasing sequence. 13 | 14 | // For sequence = [1, 3, 2], the output should be 15 | // almostIncreasingSequence(sequence) = true. 16 | 17 | // You can remove 3 from the array to get the strictly increasing sequence [1, 2]. 18 | // Alternately, 19 | // you can remove 2 to get the strictly increasing sequence [1, 3]. 20 | 21 | // Input/Output 22 | 23 | // [execution time limit] 3 seconds (java) 24 | 25 | // [input] array.integer sequence 26 | 27 | // Guaranteed constraints: 28 | // 2 ≤ sequence.length ≤ 105, 29 | // -105 ≤ sequence[i] ≤ 105. 30 | 31 | // [output] boolean 32 | 33 | // Return true if it is possible to remove one element from the array in order to get a strictly increasing sequence, 34 | // otherwise return false. 35 | 36 | // Solution 37 | boolean almostIncreasingSequence(int[] sequence) { 38 | if (sequence.length < 3) { 39 | return true; 40 | } 41 | 42 | if (sequence.length == 3) { 43 | if (sequence[0] >= sequence[1] && sequence[1] >= sequence[2]) { 44 | return false; 45 | } 46 | else { 47 | return true; 48 | } 49 | } 50 | 51 | int cnt = 0; 52 | 53 | for (int i = 0; i < sequence.length - 2; ++i) { 54 | if (sequence[i] >= sequence[i + 1]) { 55 | if (i > 0 && (sequence[i - 1] >= sequence[i + 1] && sequence[i] >= sequence[i + 2])) { 56 | return false; 57 | } 58 | 59 | ++cnt; 60 | 61 | if (cnt > 1) { 62 | return false; 63 | } 64 | } 65 | } 66 | 67 | if (cnt > 0 && sequence[sequence.length - 2] >= sequence[sequence.length - 1]) { 68 | return false; 69 | } 70 | 71 | return true; 72 | } 73 | 74 | // TC: O(n); SC: O(1) 75 | -------------------------------------------------------------------------------- /Arcade/Intro/alternatingSums.java: -------------------------------------------------------------------------------- 1 | // Several people are standing in a row and need to be divided into two teams. 2 | // The first person goes into team 1, 3 | // the second goes into team 2, 4 | // the third goes into team 1 again, 5 | // the fourth into team 2, 6 | // and so on. 7 | 8 | // You are given an array of positive integers - the weights of the people. 9 | // Return an array of two integers, 10 | // where the first element is the total weight of team 1, 11 | // and the second element is the total weight of team 2 after the division is complete. 12 | 13 | // Example 14 | // For a = [50, 60, 60, 45, 70], 15 | // the output should be 16 | // solution(a) = [180, 105]. 17 | 18 | // Input/Output 19 | 20 | // [execution time limit] 3 seconds (java) 21 | 22 | // [input] array.integer a 23 | 24 | // Guaranteed constraints: 25 | // 1 ≤ a.length ≤ 105, 26 | // 45 ≤ a[i] ≤ 100. 27 | 28 | // [output] array.integer 29 | 30 | // Solution 31 | int[] solution(int[] a) { 32 | if (a == null || a.length == 0) { 33 | return new int[0]; 34 | } 35 | 36 | int weightEven = 0; 37 | int weightOdd = 0; 38 | 39 | for (int i = 0; i < a.length; i++) { 40 | if (i % 2 == 0) { 41 | weightEven += a[i]; 42 | } 43 | else { 44 | weightOdd += a[i]; 45 | } 46 | } 47 | 48 | return new int[] {weightEven, weightOdd}; 49 | } 50 | // TC: O(n); SC: O(1) 51 | -------------------------------------------------------------------------------- /Arcade/Intro/centuryFromYear.java: -------------------------------------------------------------------------------- 1 | // Given a year, return the century it is in. 2 | // The first century spans from the year 1 up to and including the year 100, 3 | // the second - from the year 101 up to and including the year 200, etc. 4 | 5 | // Example 6 | 7 | // For year = 1905, the output should be 8 | // centuryFromYear(year) = 20; 9 | // For year = 1700, the output should be 10 | // centuryFromYear(year) = 17. 11 | // Input/Output 12 | 13 | // [execution time limit] 3 seconds (java) 14 | 15 | // [input] integer year 16 | 17 | // A positive integer, designating the year. 18 | 19 | // Guaranteed constraints: 20 | // 1 ≤ year ≤ 2005. 21 | 22 | // [output] integer 23 | 24 | // The number of the century the year is in. 25 | 26 | // Solution 27 | int centuryFromYear(int year) { 28 | int ans = year / 100; 29 | int res = year % 100; 30 | 31 | if (res == 0) { 32 | return ans; 33 | } 34 | else { 35 | return ans + 1; 36 | } 37 | } 38 | 39 | // TC: O(1); SC: O(1) 40 | -------------------------------------------------------------------------------- /Arcade/Intro/checkPalindrome.java: -------------------------------------------------------------------------------- 1 | // Given the string, 2 | // check if it is a palindrome. 3 | 4 | // Example 5 | 6 | // For inputString = "aabaa", the output should be 7 | // checkPalindrome(inputString) = true; 8 | // For inputString = "abac", the output should be 9 | // checkPalindrome(inputString) = false; 10 | // For inputString = "a", the output should be 11 | // checkPalindrome(inputString) = true. 12 | // Input/Output 13 | 14 | // [execution time limit] 3 seconds (java) 15 | 16 | // [input] string inputString 17 | 18 | // A non-empty string consisting of lowercase characters. 19 | 20 | // Guaranteed constraints: 21 | // 1 ≤ inputString.length ≤ 105. 22 | 23 | // [output] boolean 24 | 25 | // true if inputString is a palindrome, false otherwise. 26 | 27 | // Solution 28 | boolean checkPalindrome(String inputString) { 29 | if (inputString.length() < 2) { 30 | return true; 31 | } 32 | 33 | int start = 0; 34 | int end = inputString.length() - 1; 35 | 36 | while (start < end) { 37 | if (inputString.charAt(start) != inputString.charAt(end)) { 38 | return false; 39 | } 40 | 41 | ++start; 42 | --end; 43 | } 44 | 45 | return true; 46 | } 47 | 48 | // TC: O(n); SC:O(1) 49 | -------------------------------------------------------------------------------- /Arcade/Intro/commonCharacterCount.java: -------------------------------------------------------------------------------- 1 | // Given two strings, 2 | // find the number of common characters between them. 3 | 4 | // Example 5 | 6 | // For s1 = "aabcc" and s2 = "adcaa", 7 | // the output should be 8 | // commonCharacterCount(s1, s2) = 3. 9 | 10 | // Strings have 3 common characters - 2 "a"s and 1 "c". 11 | 12 | // Input/Output 13 | 14 | // [execution time limit] 3 seconds (java) 15 | 16 | // [input] string s1 17 | 18 | // A string consisting of lowercase English letters. 19 | 20 | // Guaranteed constraints: 21 | // 1 ≤ s1.length < 15. 22 | 23 | // [input] string s2 24 | 25 | // A string consisting of lowercase English letters. 26 | 27 | // Guaranteed constraints: 28 | // 1 ≤ s2.length < 15. 29 | 30 | // [output] integer 31 | 32 | // Solution 33 | int commonCharacterCount(String s1, String s2) { 34 | Map hm = new HashMap<>(); 35 | int cnt = 0; 36 | 37 | helperMap(s1, hm); 38 | 39 | for (int i = 0; i < s2.length(); ++i) { 40 | char ch = s2.charAt(i); 41 | 42 | if (hm.containsKey(ch)) { 43 | ++cnt; 44 | hm.put(ch, hm.get(ch) - 1); 45 | 46 | if (hm.get(ch) == 0) { 47 | hm.remove(ch); 48 | } 49 | } 50 | } 51 | 52 | return cnt; 53 | } 54 | 55 | void helperMap(String s1, Map hm) { 56 | for (int i = 0; i < s1.length(); ++i) { 57 | hm.put(s1.charAt(i), hm.getOrDefault(s1.charAt(i), 0) + 1); 58 | } 59 | } 60 | 61 | // TC: O(n); SC: O(n) 62 | -------------------------------------------------------------------------------- /Arcade/Intro/isLucky.java: -------------------------------------------------------------------------------- 1 | // Ticket numbers usually consist of an even number of digits. 2 | // 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. 3 | 4 | // Given a ticket number n, 5 | // determine if it's lucky or not. 6 | 7 | // Example 8 | 9 | // For n = 1230, the output should be 10 | // isLucky(n) = true; 11 | // For n = 239017, the output should be 12 | // isLucky(n) = false. 13 | // Input/Output 14 | 15 | // [execution time limit] 3 seconds (java) 16 | 17 | // [input] integer n 18 | 19 | // A ticket number represented as a positive integer with an even number of digits. 20 | 21 | // Guaranteed constraints: 22 | // 10 ≤ n < 106. 23 | 24 | // [output] boolean 25 | 26 | // true if n is a lucky ticket number, false otherwise. 27 | 28 | // Solution 29 | boolean isLucky(int n) { 30 | List digits = new ArrayList<>(); 31 | int cnt = 0; 32 | int digit = 0; 33 | 34 | while (n != 0) { 35 | digit += n % 10; 36 | n /= 10; 37 | 38 | digits.add(digit); 39 | ++cnt; 40 | } 41 | 42 | if (digits.get(cnt - 1) - digits.get(cnt / 2 - 1) * 2 == 0) { 43 | return true; 44 | } 45 | 46 | return false; 47 | } 48 | 49 | // TC: O(n); SC: O(n) 50 | -------------------------------------------------------------------------------- /Arcade/Intro/matrixElementsSum.java: -------------------------------------------------------------------------------- 1 | // After becoming famous, 2 | // the CodeBots decided to move into a new building together. 3 | // Each of the rooms has a different cost, and some of them are free, 4 | // but there's a rumour that all the free rooms are haunted! Since the CodeBots are quite superstitious, 5 | // they refuse to stay in any of the free rooms, 6 | // or any of the rooms below any of the free rooms. 7 | 8 | // Given matrix, 9 | // a rectangular matrix of integers, 10 | // where each value represents the cost of the room, 11 | // 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). 12 | 13 | // Example 14 | 15 | // For 16 | // matrix = [[0, 1, 1, 2], 17 | // [0, 5, 0, 0], 18 | // [2, 0, 3, 3]] 19 | // the output should be 20 | // matrixElementsSum(matrix) = 9. 21 | 22 | // There are several haunted rooms, 23 | // so we'll disregard them as well as any rooms beneath them. 24 | // Thus, 25 | // the answer is 1 + 5 + 1 + 2 = 9. 26 | 27 | // For 28 | // matrix = [[1, 1, 1, 0], 29 | // [0, 5, 0, 1], 30 | // [2, 1, 3, 10]] 31 | // the output should be 32 | // matrixElementsSum(matrix) = 9. 33 | 34 | // Note that the free room in the final column makes the full column unsuitable for bots (not just the room directly beneath it). 35 | // Thus, 36 | // the answer is 1 + 1 + 1 + 5 + 1 = 9. 37 | 38 | // Input/Output 39 | 40 | // [execution time limit] 3 seconds (java) 41 | 42 | // [input] array.array.integer matrix 43 | 44 | // A 2-dimensional array of integers representing the cost of each room in the building. 45 | // A value of 0 indicates that the room is haunted. 46 | 47 | // Guaranteed constraints: 48 | // 1 ≤ matrix.length ≤ 5, 49 | // 1 ≤ matrix[i].length ≤ 5, 50 | // 0 ≤ matrix[i][j] ≤ 10. 51 | 52 | // [output] integer 53 | 54 | // The total price of all the rooms that are suitable for the CodeBots to live in. 55 | 56 | // Solution 57 | int matrixElementsSum(int[][] matrix) { 58 | int ans = 0; 59 | int height = matrix.length; 60 | int width = matrix[0].length; 61 | 62 | for (int j = 0; j < width; ++j) { 63 | for (int i = 0; i < height; ++i) { 64 | if (matrix[i][j] == 0) { 65 | break; 66 | } 67 | 68 | ans += matrix[i][j]; 69 | } 70 | } 71 | 72 | return ans; 73 | } 74 | 75 | // TC: O(n^2); SC: O(1) 76 | -------------------------------------------------------------------------------- /Arcade/Intro/reverseInParentheses.java: -------------------------------------------------------------------------------- 1 | // Write a function that reverses characters in (possibly nested) parentheses in the input string. 2 | 3 | // Input strings will always be well-formed with matching ()s. 4 | 5 | // Example 6 | // For inputString = "(bar)", 7 | // the output should be 8 | // solution(inputString) = "rab"; 9 | 10 | // For inputString = "foo(bar)baz", 11 | // the output should be 12 | // solution(inputString) = "foorabbaz"; 13 | 14 | // For inputString = "foo(bar)baz(blim)", 15 | // the output should be 16 | // solution(inputString) = "foorabbazmilb"; 17 | 18 | // For inputString = "foo(bar(baz))blim", 19 | // the output should be 20 | // solution(inputString) = "foobazrabblim". 21 | 22 | // Because "foo(bar(baz))blim" becomes "foo(barzab)blim" and then "foobazrabblim". 23 | // Input/Output 24 | 25 | // [execution time limit] 3 seconds (java) 26 | 27 | // [input] string inputString 28 | 29 | // A string consisting of lowercase English letters and the characters ( and ). 30 | // It is guaranteed that all parentheses in inputString form a regular bracket sequence. 31 | 32 | // Guaranteed constraints: 33 | // 0 ≤ inputString.length ≤ 50. 34 | 35 | // [output] string 36 | 37 | // Return inputString, 38 | // with all the characters that were in parentheses reversed. 39 | 40 | // Solution 41 | String solution(String inputString) { 42 | if (inputString == null || inputString.length() < 2) { 43 | return inputString; 44 | } 45 | 46 | char[] charArr = inputString.toCharArray(); 47 | Deque stack = new LinkedList<>(); 48 | StringBuilder sb = new StringBuilder(); 49 | 50 | if (charArr.length == 2 && charArr[0] == '(') { 51 | return ""; 52 | } 53 | 54 | for (int i = 0; i < inputString.length(); i++) { 55 | if (charArr[i] == '(') { 56 | stack.addFirst(i); 57 | } 58 | else if (charArr[i] == ')') { 59 | int leftParen = stack.pollFirst(); 60 | 61 | swapArray(charArr, leftParen + 1, i - 1); 62 | } 63 | } 64 | 65 | for (int i = 0; i < charArr.length; i++) { 66 | if (charArr[i] == '(' || charArr[i] == ')') { 67 | continue; 68 | } 69 | 70 | sb.append(charArr[i]); 71 | } 72 | 73 | 74 | 75 | return sb.toString(); 76 | } 77 | 78 | private void swapArray(char[] charArr, int start, int end) { 79 | while (start < end) { 80 | swapChar(charArr, start, end); 81 | start++; 82 | end--; 83 | } 84 | } 85 | 86 | private void swapChar(char[] charArr, int left, int right) { 87 | char temp = charArr[left]; 88 | charArr[left] = charArr[right]; 89 | charArr[right] = temp; 90 | } 91 | // TC: O(n^m): SC: O(n); 92 | // n is the length of the string. m is the number of parentheses. 93 | 94 | -------------------------------------------------------------------------------- /Arcade/Intro/shapeArea.java: -------------------------------------------------------------------------------- 1 | // Below we will define an n-interesting polygon. 2 | // Your task is to find the area of a polygon for a given n. 3 | 4 | // A 1-interesting polygon is just a square with a side of length 1. 5 | // An n-interesting polygon is obtained by taking the n - 1-interesting polygon and appending 1-interesting polygons to its rim, 6 | // side by side. 7 | // You can see the 1-, 2-, 3- and 4-interesting polygons in the picture below. 8 | 9 | // Example 10 | 11 | // For n = 2, the output should be 12 | // shapeArea(n) = 5; 13 | // For n = 3, the output should be 14 | // shapeArea(n) = 13. 15 | // Input/Output 16 | 17 | // [execution time limit] 3 seconds (java) 18 | 19 | // [input] integer n 20 | 21 | // Guaranteed constraints: 22 | // 1 ≤ n < 104. 23 | 24 | // [output] integer 25 | 26 | // The area of the n-interesting polygon. 27 | 28 | // Solution 29 | int shapeArea(int n) { 30 | if (n == 1) { 31 | return 1; 32 | } 33 | 34 | return shapeArea(n - 1) + 4 * (n - 1); 35 | } 36 | 37 | // TC: O(n): SC: O(n) 38 | --------------------------------------------------------------------------------