├── Lesson 01 - Iterations ├── BinaryGap │ ├── solution.c │ └── task.txt └── Iterations.pdf ├── Lesson 02 - Arrays ├── Arrays.pdf ├── CyclicRotation │ ├── solution.c │ └── task.txt └── OddOccurrencesInArray │ ├── solution.c │ └── task.txt ├── Lesson 03 - Time Complexity ├── FrogJmp │ ├── solution.py │ └── task.txt ├── PermMissingElem │ ├── solution.py │ └── task.txt ├── TapeEquilibrium │ ├── solution.py │ └── task.txt └── TimeComplexity.pdf ├── Lesson 04 - Counting Elements ├── CountingElements.pdf ├── FrogRiverOne │ ├── solution.cpp │ └── task.txt ├── MaxCounters │ ├── solution.cpp │ └── task.txt ├── MissingInteger │ ├── solution.cpp │ └── task.txt └── PermCheck │ ├── solution.cpp │ └── task.txt ├── Lesson 05 - Prefix Sums ├── CountDiv │ ├── solution.c │ └── task.txt ├── GenomicRangeQuery │ ├── solution.c │ ├── solution.cpp │ └── task.txt ├── MinAvgTwoSlice │ ├── solution.c │ └── task.txt ├── PassingCars │ ├── solution.c │ └── task.txt └── PrefixSums.pdf ├── Lesson 06 - Sorting ├── Distinct │ ├── solution.java │ └── task.txt ├── MaxProductOfThree │ ├── solution.java │ └── task.txt ├── NumberOfDiscIntersections │ ├── disc.png │ ├── solution.java │ └── task.txt ├── Sorting.pdf └── Triangle │ ├── solution.java │ └── task.txt ├── Lesson 07 - Stacks and Queues ├── Brackets │ ├── solution.cs │ └── task.txt ├── Fish │ ├── solution.cs │ └── task.txt ├── Nesting │ ├── solution.cs │ └── task.txt ├── Stacks.pdf └── StoneWall │ ├── solution.cs │ ├── stonewall.png │ └── task.txt ├── Lesson 08 - Leader ├── Dominator │ ├── solution.php │ └── task.txt ├── EquiLeader │ ├── solution.php │ └── task.txt └── Leader.pdf ├── Lesson 09 - Maximum slice problem ├── MaxDoubleSliceSum │ ├── solution.js │ └── task.txt ├── MaxProfit │ ├── solution.js │ └── task.txt ├── MaxSlice.pdf └── MaxSliceSum │ ├── solution.js │ └── task.txt ├── Lesson 10 - Prime and composite numbers ├── CountFactors │ ├── solution.rb │ └── task.txt ├── Flags │ ├── flags.png │ ├── solution.rb │ └── task.txt ├── MinPerimeterRectangle │ ├── solution.rb │ └── task.txt ├── Peaks │ ├── solution.rb │ └── task.txt └── PrimeNumbers.pdf ├── Lesson 11 - Sieve of Eratosthenes ├── CountNonDivisible │ ├── solution.pl │ └── task.txt ├── CountSemiprimes │ ├── solution.pl │ └── task.txt └── Sieve.pdf ├── Lesson 12 - Euclidean algorithm ├── ChocolatesByNumbers │ ├── solution.py │ └── task.txt ├── CommonPrimeDivisors │ ├── solution.py │ └── task.txt └── Gcd.pdf ├── Lesson 13 - Fibonacci numbers ├── FibFrog │ ├── solution.cpp │ └── task.txt ├── Fibonacci.pdf └── Ladder │ ├── solution.cpp │ └── task.txt ├── Lesson 14 - Binary search algorithm ├── BinarySearch.pdf ├── MinMaxDivision │ ├── solution.c │ └── task.txt └── NailingPlanks │ ├── solution.c │ └── task.txt ├── Lesson 15 - Caterpillar method ├── AbsDistinct │ ├── solution.py │ └── task.txt ├── CaterpillarMethod.pdf ├── CountDistinctSlices │ ├── solution.py │ └── task.txt ├── CountTriangles │ ├── solution.py │ └── task.txt └── MinAbsSumOfTwo │ ├── solution.py │ └── task.txt ├── Lesson 16 - Greedy algorithms ├── GreedyAlgorithms.pdf └── TieRopes │ ├── solution.py │ └── task.txt └── README.md /Lesson 01 - Iterations/BinaryGap/solution.c: -------------------------------------------------------------------------------- 1 | // Code written in C 2 | // Correctness: 100 % 3 | // Performance: Not assessed 4 | // Time Complexity: O(log(N)) 5 | // Space Complexity: O(1) 6 | 7 | int solution(int N) { 8 | int longestBinaryGap = 0; 9 | int currentBinaryGap = -1; 10 | 11 | int val = N; 12 | 13 | while (val != 0) { 14 | if ((val & 1) == 1) { 15 | if (longestBinaryGap < currentBinaryGap) { 16 | longestBinaryGap = currentBinaryGap; 17 | } 18 | 19 | currentBinaryGap = 0; 20 | } else if (currentBinaryGap != -1) { 21 | ++currentBinaryGap; 22 | } 23 | 24 | val = val >> 1; 25 | } 26 | 27 | return longestBinaryGap; 28 | } 29 | -------------------------------------------------------------------------------- /Lesson 01 - Iterations/BinaryGap/task.txt: -------------------------------------------------------------------------------- 1 | A binary gap within a positive integer N is any maximal sequence of consecutive zeros that is surrounded by ones at both ends in the binary representation of N. 2 | 3 | For example, number 9 has binary representation 1001 and contains a binary gap of length 2. The number 529 has binary representation 1000010001 and contains two binary gaps: one of length 4 and one of length 3. The number 20 has binary representation 10100 and contains one binary gap of length 1. The number 15 has binary representation 1111 and has no binary gaps. 4 | 5 | Write a function: 6 | 7 | int solution(int N); 8 | 9 | that, given a positive integer N, returns the length of its longest binary gap. The function should return 0 if N doesn't contain a binary gap. 10 | 11 | For example, given N = 1041 the function should return 5, because N has binary representation 10000010001 and so its longest binary gap is of length 5. 12 | 13 | Assume that: 14 | 15 | N is an integer within the range [1..2,147,483,647]. 16 | Complexity: 17 | 18 | expected worst-case time complexity is O(log(N)); 19 | expected worst-case space complexity is O(1). 20 | -------------------------------------------------------------------------------- /Lesson 01 - Iterations/Iterations.pdf: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/ghananigans/codility-lesson-solutions/e7b7115f1a1229733cf67236a24c86ad7bc4c0ea/Lesson 01 - Iterations/Iterations.pdf -------------------------------------------------------------------------------- /Lesson 02 - Arrays/Arrays.pdf: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/ghananigans/codility-lesson-solutions/e7b7115f1a1229733cf67236a24c86ad7bc4c0ea/Lesson 02 - Arrays/Arrays.pdf -------------------------------------------------------------------------------- /Lesson 02 - Arrays/CyclicRotation/solution.c: -------------------------------------------------------------------------------- 1 | // Code written in C 2 | // Correctness: 100 % 3 | // Performance: Not assessed 4 | // Time Complexity: O(N) 5 | // Space Complexity: O(N) 6 | 7 | struct Results solution(int A[], int N, int K) { 8 | struct Results result; 9 | result.N = N; 10 | 11 | if (N == 0) { 12 | result.A = A; 13 | } else { 14 | K = K % N; 15 | 16 | int *retA = (int *)malloc(N * sizeof(int)); 17 | 18 | for (int i = 0; i < N; ++i) { 19 | int a = (i + K) % N; 20 | retA[a] = A[i]; 21 | } 22 | 23 | result.A = retA; 24 | } 25 | 26 | return result; 27 | } 28 | -------------------------------------------------------------------------------- /Lesson 02 - Arrays/CyclicRotation/task.txt: -------------------------------------------------------------------------------- 1 | A zero-indexed array A consisting of N integers is given. Rotation of the array means that each element is shifted right by one index, and the last element of the array is also moved to the first place. 2 | 3 | For example, the rotation of array A = [3, 8, 9, 7, 6] is [6, 3, 8, 9, 7]. The goal is to rotate array A K times; that is, each element of A will be shifted to the right by K indexes. 4 | 5 | Write a function: 6 | 7 | struct Results solution(int A[], int N, int K); 8 | 9 | that, given a zero-indexed array A consisting of N integers and an integer K, returns the array A rotated K times. 10 | 11 | For example, given array A = [3, 8, 9, 7, 6] and K = 3, the function should return [9, 7, 6, 3, 8]. 12 | 13 | Assume that: 14 | 15 | N and K are integers within the range [0..100]; 16 | each element of array A is an integer within the range [−1,000..1,000]. 17 | In your solution, focus on correctness. The performance of your solution will not be the focus of the assessment. 18 | -------------------------------------------------------------------------------- /Lesson 02 - Arrays/OddOccurrencesInArray/solution.c: -------------------------------------------------------------------------------- 1 | // Code written in C 2 | // Correctness: 100 % 3 | // Performance: 100 % 4 | // Time Complexity: O(N) 5 | // Space Complexity: O(1) 6 | 7 | int solution(int A[], int N) { 8 | int ret = 0; 9 | 10 | for (int i = 0; i < N; ++i) { 11 | ret = ret ^ A[i]; 12 | } 13 | 14 | return ret; 15 | } 16 | -------------------------------------------------------------------------------- /Lesson 02 - Arrays/OddOccurrencesInArray/task.txt: -------------------------------------------------------------------------------- 1 | A non-empty zero-indexed array A consisting of N integers is given. The array contains an odd number of elements, and each element of the array can be paired with another element that has the same value, except for one element that is left unpaired. 2 | 3 | For example, in array A such that: 4 | 5 | A[0] = 9 A[1] = 3 A[2] = 9 6 | A[3] = 3 A[4] = 9 A[5] = 7 7 | A[6] = 9 8 | the elements at indexes 0 and 2 have value 9, 9 | the elements at indexes 1 and 3 have value 3, 10 | the elements at indexes 4 and 6 have value 9, 11 | the element at index 5 has value 7 and is unpaired. 12 | Write a function: 13 | 14 | int solution(int A[], int N); 15 | 16 | that, given an array A consisting of N integers fulfilling the above conditions, returns the value of the unpaired element. 17 | 18 | For example, given array A such that: 19 | 20 | A[0] = 9 A[1] = 3 A[2] = 9 21 | A[3] = 3 A[4] = 9 A[5] = 7 22 | A[6] = 9 23 | the function should return 7, as explained in the example above. 24 | 25 | Assume that: 26 | 27 | N is an odd integer within the range [1..1,000,000]; 28 | each element of array A is an integer within the range [1..1,000,000,000]; 29 | all but one of the values in A occur an even number of times. 30 | Complexity: 31 | 32 | expected worst-case time complexity is O(N); 33 | expected worst-case space complexity is O(1), beyond input storage (not counting the storage required for input arguments). 34 | Elements of input arrays can be modified. 35 | -------------------------------------------------------------------------------- /Lesson 03 - Time Complexity/FrogJmp/solution.py: -------------------------------------------------------------------------------- 1 | #Code written in Python 2 | #Correctness: 100 % 3 | #Performance: 100 % 4 | #Time Complexity: O(1) 5 | #Space Complexity: O(1) 6 | 7 | import math 8 | 9 | def solution(X, Y, D): 10 | jumps = int(math.ceil( float(Y-X)/D )) 11 | return jumps 12 | -------------------------------------------------------------------------------- /Lesson 03 - Time Complexity/FrogJmp/task.txt: -------------------------------------------------------------------------------- 1 | A small frog wants to get to the other side of the road. The frog is currently located at position X and wants to get to a position greater than or equal to Y. The small frog always jumps a fixed distance, D. 2 | 3 | Count the minimal number of jumps that the small frog must perform to reach its target. 4 | 5 | Write a function: 6 | 7 | def solution(X, Y, D) 8 | 9 | that, given three integers X, Y and D, returns the minimal number of jumps from position X to a position equal to or greater than Y. 10 | 11 | For example, given: 12 | 13 | X = 10 14 | Y = 85 15 | D = 30 16 | the function should return 3, because the frog will be positioned as follows: 17 | 18 | after the first jump, at position 10 + 30 = 40 19 | after the second jump, at position 10 + 30 + 30 = 70 20 | after the third jump, at position 10 + 30 + 30 + 30 = 100 21 | Assume that: 22 | 23 | X, Y and D are integers within the range [1..1,000,000,000]; 24 | X ≤ Y. 25 | Complexity: 26 | 27 | expected worst-case time complexity is O(1); 28 | expected worst-case space complexity is O(1). 29 | -------------------------------------------------------------------------------- /Lesson 03 - Time Complexity/PermMissingElem/solution.py: -------------------------------------------------------------------------------- 1 | #Code written in Python 2 | #Correctness: 100 % 3 | #Performance: 100 % 4 | #Time Complexity: O(N) 5 | #Space Complexity: O(1) 6 | 7 | def solution(A): 8 | N = len(A) + 1 9 | missing = ((N + 1) * N) / 2 10 | 11 | for x in A: 12 | missing -= x 13 | 14 | return missing 15 | -------------------------------------------------------------------------------- /Lesson 03 - Time Complexity/PermMissingElem/task.txt: -------------------------------------------------------------------------------- 1 | A zero-indexed array A consisting of N different integers is given. The array contains integers in the range [1..(N + 1)], which means that exactly one element is missing. 2 | 3 | Your goal is to find that missing element. 4 | 5 | Write a function: 6 | 7 | def solution(A) 8 | 9 | that, given a zero-indexed array A, returns the value of the missing element. 10 | 11 | For example, given array A such that: 12 | 13 | A[0] = 2 14 | A[1] = 3 15 | A[2] = 1 16 | A[3] = 5 17 | the function should return 4, as it is the missing element. 18 | 19 | Assume that: 20 | 21 | N is an integer within the range [0..100,000]; 22 | the elements of A are all distinct; 23 | each element of array A is an integer within the range [1..(N + 1)]. 24 | Complexity: 25 | 26 | expected worst-case time complexity is O(N); 27 | expected worst-case space complexity is O(1), beyond input storage (not counting the storage required for input arguments). 28 | Elements of input arrays can be modified. 29 | -------------------------------------------------------------------------------- /Lesson 03 - Time Complexity/TapeEquilibrium/solution.py: -------------------------------------------------------------------------------- 1 | #Code written in Python 2 | #Correctness: 100 % 3 | #Performance: 100 % 4 | #Time Complexity: O(N) 5 | #Space Complexity: O(1) 6 | 7 | def solution(A): 8 | sumLeft = A[0] 9 | sumRight = sum(A[1:]) 10 | difference = abs(sumLeft - sumRight) 11 | 12 | for i in range (1, len(A) - 1): 13 | sumLeft += A[i] 14 | sumRight -= A[i] 15 | tempDifference = abs(sumLeft - sumRight) 16 | 17 | if (tempDifference < difference): 18 | difference = tempDifference 19 | 20 | i = i + 1 21 | 22 | return difference 23 | -------------------------------------------------------------------------------- /Lesson 03 - Time Complexity/TapeEquilibrium/task.txt: -------------------------------------------------------------------------------- 1 | A non-empty zero-indexed array A consisting of N integers is given. Array A represents numbers on a tape. 2 | 3 | Any integer P, such that 0 < P < N, splits this tape into two non-empty parts: A[0], A[1], ..., A[P − 1] and A[P], A[P + 1], ..., A[N − 1]. 4 | 5 | The difference between the two parts is the value of: |(A[0] + A[1] + ... + A[P − 1]) − (A[P] + A[P + 1] + ... + A[N − 1])| 6 | 7 | In other words, it is the absolute difference between the sum of the first part and the sum of the second part. 8 | 9 | For example, consider array A such that: 10 | 11 | A[0] = 3 12 | A[1] = 1 13 | A[2] = 2 14 | A[3] = 4 15 | A[4] = 3 16 | We can split this tape in four places: 17 | 18 | P = 1, difference = |3 − 10| = 7 19 | P = 2, difference = |4 − 9| = 5 20 | P = 3, difference = |6 − 7| = 1 21 | P = 4, difference = |10 − 3| = 7 22 | Write a function: 23 | 24 | def solution(A) 25 | 26 | that, given a non-empty zero-indexed array A of N integers, returns the minimal difference that can be achieved. 27 | 28 | For example, given: 29 | 30 | A[0] = 3 31 | A[1] = 1 32 | A[2] = 2 33 | A[3] = 4 34 | A[4] = 3 35 | the function should return 1, as explained above. 36 | 37 | Assume that: 38 | 39 | N is an integer within the range [2..100,000]; 40 | each element of array A is an integer within the range [−1,000..1,000]. 41 | Complexity: 42 | 43 | expected worst-case time complexity is O(N); 44 | expected worst-case space complexity is O(N), beyond input storage (not counting the storage required for input arguments). 45 | Elements of input arrays can be modified. 46 | -------------------------------------------------------------------------------- /Lesson 03 - Time Complexity/TimeComplexity.pdf: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/ghananigans/codility-lesson-solutions/e7b7115f1a1229733cf67236a24c86ad7bc4c0ea/Lesson 03 - Time Complexity/TimeComplexity.pdf -------------------------------------------------------------------------------- /Lesson 04 - Counting Elements/CountingElements.pdf: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/ghananigans/codility-lesson-solutions/e7b7115f1a1229733cf67236a24c86ad7bc4c0ea/Lesson 04 - Counting Elements/CountingElements.pdf -------------------------------------------------------------------------------- /Lesson 04 - Counting Elements/FrogRiverOne/solution.cpp: -------------------------------------------------------------------------------- 1 | // Code written in C++ 2 | // Correctness: 100 % 3 | // Performance: 100 % 4 | // Time Complexity: O(N) 5 | // Space Complexity: O(X) 6 | 7 | 8 | int solution(int X, vector &A) { 9 | bool landed[X]; 10 | 11 | for (int i = 0; i < X; i++) { 12 | landed[i] = false; 13 | } 14 | 15 | for(int i = 0; i < A.size(); i++) { 16 | if (!landed[A[i] - 1]) { 17 | landed[A[i] - 1] = true; 18 | 19 | if (--X == 0) { 20 | return i; 21 | } 22 | } 23 | } 24 | 25 | return -1; 26 | } 27 | -------------------------------------------------------------------------------- /Lesson 04 - Counting Elements/FrogRiverOne/task.txt: -------------------------------------------------------------------------------- 1 | A small frog wants to get to the other side of a river. The frog is currently located at position 0, and wants to get to position X. Leaves fall from a tree onto the surface of the river. 2 | 3 | You are given a non-empty zero-indexed array A consisting of N integers representing the falling leaves. A[K] represents the position where one leaf falls at time K, measured in seconds. 4 | 5 | The goal is to find the earliest time when the frog can jump to the other side of the river. The frog can cross only when leaves appear at every position across the river from 1 to X. You may assume that the speed of the current in the river is negligibly small, i.e. the leaves do not change their positions once they fall in the river. 6 | 7 | For example, you are given integer X = 5 and array A such that: 8 | 9 | A[0] = 1 10 | A[1] = 3 11 | A[2] = 1 12 | A[3] = 4 13 | A[4] = 2 14 | A[5] = 3 15 | A[6] = 5 16 | A[7] = 4 17 | 18 | In second 6, a leaf falls into position 5. This is the earliest time when leaves appear in every position across the river. 19 | 20 | Write a function: 21 | 22 | int solution(int X, vector &A); 23 | 24 | that, given a non-empty zero-indexed array A consisting of N integers and integer X, returns the earliest time when the frog can jump to the other side of the river. 25 | 26 | If the frog is never able to jump to the other side of the river, the function should return −1. 27 | 28 | For example, given X = 5 and array A such that: 29 | 30 | A[0] = 1 31 | A[1] = 3 32 | A[2] = 1 33 | A[3] = 4 34 | A[4] = 2 35 | A[5] = 3 36 | A[6] = 5 37 | A[7] = 4 38 | 39 | the function should return 6, as explained above. 40 | 41 | Assume that: 42 | 43 | N and X are integers within the range [1..100,000]; 44 | each element of array A is an integer within the range [1..X]. 45 | 46 | Complexity: 47 | 48 | expected worst-case time complexity is O(N); 49 | expected worst-case space complexity is O(X), beyond input storage (not counting the storage required for input arguments). 50 | 51 | Elements of input arrays can be modified. 52 | -------------------------------------------------------------------------------- /Lesson 04 - Counting Elements/MaxCounters/solution.cpp: -------------------------------------------------------------------------------- 1 | // Code written in C++ 2 | // Correctness: 100 % 3 | // Performance: 100 % 4 | // Time Complexity: O(N + M) 5 | // Space Complexity: O(N) 6 | 7 | vector solution(int N, vector &A) { 8 | std::vector *counter = new std::vector(N); 9 | 10 | int max = 0; 11 | int setMax = 0; 12 | int M = A.size(); 13 | 14 | for (int i = 0; i < M; i++) { 15 | if ((*counter)[A[i] - 1] < setMax) { 16 | (*counter)[A[i] - 1] = setMax; 17 | } 18 | 19 | if ((A[i] <= N) && (A[i] >= 0)) { 20 | if (++((*counter)[A[i] - 1]) > max) { 21 | max = (*counter)[A[i] - 1]; 22 | } 23 | } else if (A[i] == (N + 1)) { 24 | setMax = max; 25 | } 26 | } 27 | 28 | for (int i = 0; i < N; i++) { 29 | if ((*counter)[i] < setMax) { 30 | (*counter)[i] = setMax; 31 | } 32 | } 33 | 34 | return *counter; 35 | } 36 | -------------------------------------------------------------------------------- /Lesson 04 - Counting Elements/MaxCounters/task.txt: -------------------------------------------------------------------------------- 1 | You are given N counters, initially set to 0, and you have two possible operations on them: 2 | 3 | increase(X) − counter X is increased by 1, 4 | max counter − all counters are set to the maximum value of any counter. 5 | 6 | A non-empty zero-indexed array A of M integers is given. This array represents consecutive operations: 7 | 8 | if A[K] = X, such that 1 ≤ X ≤ N, then operation K is increase(X), 9 | if A[K] = N + 1 then operation K is max counter. 10 | 11 | For example, given integer N = 5 and array A such that: 12 | 13 | A[0] = 3 14 | A[1] = 4 15 | A[2] = 4 16 | A[3] = 6 17 | A[4] = 1 18 | A[5] = 4 19 | A[6] = 4 20 | 21 | the values of the counters after each consecutive operation will be: 22 | 23 | (0, 0, 1, 0, 0) 24 | (0, 0, 1, 1, 0) 25 | (0, 0, 1, 2, 0) 26 | (2, 2, 2, 2, 2) 27 | (3, 2, 2, 2, 2) 28 | (3, 2, 2, 3, 2) 29 | (3, 2, 2, 4, 2) 30 | 31 | The goal is to calculate the value of every counter after all operations. 32 | 33 | Write a function: 34 | 35 | vector solution(int N, vector &A); 36 | 37 | that, given an integer N and a non-empty zero-indexed array A consisting of M integers, returns a sequence of integers representing the values of the counters. 38 | 39 | The sequence should be returned as: 40 | 41 | a structure Results (in C), or 42 | a vector of integers (in C++), or 43 | a record Results (in Pascal), or 44 | an array of integers (in any other programming language). 45 | 46 | For example, given: 47 | 48 | A[0] = 3 49 | A[1] = 4 50 | A[2] = 4 51 | A[3] = 6 52 | A[4] = 1 53 | A[5] = 4 54 | A[6] = 4 55 | 56 | the function should return [3, 2, 2, 4, 2], as explained above. 57 | 58 | Assume that: 59 | 60 | N and M are integers within the range [1..100,000]; 61 | each element of array A is an integer within the range [1..N + 1]. 62 | 63 | Complexity: 64 | 65 | expected worst-case time complexity is O(N+M); 66 | expected worst-case space complexity is O(N), beyond input storage (not counting the storage required for input arguments). 67 | 68 | Elements of input arrays can be modified. 69 | -------------------------------------------------------------------------------- /Lesson 04 - Counting Elements/MissingInteger/solution.cpp: -------------------------------------------------------------------------------- 1 | // Code written in C++ 2 | // Correctness: 100 % 3 | // Performance: 100 % 4 | // Time Complexity: O(N) 5 | // Space Complexity: O(1) 6 | 7 | #include 8 | 9 | int solution(vector &A) { 10 | std::sort(A.begin(), A.end()); 11 | 12 | int N = A.size(); 13 | 14 | if (A[N - 1] <= 0) { 15 | return 1; 16 | } 17 | 18 | if (A[0] > 1) { 19 | return 1; 20 | } 21 | 22 | for (int i = 0; i < (N - 1); i++) { 23 | if (A[i] <= 0) { 24 | if (A[i + 1] > 1) { 25 | return 1; 26 | } 27 | 28 | continue; 29 | } 30 | 31 | if ((A[i + 1] - A[i]) > 1) { 32 | return (A[i] + 1); 33 | } 34 | } 35 | 36 | return A[N-1] + 1; 37 | } 38 | -------------------------------------------------------------------------------- /Lesson 04 - Counting Elements/MissingInteger/task.txt: -------------------------------------------------------------------------------- 1 | Write a function: 2 | 3 | int solution(vector &A); 4 | 5 | that, given a non-empty zero-indexed array A of N integers, returns the minimal positive integer (greater than 0) that does not occur in A. 6 | 7 | For example, given: 8 | 9 | A[0] = 1 10 | A[1] = 3 11 | A[2] = 6 12 | A[3] = 4 13 | A[4] = 1 14 | A[5] = 2 15 | 16 | the function should return 5. 17 | 18 | Assume that: 19 | 20 | N is an integer within the range [1..100,000]; 21 | each element of array A is an integer within the range [−2,147,483,648..2,147,483,647]. 22 | 23 | Complexity: 24 | 25 | expected worst-case time complexity is O(N); 26 | expected worst-case space complexity is O(N), beyond input storage (not counting the storage required for input arguments). 27 | 28 | Elements of input arrays can be modified. 29 | -------------------------------------------------------------------------------- /Lesson 04 - Counting Elements/PermCheck/solution.cpp: -------------------------------------------------------------------------------- 1 | // Code written in C++ 2 | // Correctness: 100 % 3 | // Performance: 100 % 4 | // Time Complexity: O(N) 5 | // Space Complexity: O(N) 6 | 7 | int solution(vector &A) { 8 | unsigned int N = A.size(); 9 | 10 | bool checked[N]; 11 | 12 | for (int i = 0; i < N; i++) { 13 | checked[i] = false; 14 | } 15 | 16 | for (std::vector::iterator it = A.begin(); it != A.end(); it++) { 17 | if (*it > N) { return 0; } 18 | else if (checked[*it - 1]) { return 0; } 19 | else { checked[*it - 1] = true; } 20 | } 21 | 22 | return 1; 23 | } 24 | -------------------------------------------------------------------------------- /Lesson 04 - Counting Elements/PermCheck/task.txt: -------------------------------------------------------------------------------- 1 | A non-empty zero-indexed array A consisting of N integers is given. 2 | 3 | A permutation is a sequence containing each element from 1 to N once, and only once. 4 | 5 | For example, array A such that: 6 | 7 | A[0] = 4 8 | A[1] = 1 9 | A[2] = 3 10 | A[3] = 2 11 | 12 | is a permutation, but array A such that: 13 | 14 | A[0] = 4 15 | A[1] = 1 16 | A[2] = 3 17 | 18 | is not a permutation, because value 2 is missing. 19 | 20 | The goal is to check whether array A is a permutation. 21 | 22 | Write a function: 23 | 24 | int solution(vector &A); 25 | 26 | that, given a zero-indexed array A, returns 1 if array A is a permutation and 0 if it is not. 27 | 28 | For example, given array A such that: 29 | 30 | A[0] = 4 31 | A[1] = 1 32 | A[2] = 3 33 | A[3] = 2 34 | 35 | the function should return 1. 36 | 37 | Given array A such that: 38 | 39 | A[0] = 4 40 | A[1] = 1 41 | A[2] = 3 42 | 43 | the function should return 0. 44 | 45 | Assume that: 46 | 47 | N is an integer within the range [1..100,000]; 48 | each element of array A is an integer within the range [1..1,000,000,000]. 49 | 50 | Complexity: 51 | 52 | expected worst-case time complexity is O(N); 53 | expected worst-case space complexity is O(N), beyond input storage (not counting the storage required for input arguments). 54 | 55 | Elements of input arrays can be modified. 56 | -------------------------------------------------------------------------------- /Lesson 05 - Prefix Sums/CountDiv/solution.c: -------------------------------------------------------------------------------- 1 | // Code written in C 2 | // Correctness: 100 % 3 | // Performance: 100 % 4 | // Time Complexity: O(1) 5 | // Space Complexity: O(1) 6 | 7 | int solution(int A, int B, int K) { 8 | int remainderA = A % K; 9 | 10 | if (remainderA) { 11 | A = A + (K - remainderA); 12 | } 13 | 14 | if (A > B) { 15 | return 0; 16 | } 17 | 18 | return ((B - A) / K) + 1; 19 | } 20 | -------------------------------------------------------------------------------- /Lesson 05 - Prefix Sums/CountDiv/task.txt: -------------------------------------------------------------------------------- 1 | Write a function: 2 | 3 | int solution(int A, int B, int K); 4 | 5 | that, given three integers A, B and K, returns the number of integers within the range [A..B] that are divisible by K, i.e.: 6 | 7 | { i : A ≤ i ≤ B, i mod K = 0 } 8 | 9 | For example, for A = 6, B = 11 and K = 2, your function should return 3, because there are three numbers divisible by 2 within the range [6..11], namely 6, 8 and 10. 10 | 11 | Assume that: 12 | 13 | A and B are integers within the range [0..2,000,000,000]; 14 | K is an integer within the range [1..2,000,000,000]; 15 | A ≤ B. 16 | 17 | Complexity: 18 | 19 | expected worst-case time complexity is O(1); 20 | expected worst-case space complexity is O(1). 21 | -------------------------------------------------------------------------------- /Lesson 05 - Prefix Sums/GenomicRangeQuery/solution.c: -------------------------------------------------------------------------------- 1 | // Code written in C 2 | // I used this task as an opportunity to play around with pointers a bit, explaining the confusing math with the pointers. 3 | // There is C++ equivalent of this code available in solution.cpp 4 | // Correctness: 100 % 5 | // Performance: 100 % 6 | // Time Complexity: O(N + M) 7 | // Space Complexity: O(N) 8 | 9 | struct Results solution(char *S, int P[], int Q[], int M) { 10 | const int NUCLEOTIDES_TO_USE = 3; 11 | const int N = strlen(S); 12 | 13 | int **dna = (int **)malloc(NUCLEOTIDES_TO_USE * sizeof(int *)); 14 | 15 | for (int j = 0; j < NUCLEOTIDES_TO_USE; j++) { 16 | *(dna + j) = (int *)malloc((N + 1) * sizeof(int)); // dna[j] 17 | *(*(dna + j) + 0) = 0; // dna[j][0] 18 | } 19 | 20 | for (int i = 1; i <= N; i++) { 21 | for (int j = 0; j < NUCLEOTIDES_TO_USE; j++) { 22 | *(*(dna + j) + i) = *(*(dna + j) + i - 1); // dna[j][i] = dna[j][i - 1] 23 | } 24 | 25 | switch (S[i - 1]) { 26 | case 'A': 27 | (*(*(dna + 0) + i))++; // dna[0][i]++ 28 | break; 29 | case 'C': 30 | (*(*(dna + 1) + i))++; // dna[1][i]++ 31 | break; 32 | case 'G': 33 | (*(*(dna + 2) + i))++; // dna[2][i]++ 34 | break; 35 | default: 36 | break; 37 | } 38 | } 39 | 40 | int *resultA = malloc(M * sizeof(int)); 41 | 42 | for (int i = 0; i < M; i++) { 43 | *(resultA + i) = 4; // resultA[i] = 4 44 | 45 | for (int j = 0; j < NUCLEOTIDES_TO_USE; j++) { 46 | if ((*(*(dna + j) + *(Q + i) + 1) - *(*(dna + j) + *(P + i))) > 0) { // dna[j][Q[i] + 1] - dna[j][P[i]] 47 | *(resultA + i) = j + 1; 48 | break; 49 | } 50 | } 51 | } 52 | 53 | for (int j = 0; j < NUCLEOTIDES_TO_USE; j++) { 54 | free(dna[j]); 55 | } 56 | free(dna); 57 | 58 | struct Results result; 59 | result.A = resultA; 60 | result.M = M; 61 | 62 | return result; 63 | } 64 | 65 | -------------------------------------------------------------------------------- /Lesson 05 - Prefix Sums/GenomicRangeQuery/solution.cpp: -------------------------------------------------------------------------------- 1 | // Code written in C++ 2 | // Correctness: 100 % 3 | // Performance: 100 % 4 | // Time Complexity: O(N + M) 5 | // Space Complexity: O(N) 6 | 7 | vector solution(string &S, vector &P, vector &Q) { 8 | const int NUCLEOTIDES_TO_USE = 3; 9 | const int N = S.length(); 10 | const int M = P.size(); 11 | 12 | int **dna = new int*[NUCLEOTIDES_TO_USE]; 13 | 14 | for (int i = 0; i < NUCLEOTIDES_TO_USE; i++) { 15 | dna[i] = new int[N + 1]; 16 | dna[i][0] = 0; 17 | } 18 | 19 | for (int i = 1; i <= N; i++) { 20 | for (int j = 0; j < NUCLEOTIDES_TO_USE; j++) { 21 | dna[j][i] = dna[j][i - 1]; 22 | } 23 | 24 | switch (S[i - 1]) { 25 | case 'A': 26 | dna[0][i]++; 27 | break; 28 | case 'C': 29 | dna[1][i]++; 30 | break; 31 | case 'G': 32 | dna[2][i]++; 33 | break; 34 | default: 35 | break; 36 | } 37 | } 38 | 39 | std::vector *results = new std::vector(M); 40 | 41 | for (int i = 0; i < M; i++) { 42 | (*results)[i] = 4; 43 | 44 | for (int j = 0; j < NUCLEOTIDES_TO_USE; j++) { 45 | if ((dna[j][Q[i] + 1] - dna[j][P[i]]) > 0) { 46 | (*results)[i] = j + 1; 47 | break; 48 | } 49 | } 50 | } 51 | 52 | for (int j = 0; j < NUCLEOTIDES_TO_USE; j++) { 53 | delete[] dna[j]; 54 | } 55 | delete[] dna; 56 | 57 | return *results; 58 | } 59 | -------------------------------------------------------------------------------- /Lesson 05 - Prefix Sums/GenomicRangeQuery/task.txt: -------------------------------------------------------------------------------- 1 | A DNA sequence can be represented as a string consisting of the letters A, C, G and T, which correspond to the types of successive nucleotides in the sequence. Each nucleotide has an impact factor, which is an integer. Nucleotides of types A, C, G and T have impact factors of 1, 2, 3 and 4, respectively. You are going to answer several queries of the form: What is the minimal impact factor of nucleotides contained in a particular part of the given DNA sequence? 2 | 3 | The DNA sequence is given as a non-empty string S = S[0]S[1]...S[N-1] consisting of N characters. There are M queries, which are given in non-empty arrays P and Q, each consisting of M integers. The K-th query (0 ≤ K < M) requires you to find the minimal impact factor of nucleotides contained in the DNA sequence between positions P[K] and Q[K] (inclusive). 4 | 5 | For example, consider string S = CAGCCTA and arrays P, Q such that: 6 | 7 | P[0] = 2 Q[0] = 4 8 | P[1] = 5 Q[1] = 5 9 | P[2] = 0 Q[2] = 6 10 | 11 | The answers to these M = 3 queries are as follows: 12 | 13 | The part of the DNA between positions 2 and 4 contains nucleotides G and C (twice), whose impact factors are 3 and 2 respectively, so the answer is 2. 14 | The part between positions 5 and 5 contains a single nucleotide T, whose impact factor is 4, so the answer is 4. 15 | The part between positions 0 and 6 (the whole string) contains all nucleotides, in particular nucleotide A whose impact factor is 1, so the answer is 1. 16 | 17 | Assume that the following declarations are given: 18 | 19 | struct Results { 20 | int * A; 21 | int M; 22 | }; 23 | 24 | Write a function: 25 | 26 | struct Results solution(char *S, int P[], int Q[], int M); 27 | 28 | that, given a non-empty zero-indexed string S consisting of N characters and two non-empty zero-indexed arrays P and Q consisting of M integers, returns an array consisting of M integers specifying the consecutive answers to all queries. 29 | 30 | The sequence should be returned as: 31 | 32 | a Results structure (in C), or 33 | a vector of integers (in C++), or 34 | a Results record (in Pascal), or 35 | an array of integers (in any other programming language). 36 | 37 | For example, given the string S = CAGCCTA and arrays P, Q such that: 38 | 39 | P[0] = 2 Q[0] = 4 40 | P[1] = 5 Q[1] = 5 41 | P[2] = 0 Q[2] = 6 42 | 43 | the function should return the values [2, 4, 1], as explained above. 44 | 45 | Assume that: 46 | 47 | N is an integer within the range [1..100,000]; 48 | M is an integer within the range [1..50,000]; 49 | each element of arrays P, Q is an integer within the range [0..N − 1]; 50 | P[K] ≤ Q[K], where 0 ≤ K < M; 51 | string S consists only of upper-case English letters A, C, G, T. 52 | 53 | Complexity: 54 | 55 | expected worst-case time complexity is O(N+M); 56 | expected worst-case space complexity is O(N), beyond input storage (not counting the storage required for input arguments). 57 | 58 | Elements of input arrays can be modified. 59 | -------------------------------------------------------------------------------- /Lesson 05 - Prefix Sums/MinAvgTwoSlice/solution.c: -------------------------------------------------------------------------------- 1 | // Code written in C 2 | // Correctness: 100 % 3 | // Performance: 100 % 4 | // Time Complexity: O(N) 5 | // Space Complexity: O(1) 6 | 7 | double min(double a, double b) { 8 | return (a < b) ? a : b; 9 | } 10 | 11 | int solution(int A[], int N) { 12 | double minV = (A[0] + A[1]) / 2.0; 13 | int minP = 0; 14 | 15 | for (int i = 0; i < N-2; i++) { 16 | double tempMinV = min((A[i] + A[i + 1]) / 2.0, (A[i] + A[i + 1] + A[i + 2]) / 3.0); 17 | 18 | if (tempMinV < minV) { 19 | minV = tempMinV; 20 | minP = i; 21 | } 22 | } 23 | 24 | if (((A[N - 2] + A[N - 1]) / 2.0) < minV) { 25 | minP = N - 2; 26 | } 27 | 28 | return minP; 29 | } 30 | -------------------------------------------------------------------------------- /Lesson 05 - Prefix Sums/MinAvgTwoSlice/task.txt: -------------------------------------------------------------------------------- 1 | A non-empty zero-indexed array A consisting of N integers is given. A pair of integers (P, Q), such that 0 ≤ P < Q < N, is called a slice of array A (notice that the slice contains at least two elements). The average of a slice (P, Q) is the sum of A[P] + A[P + 1] + ... + A[Q] divided by the length of the slice. To be precise, the average equals (A[P] + A[P + 1] + ... + A[Q]) / (Q − P + 1). 2 | 3 | For example, array A such that: 4 | 5 | A[0] = 4 6 | A[1] = 2 7 | A[2] = 2 8 | A[3] = 5 9 | A[4] = 1 10 | A[5] = 5 11 | A[6] = 8 12 | 13 | contains the following example slices: 14 | 15 | slice (1, 2), whose average is (2 + 2) / 2 = 2; 16 | slice (3, 4), whose average is (5 + 1) / 2 = 3; 17 | slice (1, 4), whose average is (2 + 2 + 5 + 1) / 4 = 2.5. 18 | 19 | The goal is to find the starting position of a slice whose average is minimal. 20 | 21 | Write a function: 22 | 23 | int solution(int A[], int N); 24 | 25 | that, given a non-empty zero-indexed array A consisting of N integers, returns the starting position of the slice with the minimal average. If there is more than one slice with a minimal average, you should return the smallest starting position of such a slice. 26 | 27 | For example, given array A such that: 28 | 29 | A[0] = 4 30 | A[1] = 2 31 | A[2] = 2 32 | A[3] = 5 33 | A[4] = 1 34 | A[5] = 5 35 | A[6] = 8 36 | 37 | the function should return 1, as explained above. 38 | 39 | Assume that: 40 | 41 | N is an integer within the range [2..100,000]; 42 | each element of array A is an integer within the range [−10,000..10,000]. 43 | 44 | Complexity: 45 | 46 | expected worst-case time complexity is O(N); 47 | expected worst-case space complexity is O(N), beyond input storage (not counting the storage required for input arguments). 48 | 49 | Elements of input arrays can be modified. 50 | -------------------------------------------------------------------------------- /Lesson 05 - Prefix Sums/PassingCars/solution.c: -------------------------------------------------------------------------------- 1 | // Code written in C 2 | // Correctness: 100 % 3 | // Performance: 100 % 4 | // Time Complexity: O(N) 5 | // Space Complexity: O(1) 6 | 7 | int solution(int A[], int N) { 8 | unsigned int counter = 0; 9 | unsigned int multiplier = 0; 10 | 11 | for (int i = 0; i < N; i++) { 12 | if (A[i] != 0) { 13 | counter += multiplier; 14 | 15 | if (counter > 1000000000) { 16 | return -1; 17 | } 18 | } else { 19 | multiplier++; 20 | } 21 | } 22 | 23 | return counter; 24 | } 25 | -------------------------------------------------------------------------------- /Lesson 05 - Prefix Sums/PassingCars/task.txt: -------------------------------------------------------------------------------- 1 | A non-empty zero-indexed array A consisting of N integers is given. The consecutive elements of array A represent consecutive cars on a road. 2 | 3 | Array A contains only 0s and/or 1s: 4 | 5 | 0 represents a car traveling east, 6 | 1 represents a car traveling west. 7 | 8 | The goal is to count passing cars. We say that a pair of cars (P, Q), where 0 ≤ P < Q < N, is passing when P is traveling to the east and Q is traveling to the west. 9 | 10 | For example, consider array A such that: 11 | 12 | A[0] = 0 13 | A[1] = 1 14 | A[2] = 0 15 | A[3] = 1 16 | A[4] = 1 17 | 18 | We have five pairs of passing cars: (0, 1), (0, 3), (0, 4), (2, 3), (2, 4). 19 | 20 | Write a function: 21 | 22 | int solution(int A[], int N); 23 | 24 | that, given a non-empty zero-indexed array A of N integers, returns the number of pairs of passing cars. 25 | 26 | The function should return −1 if the number of pairs of passing cars exceeds 1,000,000,000. 27 | 28 | For example, given: 29 | 30 | A[0] = 0 31 | A[1] = 1 32 | A[2] = 0 33 | A[3] = 1 34 | A[4] = 1 35 | 36 | the function should return 5, as explained above. 37 | 38 | Assume that: 39 | 40 | N is an integer within the range [1..100,000]; 41 | each element of array A is an integer that can have one of the following values: 0, 1. 42 | 43 | Complexity: 44 | 45 | expected worst-case time complexity is O(N); 46 | expected worst-case space complexity is O(1), beyond input storage (not counting the storage required for input arguments). 47 | 48 | Elements of input arrays can be modified. 49 | -------------------------------------------------------------------------------- /Lesson 05 - Prefix Sums/PrefixSums.pdf: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/ghananigans/codility-lesson-solutions/e7b7115f1a1229733cf67236a24c86ad7bc4c0ea/Lesson 05 - Prefix Sums/PrefixSums.pdf -------------------------------------------------------------------------------- /Lesson 06 - Sorting/Distinct/solution.java: -------------------------------------------------------------------------------- 1 | // Code written in Java 2 | // Correctness: 100 % 3 | // Performance: 100 % 4 | // Time Complexity: O(N * log(N)) 5 | // Space Complexity: O(1) 6 | 7 | class Solution { 8 | public int solution(int[] A) { 9 | final int A_LENGTH = A.length; 10 | 11 | if (A_LENGTH == 0) { 12 | return 0; 13 | } 14 | 15 | quicksort(A, 0, A_LENGTH - 1); 16 | 17 | int counter = 1; 18 | 19 | for (int i = 1; i < A_LENGTH; i++) { 20 | if (A[i] != A[i - 1]) { 21 | counter++; 22 | } 23 | } 24 | 25 | return counter; 26 | } 27 | 28 | private void quicksort(int arr[], int left, int right) { 29 | int index = partition(arr, left, right); 30 | 31 | if (left < index - 1) { 32 | quicksort(arr, left, index - 1); 33 | } 34 | 35 | if (index < right) { 36 | quicksort(arr, index, right); 37 | } 38 | } 39 | 40 | private int partition(int arr[], int left, int right) { 41 | int pivot = arr[(left + right) / 2]; 42 | 43 | while (left <= right) { 44 | while (arr[left] < pivot) { 45 | left++; 46 | } 47 | 48 | while (arr[right] > pivot) { 49 | right--; 50 | } 51 | 52 | if (left <= right) { 53 | int tmp = arr[left]; 54 | arr[left++] = arr[right]; 55 | arr[right--] = tmp; 56 | } 57 | } 58 | 59 | return left; 60 | } 61 | } 62 | -------------------------------------------------------------------------------- /Lesson 06 - Sorting/Distinct/task.txt: -------------------------------------------------------------------------------- 1 | Write a function 2 | 3 | class Solution { public int solution(int[] A); } 4 | 5 | that, given a zero-indexed array A consisting of N integers, returns the number of distinct values in array A. 6 | 7 | Assume that: 8 | 9 | N is an integer within the range [0..100,000]; 10 | each element of array A is an integer within the range [−1,000,000..1,000,000]. 11 | 12 | For example, given array A consisting of six elements such that: 13 | 14 | A[0] = 2 A[1] = 1 A[2] = 1 15 | A[3] = 2 A[4] = 3 A[5] = 1 16 | 17 | the function should return 3, because there are 3 distinct values appearing in array A, namely 1, 2 and 3. 18 | 19 | Complexity: 20 | 21 | expected worst-case time complexity is O(N*log(N)); 22 | expected worst-case space complexity is O(N), beyond input storage (not counting the storage required for input arguments). 23 | 24 | Elements of input arrays can be modified. 25 | -------------------------------------------------------------------------------- /Lesson 06 - Sorting/MaxProductOfThree/solution.java: -------------------------------------------------------------------------------- 1 | // Code written in Java 2 | // Correctness: 100 % 3 | // Performance: 100 % 4 | // Time Complexity: O(N * log(N)) 5 | // Space Complexity: O(1) 6 | 7 | class Solution { 8 | public int solution(int[] A) { 9 | int N = A.length; 10 | quicksort(A, 0, N - 1); 11 | 12 | int tmp1 = (A[0] * A[1]); 13 | int tmp2 = (A[N - 2] * A[N - 3]); 14 | 15 | if (A[N - 1] > 0) { 16 | return A[N - 1] * ((tmp1 > tmp2) ? tmp1 : tmp2); 17 | } else { 18 | return A[N - 1] * ((tmp1 < tmp2) ? tmp1 : tmp2); 19 | } 20 | } 21 | 22 | private void quicksort(int arr[], int left, int right) { 23 | int index = partition(arr, left, right); 24 | 25 | if (left < index - 1) { 26 | quicksort(arr, left, index - 1); 27 | } 28 | 29 | if (index < right) { 30 | quicksort(arr, index, right); 31 | } 32 | } 33 | 34 | private int partition(int arr[], int left, int right) { 35 | int pivot = arr[(left + right) / 2]; 36 | 37 | while (left <= right) { 38 | while (arr[left] < pivot) { 39 | left++; 40 | } 41 | 42 | while (arr[right] > pivot) { 43 | right--; 44 | } 45 | 46 | if (left <= right) { 47 | int tmp = arr[left]; 48 | arr[left++] = arr[right]; 49 | arr[right--] = tmp; 50 | } 51 | } 52 | 53 | return left; 54 | } 55 | } 56 | -------------------------------------------------------------------------------- /Lesson 06 - Sorting/MaxProductOfThree/task.txt: -------------------------------------------------------------------------------- 1 | A non-empty zero-indexed array A consisting of N integers is given. The product of triplet (P, Q, R) equates to A[P] * A[Q] * A[R] (0 ≤ P < Q < R < N). 2 | 3 | For example, array A such that: 4 | 5 | A[0] = -3 6 | A[1] = 1 7 | A[2] = 2 8 | A[3] = -2 9 | A[4] = 5 10 | A[5] = 6 11 | 12 | contains the following example triplets: 13 | 14 | (0, 1, 2), product is −3 * 1 * 2 = −6 15 | (1, 2, 4), product is 1 * 2 * 5 = 10 16 | (2, 4, 5), product is 2 * 5 * 6 = 60 17 | 18 | Your goal is to find the maximal product of any triplet. 19 | 20 | Write a function: 21 | 22 | class Solution { public int solution(int[] A); } 23 | 24 | that, given a non-empty zero-indexed array A, returns the value of the maximal product of any triplet. 25 | 26 | For example, given array A such that: 27 | 28 | A[0] = -3 29 | A[1] = 1 30 | A[2] = 2 31 | A[3] = -2 32 | A[4] = 5 33 | A[5] = 6 34 | 35 | the function should return 60, as the product of triplet (2, 4, 5) is maximal. 36 | 37 | Assume that: 38 | 39 | N is an integer within the range [3..100,000]; 40 | each element of array A is an integer within the range [−1,000..1,000]. 41 | 42 | Complexity: 43 | 44 | expected worst-case time complexity is O(N*log(N)); 45 | expected worst-case space complexity is O(1), beyond input storage (not counting the storage required for input arguments). 46 | 47 | Elements of input arrays can be modified. 48 | 49 | -------------------------------------------------------------------------------- /Lesson 06 - Sorting/NumberOfDiscIntersections/disc.png: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/ghananigans/codility-lesson-solutions/e7b7115f1a1229733cf67236a24c86ad7bc4c0ea/Lesson 06 - Sorting/NumberOfDiscIntersections/disc.png -------------------------------------------------------------------------------- /Lesson 06 - Sorting/NumberOfDiscIntersections/solution.java: -------------------------------------------------------------------------------- 1 | // Code written in Java 2 | // Correctness: 100 % 3 | // Performance: 100 % 4 | // Time Complexity: O(N * log(N)) 5 | // Space Complexity: O(N) 6 | 7 | class Solution { 8 | public int solution(int[] A) { 9 | final int N = A.length; 10 | 11 | if (N < 2) { 12 | return 0; 13 | } 14 | 15 | long[] leftEdges = new long[N]; 16 | long[] rightEdges = new long[N]; 17 | 18 | for (int i = 0; i < N; i++) { 19 | long r = (long)A[i]; 20 | 21 | long left = i - r; 22 | leftEdges[i] = left; 23 | 24 | long right = i + r; 25 | rightEdges[i] = right; 26 | } 27 | 28 | quicksort(leftEdges, 0, N - 1); 29 | quicksort(rightEdges, 0, N - 1); 30 | 31 | int counter = 0; 32 | int previousCircles = 0; 33 | int leftP = 0; 34 | 35 | for (int rightP = 0; rightP < N; rightP++) { 36 | while ((leftP < N) && (leftEdges[leftP] <= rightEdges[rightP])) { 37 | leftP++; 38 | previousCircles++; 39 | } 40 | 41 | counter += --previousCircles; 42 | 43 | if (counter > 10000000) { 44 | return -1; 45 | } 46 | } 47 | return counter; 48 | } 49 | 50 | private void quicksort(long arr[], int left, int right) { 51 | int index = partition(arr, left, right); 52 | 53 | if (left < index - 1) { 54 | quicksort(arr, left, index - 1); 55 | } 56 | 57 | if (index < right) { 58 | quicksort(arr, index, right); 59 | } 60 | } 61 | 62 | private int partition(long arr[], int left, int right) { 63 | long pivot = arr[(left + right) / 2]; 64 | 65 | while (left <= right) { 66 | while (arr[left] < pivot) { 67 | left++; 68 | } 69 | 70 | while (arr[right] > pivot) { 71 | right--; 72 | } 73 | 74 | if (left <= right) { 75 | long tmp = arr[left]; 76 | arr[left++] = arr[right]; 77 | arr[right--] = tmp; 78 | } 79 | } 80 | 81 | return left; 82 | } 83 | } 84 | -------------------------------------------------------------------------------- /Lesson 06 - Sorting/NumberOfDiscIntersections/task.txt: -------------------------------------------------------------------------------- 1 | We draw N discs on a plane. The discs are numbered from 0 to N − 1. A zero-indexed array A of N non-negative integers, specifying the radiuses of the discs, is given. The J-th disc is drawn with its center at (J, 0) and radius A[J]. 2 | 3 | We say that the J-th disc and K-th disc intersect if J ≠ K and the J-th and K-th discs have at least one common point (assuming that the discs contain their borders). 4 | 5 | The figure below shows discs drawn for N = 6 and A as follows: 6 | 7 | A[0] = 1 8 | A[1] = 5 9 | A[2] = 2 10 | A[3] = 1 11 | A[4] = 4 12 | A[5] = 0 13 | 14 | 15 | 16 | There are eleven (unordered) pairs of discs that intersect, namely: 17 | 18 | discs 1 and 4 intersect, and both intersect with all the other discs; 19 | disc 2 also intersects with discs 0 and 3. 20 | 21 | Write a function: 22 | 23 | class Solution { public int solution(int[] A); } 24 | 25 | that, given an array A describing N discs as explained above, returns the number of (unordered) pairs of intersecting discs. The function should return −1 if the number of intersecting pairs exceeds 10,000,000. 26 | 27 | Given array A shown above, the function should return 11, as explained above. 28 | 29 | Assume that: 30 | 31 | N is an integer within the range [0..100,000]; 32 | each element of array A is an integer within the range [0..2,147,483,647]. 33 | 34 | Complexity: 35 | 36 | expected worst-case time complexity is O(N*log(N)); 37 | expected worst-case space complexity is O(N), beyond input storage (not counting the storage required for input arguments). 38 | 39 | Elements of input arrays can be modified. 40 | 41 | -------------------------------------------------------------------------------- /Lesson 06 - Sorting/Sorting.pdf: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/ghananigans/codility-lesson-solutions/e7b7115f1a1229733cf67236a24c86ad7bc4c0ea/Lesson 06 - Sorting/Sorting.pdf -------------------------------------------------------------------------------- /Lesson 06 - Sorting/Triangle/solution.java: -------------------------------------------------------------------------------- 1 | // Code written in Java 2 | // Correctness: 100 % 3 | // Performance: 100 % 4 | // Time Complexity: O(N * log(N)) 5 | // Space Complexity: O(1) 6 | 7 | class Solution { 8 | public int solution(int[] A) { 9 | final int A_LENGTH = A.length; 10 | 11 | if (A_LENGTH < 3) { 12 | return 0; 13 | } 14 | 15 | quicksort(A, 0, A_LENGTH - 1); 16 | 17 | for (int i = 2; i < A_LENGTH; i++) { 18 | if ((long)A[i] < (long)A[i - 2] + (long)A[i - 1]) { 19 | return 1; 20 | } 21 | } 22 | 23 | return 0; 24 | } 25 | 26 | private void quicksort(int arr[], int left, int right) { 27 | int index = partition(arr, left, right); 28 | 29 | if (left < index - 1) { 30 | quicksort(arr, left, index - 1); 31 | } 32 | 33 | if (index < right) { 34 | quicksort(arr, index, right); 35 | } 36 | } 37 | 38 | private int partition(int arr[], int left, int right) { 39 | int pivot = arr[(left + right) / 2]; 40 | 41 | while (left <= right) { 42 | while (arr[left] < pivot) { 43 | left++; 44 | } 45 | 46 | while (arr[right] > pivot) { 47 | right--; 48 | } 49 | 50 | if (left <= right) { 51 | int tmp = arr[left]; 52 | arr[left++] = arr[right]; 53 | arr[right--] = tmp; 54 | } 55 | } 56 | 57 | return left; 58 | } 59 | } 60 | -------------------------------------------------------------------------------- /Lesson 06 - Sorting/Triangle/task.txt: -------------------------------------------------------------------------------- 1 | A zero-indexed array A consisting of N integers is given. A triplet (P, Q, R) is triangular if 0 ≤ P < Q < R < N and: 2 | 3 | A[P] + A[Q] > A[R], 4 | A[Q] + A[R] > A[P], 5 | A[R] + A[P] > A[Q]. 6 | 7 | For example, consider array A such that: 8 | 9 | A[0] = 10 A[1] = 2 A[2] = 5 10 | A[3] = 1 A[4] = 8 A[5] = 20 11 | 12 | Triplet (0, 2, 4) is triangular. 13 | 14 | Write a function: 15 | 16 | class Solution { public int solution(int[] A); } 17 | 18 | that, given a zero-indexed array A consisting of N integers, returns 1 if there exists a triangular triplet for this array and returns 0 otherwise. 19 | 20 | For example, given array A such that: 21 | 22 | A[0] = 10 A[1] = 2 A[2] = 5 23 | A[3] = 1 A[4] = 8 A[5] = 20 24 | 25 | the function should return 1, as explained above. Given array A such that: 26 | 27 | A[0] = 10 A[1] = 50 A[2] = 5 28 | A[3] = 1 29 | 30 | the function should return 0. 31 | 32 | Assume that: 33 | 34 | N is an integer within the range [0..100,000]; 35 | each element of array A is an integer within the range [−2,147,483,648..2,147,483,647]. 36 | 37 | Complexity: 38 | 39 | expected worst-case time complexity is O(N*log(N)); 40 | expected worst-case space complexity is O(N), beyond input storage (not counting the storage required for input arguments). 41 | 42 | Elements of input arrays can be modified. 43 | -------------------------------------------------------------------------------- /Lesson 07 - Stacks and Queues/Brackets/solution.cs: -------------------------------------------------------------------------------- 1 | // Code written in C# 2 | // Correctness: 100 % 3 | // Performance: 100 % 4 | // Time Complexity: O(N) 5 | // Space Complexity: O(N) 6 | 7 | using System; 8 | using System.Collections.Generic; 9 | 10 | class Solution { 11 | public int solution(string S) { 12 | int N = S.Length; 13 | 14 | Stack stack = new Stack(); 15 | 16 | for (int i = 0; i < N; i++) { 17 | if ((S[i] == '(') || (S[i] == '{') || (S[i] == '[')) { 18 | stack.Push(S[i]); 19 | } else { 20 | if (stack.Count == 0) { 21 | return 0; 22 | } 23 | 24 | char peek = stack.Peek(); 25 | 26 | if (((peek == '(') && (S[i] == ')')) || ((peek == '{') && (S[i] == '}')) || ((peek == '[') && (S[i] == ']'))) { 27 | stack.Pop(); 28 | } else { 29 | return 0; 30 | } 31 | } 32 | } 33 | 34 | return (stack.Count == 0) ? 1 : 0; 35 | } 36 | } 37 | -------------------------------------------------------------------------------- /Lesson 07 - Stacks and Queues/Brackets/task.txt: -------------------------------------------------------------------------------- 1 | A string S consisting of N characters is considered to be properly nested if any of the following conditions is true: 2 | 3 | S is empty; 4 | S has the form "(U)" or "[U]" or "{U}" where U is a properly nested string; 5 | S has the form "VW" where V and W are properly nested strings. 6 | 7 | For example, the string "{[()()]}" is properly nested but "([)()]" is not. 8 | 9 | Write a function: 10 | 11 | class Solution { public int solution(string S); } 12 | 13 | that, given a string S consisting of N characters, returns 1 if S is properly nested and 0 otherwise. 14 | 15 | For example, given S = "{[()()]}", the function should return 1 and given S = "([)()]", the function should return 0, as explained above. 16 | 17 | Assume that: 18 | 19 | N is an integer within the range [0..200,000]; 20 | string S consists only of the following characters: "(", "{", "[", "]", "}" and/or ")". 21 | 22 | Complexity: 23 | 24 | expected worst-case time complexity is O(N); 25 | expected worst-case space complexity is O(N) (not counting the storage required for input arguments). 26 | -------------------------------------------------------------------------------- /Lesson 07 - Stacks and Queues/Fish/solution.cs: -------------------------------------------------------------------------------- 1 | // Code written in C# 2 | // Correctness: 100 % 3 | // Performance: 100 % 4 | // Time Complexity: O(N) 5 | // Space Complexity: O(N) 6 | 7 | using System; 8 | using System.Collections.Generic; 9 | 10 | class Solution { 11 | public int solution(int[] A, int[] B) { 12 | int N = A.Length; 13 | 14 | Stack upStream = new Stack(); 15 | Stack downStream = new Stack(); 16 | 17 | for (int i = 0; i < N; i++) { 18 | if ((B[i] == 0)) { 19 | upStream.Push(A[i]); 20 | 21 | while (downStream.Count > 0) { 22 | if (downStream.Peek() < A[i]) { 23 | downStream.Pop(); 24 | } else { 25 | upStream.Pop(); 26 | break; 27 | } 28 | } 29 | } else { 30 | downStream.Push(A[i]); 31 | } 32 | } 33 | 34 | return downStream.Count + upStream.Count; 35 | } 36 | } 37 | -------------------------------------------------------------------------------- /Lesson 07 - Stacks and Queues/Fish/task.txt: -------------------------------------------------------------------------------- 1 | You are given two non-empty zero-indexed arrays A and B consisting of N integers. Arrays A and B represent N voracious fish in a river, ordered downstream along the flow of the river. 2 | 3 | The fish are numbered from 0 to N − 1. If P and Q are two fish and P < Q, then fish P is initially upstream of fish Q. Initially, each fish has a unique position. 4 | 5 | Fish number P is represented by A[P] and B[P]. Array A contains the sizes of the fish. All its elements are unique. Array B contains the directions of the fish. It contains only 0s and/or 1s, where: 6 | 7 | 0 represents a fish flowing upstream, 8 | 1 represents a fish flowing downstream. 9 | 10 | If two fish move in opposite directions and there are no other (living) fish between them, they will eventually meet each other. Then only one fish can stay alive − the larger fish eats the smaller one. More precisely, we say that two fish P and Q meet each other when P < Q, B[P] = 1 and B[Q] = 0, and there are no living fish between them. After they meet: 11 | 12 | If A[P] > A[Q] then P eats Q, and P will still be flowing downstream, 13 | If A[Q] > A[P] then Q eats P, and Q will still be flowing upstream. 14 | 15 | We assume that all the fish are flowing at the same speed. That is, fish moving in the same direction never meet. The goal is to calculate the number of fish that will stay alive. 16 | 17 | For example, consider arrays A and B such that: 18 | 19 | A[0] = 4 B[0] = 0 20 | A[1] = 3 B[1] = 1 21 | A[2] = 2 B[2] = 0 22 | A[3] = 1 B[3] = 0 23 | A[4] = 5 B[4] = 0 24 | 25 | Initially all the fish are alive and all except fish number 1 are moving upstream. Fish number 1 meets fish number 2 and eats it, then it meets fish number 3 and eats it too. Finally, it meets fish number 4 and is eaten by it. The remaining two fish, number 0 and 4, never meet and therefore stay alive. 26 | 27 | Write a function: 28 | 29 | class Solution { public int solution(int[] A, int[] B); } 30 | 31 | that, given two non-empty zero-indexed arrays A and B consisting of N integers, returns the number of fish that will stay alive. 32 | 33 | For example, given the arrays shown above, the function should return 2, as explained above. 34 | 35 | Assume that: 36 | 37 | N is an integer within the range [1..100,000]; 38 | each element of array A is an integer within the range [0..1,000,000,000]; 39 | each element of array B is an integer that can have one of the following values: 0, 1; 40 | the elements of A are all distinct. 41 | 42 | Complexity: 43 | 44 | expected worst-case time complexity is O(N); 45 | expected worst-case space complexity is O(N), beyond input storage (not counting the storage required for input arguments). 46 | 47 | Elements of input arrays can be modified. 48 | -------------------------------------------------------------------------------- /Lesson 07 - Stacks and Queues/Nesting/solution.cs: -------------------------------------------------------------------------------- 1 | // Code written in C# 2 | // Correctness: 100 % 3 | // Performance: 100 % 4 | // Time Complexity: O(N) 5 | // Space Complexity: O(1) 6 | 7 | using System; 8 | 9 | class Solution { 10 | public int solution(string S) { 11 | int N = S.Length; 12 | int counter = 0; 13 | 14 | for (int i = 0; i < N; i++) { 15 | if (S[i] == '(') { 16 | counter++; 17 | } else if (S[i] == ')') { 18 | if (--counter < 0) { 19 | return 0; 20 | } 21 | } 22 | } 23 | 24 | return ((counter == 0) ? 1 : 0); 25 | } 26 | } 27 | -------------------------------------------------------------------------------- /Lesson 07 - Stacks and Queues/Nesting/task.txt: -------------------------------------------------------------------------------- 1 | A string S consisting of N characters is called properly nested if: 2 | 3 | S is empty; 4 | S has the form "(U)" where U is a properly nested string; 5 | S has the form "VW" where V and W are properly nested strings. 6 | 7 | For example, string "(()(())())" is properly nested but string "())" isn't. 8 | 9 | Write a function: 10 | 11 | class Solution { public int solution(string S); } 12 | 13 | that, given a string S consisting of N characters, returns 1 if string S is properly nested and 0 otherwise. 14 | 15 | For example, given S = "(()(())())", the function should return 1 and given S = "())", the function should return 0, as explained above. 16 | 17 | Assume that: 18 | 19 | N is an integer within the range [0..1,000,000]; 20 | string S consists only of the characters "(" and/or ")". 21 | 22 | Complexity: 23 | 24 | expected worst-case time complexity is O(N); 25 | expected worst-case space complexity is O(1) (not counting the storage required for input arguments). 26 | -------------------------------------------------------------------------------- /Lesson 07 - Stacks and Queues/Stacks.pdf: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/ghananigans/codility-lesson-solutions/e7b7115f1a1229733cf67236a24c86ad7bc4c0ea/Lesson 07 - Stacks and Queues/Stacks.pdf -------------------------------------------------------------------------------- /Lesson 07 - Stacks and Queues/StoneWall/solution.cs: -------------------------------------------------------------------------------- 1 | // Code written in C# 2 | // Correctness: 100 % 3 | // Performance: 100 % 4 | // Time Complexity: O(N) 5 | // Space Complexity: O(N) 6 | 7 | using System; 8 | using System.Collections.Generic; 9 | 10 | class Solution { 11 | public int solution(int[] H) { 12 | int N = H.Length; 13 | 14 | Stack stack = new Stack(); 15 | stack.Push(H[0]); 16 | 17 | int blocks = 1; 18 | 19 | for (int i = 1; i < N; i++) { 20 | int currentHeight = 0; 21 | 22 | if (stack.Count > 0) { 23 | currentHeight = stack.Peek(); 24 | } 25 | 26 | if (currentHeight > H[i]) { 27 | stack.Pop(); 28 | i--; 29 | } else if (currentHeight < H[i]) { 30 | stack.Push(H[i]); 31 | blocks++; 32 | } 33 | } 34 | 35 | return blocks; 36 | } 37 | } 38 | -------------------------------------------------------------------------------- /Lesson 07 - Stacks and Queues/StoneWall/stonewall.png: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/ghananigans/codility-lesson-solutions/e7b7115f1a1229733cf67236a24c86ad7bc4c0ea/Lesson 07 - Stacks and Queues/StoneWall/stonewall.png -------------------------------------------------------------------------------- /Lesson 07 - Stacks and Queues/StoneWall/task.txt: -------------------------------------------------------------------------------- 1 | You are going to build a stone wall. The wall should be straight and N meters long, and its thickness should be constant; however, it should have different heights in different places. The height of the wall is specified by a zero-indexed array H of N positive integers. H[I] is the height of the wall from I to I+1 meters to the right of its left end. In particular, H[0] is the height of the wall's left end and H[N−1] is the height of the wall's right end. 2 | 3 | The wall should be built of cuboid stone blocks (that is, all sides of such blocks are rectangular). Your task is to compute the minimum number of blocks needed to build the wall. 4 | 5 | Write a function: 6 | 7 | class Solution { public int solution(int[] H); } 8 | 9 | that, given a zero-indexed array H of N positive integers specifying the height of the wall, returns the minimum number of blocks needed to build it. 10 | 11 | For example, given array H containing N = 9 integers: 12 | 13 | H[0] = 8 H[1] = 8 H[2] = 5 14 | H[3] = 7 H[4] = 9 H[5] = 8 15 | H[6] = 7 H[7] = 4 H[8] = 8 16 | 17 | the function should return 7. The figure shows one possible arrangement of seven blocks. 18 | 19 | 20 | 21 | Assume that: 22 | 23 | N is an integer within the range [1..100,000]; 24 | each element of array H is an integer within the range [1..1,000,000,000]. 25 | 26 | Complexity: 27 | 28 | expected worst-case time complexity is O(N); 29 | expected worst-case space complexity is O(N), beyond input storage (not counting the storage required for input arguments). 30 | 31 | Elements of input arrays can be modified. 32 | 33 | -------------------------------------------------------------------------------- /Lesson 08 - Leader/Dominator/solution.php: -------------------------------------------------------------------------------- 1 | ($N/2)) { 42 | return $candidateIndex; 43 | } 44 | 45 | return -1; 46 | } 47 | 48 | ?> 49 | -------------------------------------------------------------------------------- /Lesson 08 - Leader/Dominator/task.txt: -------------------------------------------------------------------------------- 1 | A zero-indexed array A consisting of N integers is given. The dominator of array A is the value that occurs in more than half of the elements of A. 2 | 3 | For example, consider array A such that 4 | 5 | A[0] = 3 A[1] = 4 A[2] = 3 6 | A[3] = 2 A[4] = 3 A[5] = -1 7 | A[6] = 3 A[7] = 3 8 | 9 | The dominator of A is 3 because it occurs in 5 out of 8 elements of A (namely in those with indices 0, 2, 4, 6 and 7) and 5 is more than a half of 8. 10 | 11 | Write a function 12 | 13 | function solution($A); 14 | 15 | that, given a zero-indexed array A consisting of N integers, returns index of any element of array A in which the dominator of A occurs. The function should return −1 if array A does not have a dominator. 16 | 17 | Assume that: 18 | 19 | N is an integer within the range [0..100,000]; 20 | each element of array A is an integer within the range [−2,147,483,648..2,147,483,647]. 21 | 22 | For example, given array A such that 23 | 24 | A[0] = 3 A[1] = 4 A[2] = 3 25 | A[3] = 2 A[4] = 3 A[5] = -1 26 | A[6] = 3 A[7] = 3 27 | 28 | the function may return 0, 2, 4, 6 or 7, as explained above. 29 | 30 | Complexity: 31 | 32 | expected worst-case time complexity is O(N); 33 | expected worst-case space complexity is O(1), beyond input storage (not counting the storage required for input arguments). 34 | 35 | Elements of input arrays can be modified. 36 | -------------------------------------------------------------------------------- /Lesson 08 - Leader/EquiLeader/solution.php: -------------------------------------------------------------------------------- 1 | ($i + 1)) && ((($counter - $counter2) * 2) > ($N - $i - 1))) { 49 | $equiLeaders++; 50 | } 51 | } 52 | 53 | return $equiLeaders; 54 | } 55 | 56 | ?> 57 | -------------------------------------------------------------------------------- /Lesson 08 - Leader/EquiLeader/task.txt: -------------------------------------------------------------------------------- 1 | A non-empty zero-indexed array A consisting of N integers is given. 2 | 3 | The leader of this array is the value that occurs in more than half of the elements of A. 4 | 5 | An equi leader is an index S such that 0 ≤ S < N − 1 and two sequences A[0], A[1], ..., A[S] and A[S + 1], A[S + 2], ..., A[N − 1] have leaders of the same value. 6 | 7 | For example, given array A such that: 8 | 9 | A[0] = 4 10 | A[1] = 3 11 | A[2] = 4 12 | A[3] = 4 13 | A[4] = 4 14 | A[5] = 2 15 | 16 | we can find two equi leaders: 17 | 18 | 0, because sequences: (4) and (3, 4, 4, 4, 2) have the same leader, whose value is 4. 19 | 2, because sequences: (4, 3, 4) and (4, 4, 2) have the same leader, whose value is 4. 20 | 21 | The goal is to count the number of equi leaders. 22 | 23 | Write a function: 24 | 25 | function solution($A); 26 | 27 | that, given a non-empty zero-indexed array A consisting of N integers, returns the number of equi leaders. 28 | 29 | For example, given: 30 | 31 | A[0] = 4 32 | A[1] = 3 33 | A[2] = 4 34 | A[3] = 4 35 | A[4] = 4 36 | A[5] = 2 37 | 38 | the function should return 2, as explained above. 39 | 40 | Assume that: 41 | 42 | N is an integer within the range [1..100,000]; 43 | each element of array A is an integer within the range [−1,000,000,000..1,000,000,000]. 44 | 45 | Complexity: 46 | 47 | expected worst-case time complexity is O(N); 48 | expected worst-case space complexity is O(N), beyond input storage (not counting the storage required for input arguments). 49 | 50 | Elements of input arrays can be modified. 51 | -------------------------------------------------------------------------------- /Lesson 08 - Leader/Leader.pdf: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/ghananigans/codility-lesson-solutions/e7b7115f1a1229733cf67236a24c86ad7bc4c0ea/Lesson 08 - Leader/Leader.pdf -------------------------------------------------------------------------------- /Lesson 09 - Maximum slice problem/MaxDoubleSliceSum/solution.js: -------------------------------------------------------------------------------- 1 | // Code written in JavaScript 2 | // Correctness: 100 % 3 | // Performance: 100 % 4 | // Time Complexity: O(N) 5 | // Space Complexity: O(N) 6 | 7 | function solution(A) { 8 | var N = A.length; 9 | 10 | if (N < 4) { 11 | return 0; 12 | } 13 | 14 | var max_ending = new Array(); 15 | var max_starting = new Array(); 16 | 17 | max_ending[0] = 0; 18 | max_ending[N - 1] = 0; 19 | max_starting[0] = 0; 20 | max_starting[N - 1] = 0; 21 | 22 | var i = 0; 23 | 24 | for (i = 1; i < (N - 1); i++) { 25 | max_ending[i] = Math.max(0, max_ending[i - 1] + A[i]); 26 | max_starting[N - 1 - i] = Math.max(0, max_starting[N - i] + A[N - 1 - i]); 27 | } 28 | 29 | 30 | var max = 0; 31 | 32 | for (i = 1; i < (N - 1); i++) { 33 | max = Math.max(max, max_ending[i - 1] + max_starting[i + 1]); 34 | } 35 | 36 | return max; 37 | } 38 | -------------------------------------------------------------------------------- /Lesson 09 - Maximum slice problem/MaxDoubleSliceSum/task.txt: -------------------------------------------------------------------------------- 1 | A non-empty zero-indexed array A consisting of N integers is given. 2 | 3 | A triplet (X, Y, Z), such that 0 ≤ X < Y < Z < N, is called a double slice. 4 | 5 | The sum of double slice (X, Y, Z) is the total of A[X + 1] + A[X + 2] + ... + A[Y − 1] + A[Y + 1] + A[Y + 2] + ... + A[Z − 1]. 6 | 7 | For example, array A such that: 8 | 9 | A[0] = 3 10 | A[1] = 2 11 | A[2] = 6 12 | A[3] = -1 13 | A[4] = 4 14 | A[5] = 5 15 | A[6] = -1 16 | A[7] = 2 17 | 18 | contains the following example double slices: 19 | 20 | double slice (0, 3, 6), sum is 2 + 6 + 4 + 5 = 17, 21 | double slice (0, 3, 7), sum is 2 + 6 + 4 + 5 − 1 = 16, 22 | double slice (3, 4, 5), sum is 0. 23 | 24 | The goal is to find the maximal sum of any double slice. 25 | 26 | Write a function: 27 | 28 | function solution(A); 29 | 30 | that, given a non-empty zero-indexed array A consisting of N integers, returns the maximal sum of any double slice. 31 | 32 | For example, given: 33 | 34 | A[0] = 3 35 | A[1] = 2 36 | A[2] = 6 37 | A[3] = -1 38 | A[4] = 4 39 | A[5] = 5 40 | A[6] = -1 41 | A[7] = 2 42 | 43 | the function should return 17, because no double slice of array A has a sum of greater than 17. 44 | 45 | Assume that: 46 | 47 | N is an integer within the range [3..100,000]; 48 | each element of array A is an integer within the range [−10,000..10,000]. 49 | 50 | Complexity: 51 | 52 | expected worst-case time complexity is O(N); 53 | expected worst-case space complexity is O(N), beyond input storage (not counting the storage required for input arguments). 54 | 55 | Elements of input arrays can be modified. 56 | -------------------------------------------------------------------------------- /Lesson 09 - Maximum slice problem/MaxProfit/solution.js: -------------------------------------------------------------------------------- 1 | // Code written in JavaScript 2 | // Correctness: 100 % 3 | // Performance: 100 % 4 | // Time Complexity: O(N) 5 | // Space Complexity: O(1) 6 | 7 | function solution(A) { 8 | var N = A.length; 9 | 10 | var max_profit = 0; 11 | var min = 200001; 12 | 13 | for (var i = 0; i < N; i++) { 14 | if (min > A[i]) { 15 | min = A[i]; 16 | } else { 17 | max_profit = Math.max(max_profit, A[i] - min); 18 | } 19 | } 20 | 21 | return max_profit; 22 | } 23 | -------------------------------------------------------------------------------- /Lesson 09 - Maximum slice problem/MaxProfit/task.txt: -------------------------------------------------------------------------------- 1 | A zero-indexed array A consisting of N integers is given. It contains daily prices of a stock share for a period of N consecutive days. If a single share was bought on day P and sold on day Q, where 0 ≤ P ≤ Q < N, then the profit of such transaction is equal to A[Q] − A[P], provided that A[Q] ≥ A[P]. Otherwise, the transaction brings loss of A[P] − A[Q]. 2 | 3 | For example, consider the following array A consisting of six elements such that: 4 | 5 | A[0] = 23171 6 | A[1] = 21011 7 | A[2] = 21123 8 | A[3] = 21366 9 | A[4] = 21013 10 | A[5] = 21367 11 | 12 | If a share was bought on day 0 and sold on day 2, a loss of 2048 would occur because A[2] − A[0] = 21123 − 23171 = −2048. If a share was bought on day 4 and sold on day 5, a profit of 354 would occur because A[5] − A[4] = 21367 − 21013 = 354. Maximum possible profit was 356. It would occur if a share was bought on day 1 and sold on day 5. 13 | 14 | Write a function, 15 | 16 | function solution(A); 17 | 18 | that, given a zero-indexed array A consisting of N integers containing daily prices of a stock share for a period of N consecutive days, returns the maximum possible profit from one transaction during this period. The function should return 0 if it was impossible to gain any profit. 19 | 20 | For example, given array A consisting of six elements such that: 21 | 22 | A[0] = 23171 23 | A[1] = 21011 24 | A[2] = 21123 25 | A[3] = 21366 26 | A[4] = 21013 27 | A[5] = 21367 28 | 29 | the function should return 356, as explained above. 30 | 31 | Assume that: 32 | 33 | N is an integer within the range [0..400,000]; 34 | each element of array A is an integer within the range [0..200,000]. 35 | 36 | Complexity: 37 | 38 | expected worst-case time complexity is O(N); 39 | expected worst-case space complexity is O(1), beyond input storage (not counting the storage required for input arguments). 40 | 41 | Elements of input arrays can be modified. 42 | 43 | -------------------------------------------------------------------------------- /Lesson 09 - Maximum slice problem/MaxSlice.pdf: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/ghananigans/codility-lesson-solutions/e7b7115f1a1229733cf67236a24c86ad7bc4c0ea/Lesson 09 - Maximum slice problem/MaxSlice.pdf -------------------------------------------------------------------------------- /Lesson 09 - Maximum slice problem/MaxSliceSum/solution.js: -------------------------------------------------------------------------------- 1 | // Code written in JavaScript 2 | // Correctness: 100 % 3 | // Performance: 100 % 4 | // Time Complexity: O(N) 5 | // Space Complexity: O(1) 6 | 7 | function solution(A) { 8 | var N = A.length; 9 | 10 | var max_slice = A[0]; 11 | var max_ending = A[0]; 12 | 13 | for (var i = 1; i < N; i++) { 14 | max_ending = Math.max(A[i], max_ending + A[i]); 15 | max_slice = Math.max(max_slice, max_ending); 16 | } 17 | 18 | return max_slice; 19 | }} 20 | -------------------------------------------------------------------------------- /Lesson 09 - Maximum slice problem/MaxSliceSum/task.txt: -------------------------------------------------------------------------------- 1 | A non-empty zero-indexed array A consisting of N integers is given. A pair of integers (P, Q), such that 0 ≤ P ≤ Q < N, is called a slice of array A. The sum of a slice (P, Q) is the total of A[P] + A[P+1] + ... + A[Q]. 2 | 3 | Write a function: 4 | 5 | function solution(A); 6 | 7 | that, given an array A consisting of N integers, returns the maximum sum of any slice of A. 8 | 9 | For example, given array A such that: 10 | 11 | A[0] = 3 A[1] = 2 A[2] = -6 12 | A[3] = 4 A[4] = 0 13 | 14 | the function should return 5 because: 15 | 16 | (3, 4) is a slice of A that has sum 4, 17 | (2, 2) is a slice of A that has sum −6, 18 | (0, 1) is a slice of A that has sum 5, 19 | no other slice of A has sum greater than (0, 1). 20 | 21 | Assume that: 22 | 23 | N is an integer within the range [1..1,000,000]; 24 | each element of array A is an integer within the range [−1,000,000..1,000,000]; 25 | the result will be an integer within the range [−2,147,483,648..2,147,483,647]. 26 | 27 | Complexity: 28 | 29 | expected worst-case time complexity is O(N); 30 | expected worst-case space complexity is O(N), beyond input storage (not counting the storage required for input arguments). 31 | 32 | Elements of input arrays can be modified. 33 | 34 | -------------------------------------------------------------------------------- /Lesson 10 - Prime and composite numbers/CountFactors/solution.rb: -------------------------------------------------------------------------------- 1 | # Code written in Ruby 2 | # Correctness: 100 % 3 | # Performance: 100 % 4 | # Time Complexity: O(sqrt(n)) 5 | # Space Complexity: O(1) 6 | 7 | def solution(n) 8 | i = Math.sqrt(n).floor 9 | factors = 0 10 | 11 | while i > 0 do 12 | k = n / i 13 | 14 | if k * i == n 15 | factors += k == i ? 1 : 2 16 | end 17 | 18 | i -= 1 19 | end 20 | 21 | return factors 22 | end 23 | 24 | 25 | 26 | -------------------------------------------------------------------------------- /Lesson 10 - Prime and composite numbers/CountFactors/task.txt: -------------------------------------------------------------------------------- 1 | A positive integer D is a factor of a positive integer N if there exists an integer M such that N = D * M. 2 | 3 | For example, 6 is a factor of 24, because M = 4 satisfies the above condition (24 = 6 * 4). 4 | 5 | Write a function: 6 | 7 | def solution(n) 8 | 9 | that, given a positive integer N, returns the number of its factors. 10 | 11 | For example, given N = 24, the function should return 8, because 24 has 8 factors, namely 1, 2, 3, 4, 6, 8, 12, 24. There are no other factors of 24. 12 | 13 | Assume that: 14 | 15 | N is an integer within the range [1..2,147,483,647]. 16 | 17 | Complexity: 18 | 19 | expected worst-case time complexity is O(sqrt(N)); 20 | expected worst-case space complexity is O(1). 21 | 22 | 23 | -------------------------------------------------------------------------------- /Lesson 10 - Prime and composite numbers/Flags/flags.png: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/ghananigans/codility-lesson-solutions/e7b7115f1a1229733cf67236a24c86ad7bc4c0ea/Lesson 10 - Prime and composite numbers/Flags/flags.png -------------------------------------------------------------------------------- /Lesson 10 - Prime and composite numbers/Flags/solution.rb: -------------------------------------------------------------------------------- 1 | # Code written in Ruby 2 | # Correctness: 100 % 3 | # Performance: 100 % 4 | # Time Complexity: O(N) 5 | # Space Complexity: O(N) 6 | 7 | def solution(a) 8 | _N = a.length 9 | 10 | if _N < 3 11 | return 0 12 | end 13 | 14 | next_peak = [-1] * _N 15 | 16 | i = _N - 2 17 | 18 | while i > 0 do 19 | if a[i - 1] < a[i] && a[i] > a[i + 1] 20 | next_peak[i - 1] = i 21 | else 22 | next_peak[i - 1] = next_peak[i] 23 | end 24 | 25 | i -= 1 26 | end 27 | 28 | if next_peak[0] == -1 29 | return 0 30 | end 31 | 32 | flags = 0 33 | 34 | max_flags = Math.sqrt(_N).ceil 35 | 36 | k = 1 37 | 38 | while k <= max_flags do 39 | last_flagged_peak_index = next_peak[0] 40 | peak_index = next_peak[0] 41 | 42 | flags_detected = 1 43 | 44 | while peak_index < _N && flags_detected < k do 45 | next_peak_index = next_peak[peak_index] 46 | 47 | if next_peak_index == -1 48 | break 49 | end 50 | 51 | if next_peak_index - last_flagged_peak_index >= k 52 | flags_detected += 1 53 | last_flagged_peak_index = next_peak_index 54 | end 55 | 56 | peak_index = next_peak_index 57 | end 58 | 59 | if flags_detected > flags 60 | flags = flags_detected 61 | end 62 | 63 | k += 1 64 | end 65 | 66 | return flags 67 | end 68 | 69 | -------------------------------------------------------------------------------- /Lesson 10 - Prime and composite numbers/Flags/task.txt: -------------------------------------------------------------------------------- 1 | A non-empty zero-indexed array A consisting of N integers is given. 2 | 3 | A peak is an array element which is larger than its neighbours. More precisely, it is an index P such that 0 < P < N − 1 and A[P − 1] < A[P] > A[P + 1]. 4 | 5 | For example, the following array A: 6 | 7 | A[0] = 1 8 | A[1] = 5 9 | A[2] = 3 10 | A[3] = 4 11 | A[4] = 3 12 | A[5] = 4 13 | A[6] = 1 14 | A[7] = 2 15 | A[8] = 3 16 | A[9] = 4 17 | A[10] = 6 18 | A[11] = 2 19 | 20 | has exactly four peaks: elements 1, 3, 5 and 10. 21 | 22 | You are going on a trip to a range of mountains whose relative heights are represented by array A, as shown in a figure below. You have to choose how many flags you should take with you. The goal is to set the maximum number of flags on the peaks, according to certain rules. 23 | 24 | 25 | 26 | Flags can only be set on peaks. What's more, if you take K flags, then the distance between any two flags should be greater than or equal to K. The distance between indices P and Q is the absolute value |P − Q|. 27 | 28 | For example, given the mountain range represented by array A, above, with N = 12, if you take: 29 | 30 | two flags, you can set them on peaks 1 and 5; 31 | three flags, you can set them on peaks 1, 5 and 10; 32 | four flags, you can set only three flags, on peaks 1, 5 and 10. 33 | 34 | You can therefore set a maximum of three flags in this case. 35 | 36 | Write a function: 37 | 38 | def solution(a) 39 | 40 | that, given a non-empty zero-indexed array A of N integers, returns the maximum number of flags that can be set on the peaks of the array. 41 | 42 | For example, the following array A: 43 | 44 | A[0] = 1 45 | A[1] = 5 46 | A[2] = 3 47 | A[3] = 4 48 | A[4] = 3 49 | A[5] = 4 50 | A[6] = 1 51 | A[7] = 2 52 | A[8] = 3 53 | A[9] = 4 54 | A[10] = 6 55 | A[11] = 2 56 | 57 | the function should return 3, as explained above. 58 | 59 | Assume that: 60 | 61 | N is an integer within the range [1..200,000]; 62 | each element of array A is an integer within the range [0..1,000,000,000]. 63 | 64 | Complexity: 65 | 66 | expected worst-case time complexity is O(N); 67 | expected worst-case space complexity is O(N), beyond input storage (not counting the storage required for input arguments). 68 | 69 | Elements of input arrays can be modified. 70 | 71 | -------------------------------------------------------------------------------- /Lesson 10 - Prime and composite numbers/MinPerimeterRectangle/solution.rb: -------------------------------------------------------------------------------- 1 | # Code written in ruby 2 | # Correctness: 100 % 3 | # Performance: 100 % 4 | # Time Complexity: O(sqrt(n)) 5 | # Space Complexity: O(1) 6 | 7 | def solution(n) 8 | i = Math.sqrt(n).floor 9 | 10 | while i > 0 do 11 | k = n / i 12 | 13 | if k * i == n 14 | return 2 * (k + i) 15 | end 16 | 17 | i -= 1 18 | end 19 | end 20 | -------------------------------------------------------------------------------- /Lesson 10 - Prime and composite numbers/MinPerimeterRectangle/task.txt: -------------------------------------------------------------------------------- 1 | An integer N is given, representing the area of some rectangle. 2 | 3 | The area of a rectangle whose sides are of length A and B is A * B, and the perimeter is 2 * (A + B). 4 | 5 | The goal is to find the minimal perimeter of any rectangle whose area equals N. The sides of this rectangle should be only integers. 6 | 7 | For example, given integer N = 30, rectangles of area 30 are: 8 | 9 | (1, 30), with a perimeter of 62, 10 | (2, 15), with a perimeter of 34, 11 | (3, 10), with a perimeter of 26, 12 | (5, 6), with a perimeter of 22. 13 | 14 | Write a function: 15 | 16 | def solution(n) 17 | 18 | that, given an integer N, returns the minimal perimeter of any rectangle whose area is exactly equal to N. 19 | 20 | For example, given an integer N = 30, the function should return 22, as explained above. 21 | 22 | Assume that: 23 | 24 | N is an integer within the range [1..1,000,000,000]. 25 | 26 | Complexity: 27 | 28 | expected worst-case time complexity is O(sqrt(N)); 29 | expected worst-case space complexity is O(1). 30 | -------------------------------------------------------------------------------- /Lesson 10 - Prime and composite numbers/Peaks/solution.rb: -------------------------------------------------------------------------------- 1 | # Code written in Ruby 2 | # Correctness: 100 % 3 | # Performance: 100 % 4 | # Time Complexity: O(N*log(log(N))) 5 | # Space Complexity: O(N) 6 | 7 | def solution(a) 8 | _N = a.length 9 | 10 | peaks = Array.new() 11 | 12 | i = 1 13 | 14 | while i < _N -1 do 15 | if a[i - 1] < a[i] && a[i] > a[i + 1] 16 | peaks.push(i) 17 | i += 2 18 | else 19 | i += 1 20 | end 21 | end 22 | 23 | blocks = peaks.length 24 | 25 | while blocks > 0 26 | if _N % blocks != 0 27 | blocks -= 1 28 | next 29 | end 30 | 31 | itemsPerBlock = _N / blocks 32 | 33 | blocksDetected = 0 34 | 35 | for peak in peaks 36 | if peak / itemsPerBlock == blocksDetected 37 | blocksDetected += 1 38 | end 39 | end 40 | 41 | if blocksDetected == blocks 42 | return blocks 43 | else 44 | blocks -= 1 45 | end 46 | end 47 | 48 | return 0 49 | end 50 | 51 | -------------------------------------------------------------------------------- /Lesson 10 - Prime and composite numbers/Peaks/task.txt: -------------------------------------------------------------------------------- 1 | A non-empty zero-indexed array A consisting of N integers is given. 2 | 3 | A peak is an array element which is larger than its neighbors. More precisely, it is an index P such that 0 < P < N − 1, A[P − 1] < A[P] and A[P] > A[P + 1]. 4 | 5 | For example, the following array A: 6 | 7 | A[0] = 1 8 | A[1] = 2 9 | A[2] = 3 10 | A[3] = 4 11 | A[4] = 3 12 | A[5] = 4 13 | A[6] = 1 14 | A[7] = 2 15 | A[8] = 3 16 | A[9] = 4 17 | A[10] = 6 18 | A[11] = 2 19 | 20 | has exactly three peaks: 3, 5, 10. 21 | 22 | We want to divide this array into blocks containing the same number of elements. More precisely, we want to choose a number K that will yield the following blocks: 23 | 24 | A[0], A[1], ..., A[K − 1], 25 | A[K], A[K + 1], ..., A[2K − 1], 26 | ... 27 | A[N − K], A[N − K + 1], ..., A[N − 1]. 28 | 29 | What's more, every block should contain at least one peak. Notice that extreme elements of the blocks (for example A[K − 1] or A[K]) can also be peaks, but only if they have both neighbors (including one in an adjacent blocks). 30 | 31 | The goal is to find the maximum number of blocks into which the array A can be divided. 32 | 33 | Array A can be divided into blocks as follows: 34 | 35 | one block (1, 2, 3, 4, 3, 4, 1, 2, 3, 4, 6, 2). This block contains three peaks. 36 | two blocks (1, 2, 3, 4, 3, 4) and (1, 2, 3, 4, 6, 2). Every block has a peak. 37 | three blocks (1, 2, 3, 4), (3, 4, 1, 2), (3, 4, 6, 2). Every block has a peak. Notice in particular that the first block (1, 2, 3, 4) has a peak at A[3], because A[2] < A[3] > A[4], even though A[4] is in the adjacent block. 38 | 39 | However, array A cannot be divided into four blocks, (1, 2, 3), (4, 3, 4), (1, 2, 3) and (4, 6, 2), because the (1, 2, 3) blocks do not contain a peak. Notice in particular that the (4, 3, 4) block contains two peaks: A[3] and A[5]. 40 | 41 | The maximum number of blocks that array A can be divided into is three. 42 | 43 | Write a function: 44 | 45 | def solution(a) 46 | 47 | that, given a non-empty zero-indexed array A consisting of N integers, returns the maximum number of blocks into which A can be divided. 48 | 49 | If A cannot be divided into some number of blocks, the function should return 0. 50 | 51 | For example, given: 52 | 53 | A[0] = 1 54 | A[1] = 2 55 | A[2] = 3 56 | A[3] = 4 57 | A[4] = 3 58 | A[5] = 4 59 | A[6] = 1 60 | A[7] = 2 61 | A[8] = 3 62 | A[9] = 4 63 | A[10] = 6 64 | A[11] = 2 65 | 66 | the function should return 3, as explained above. 67 | 68 | Assume that: 69 | 70 | N is an integer within the range [1..100,000]; 71 | each element of array A is an integer within the range [0..1,000,000,000]. 72 | 73 | Complexity: 74 | 75 | expected worst-case time complexity is O(N*log(log(N))); 76 | expected worst-case space complexity is O(N), beyond input storage (not counting the storage required for input arguments). 77 | 78 | Elements of input arrays can be modified. 79 | -------------------------------------------------------------------------------- /Lesson 10 - Prime and composite numbers/PrimeNumbers.pdf: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/ghananigans/codility-lesson-solutions/e7b7115f1a1229733cf67236a24c86ad7bc4c0ea/Lesson 10 - Prime and composite numbers/PrimeNumbers.pdf -------------------------------------------------------------------------------- /Lesson 11 - Sieve of Eratosthenes/CountNonDivisible/solution.pl: -------------------------------------------------------------------------------- 1 | # Code written in Perl 2 | # Correctness: 100 % 3 | # Performance: 100 % 4 | # Time Complexity: O(N*log(N)) 5 | # Space Complexity: O(N) 6 | 7 | sub solution { 8 | my (@A)=@_; 9 | my $N = $#A + 1; 10 | my @occurrences = (0)x($N * 2 + 1); 11 | my @factors = (0)x($N * 2 + 1); 12 | 13 | for (my $i = 0; $i < $N; $i++) { 14 | @occurrences[@A[$i]]++; 15 | @factors[@A[$i]]++; 16 | } 17 | 18 | @factors[1] = 0; 19 | 20 | for (my $i = 2; $i * $i <= $N * 2; $i++) { 21 | @factors[$i * $i] += @occurrences[$i]; 22 | 23 | for (my $j = $i * ($i + 1); $j <= $N * 2; $j += $i) { 24 | @factors[$j] += @occurrences[$i] + @occurrences[$j / $i]; 25 | } 26 | 27 | } 28 | 29 | my @non_divisible_factors = (0)x$N; 30 | 31 | for (my $i = 0; $i < $N; $i++) { 32 | @non_divisible_factors[$i] = $N - @factors[@A[$i]] - @occurrences[1]; 33 | } 34 | 35 | return @non_divisible_factors; 36 | } 37 | -------------------------------------------------------------------------------- /Lesson 11 - Sieve of Eratosthenes/CountNonDivisible/task.txt: -------------------------------------------------------------------------------- 1 | You are given a non-empty zero-indexed array A consisting of N integers. 2 | 3 | For each number A[i] such that 0 ≤ i < N, we want to count the number of elements of the array that are not the divisors of A[i]. We say that these elements are non-divisors. 4 | 5 | For example, consider integer N = 5 and array A such that: 6 | 7 | A[0] = 3 8 | A[1] = 1 9 | A[2] = 2 10 | A[3] = 3 11 | A[4] = 6 12 | 13 | For the following elements: 14 | 15 | A[0] = 3, the non-divisors are: 2, 6, 16 | A[1] = 1, the non-divisors are: 3, 2, 3, 6, 17 | A[2] = 2, the non-divisors are: 3, 3, 6, 18 | A[3] = 3, the non-divisors are: 2, 6, 19 | A[6] = 6, there aren't any non-divisors. 20 | 21 | Write a function: 22 | 23 | sub solution { my (@A)=@_; ... } 24 | 25 | that, given a non-empty zero-indexed array A consisting of N integers, returns a sequence of integers representing the amount of non-divisors. 26 | 27 | The sequence should be returned as: 28 | 29 | a structure Results (in C), or 30 | a vector of integers (in C++), or 31 | a record Results (in Pascal), or 32 | an array of integers (in any other programming language). 33 | 34 | For example, given: 35 | 36 | A[0] = 3 37 | A[1] = 1 38 | A[2] = 2 39 | A[3] = 3 40 | A[4] = 6 41 | 42 | the function should return [2, 4, 3, 2, 0], as explained above. 43 | 44 | Assume that: 45 | 46 | N is an integer within the range [1..50,000]; 47 | each element of array A is an integer within the range [1..2 * N]. 48 | 49 | Complexity: 50 | 51 | expected worst-case time complexity is O(N*log(N)); 52 | expected worst-case space complexity is O(N), beyond input storage (not counting the storage required for input arguments). 53 | 54 | Elements of input arrays can be modified. 55 | 56 | -------------------------------------------------------------------------------- /Lesson 11 - Sieve of Eratosthenes/CountSemiprimes/solution.pl: -------------------------------------------------------------------------------- 1 | # Code written in Perl 2 | # Correctness: 100 % 3 | # Performance: 100 % 4 | # Time Complexity: O(N*log(log(N)) + M) 5 | # Space Complexity: O(N + M) 6 | 7 | sub solution { 8 | my ($N, $P, $Q)=@_; my @P=@$P; my @Q=@$Q; 9 | 10 | my $M = $#P + 1; 11 | 12 | if ($N < 4) { 13 | return (0)x$M; 14 | } 15 | 16 | my @sieve = (1)x($N + 1); 17 | @sieve[0] = @sieve[1] = 0; 18 | 19 | for (my $i = 2; $i * $i <= $N; $i++) { 20 | if (@sieve[$i]) { 21 | for (my $k = $i * $i; $k <= $N; $k += $i) { 22 | @sieve[$k] = 0; 23 | } 24 | } 25 | } 26 | 27 | my @semiprimes_count = (0)x($N + 1); 28 | 29 | for (my $i = 2; $i * $i <= $N; $i++) { 30 | if (@sieve[$i]) { 31 | for (my $k = $i; $k * $i <= $N; $k++) { 32 | if (@sieve[$k]) { 33 | @semiprimes_count[$k * $i] = 1; 34 | } 35 | } 36 | } 37 | } 38 | 39 | for (my $i = 1; $i <= $N; $i++) { 40 | @semiprimes_count[$i] = @semiprimes_count[$i] + @semiprimes_count[$i - 1]; 41 | } 42 | 43 | my @semiprimes = (0)x$M; 44 | 45 | for ($m = 0; $m < $M; $m++) { 46 | @semiprimes[$m] = @semiprimes_count[@Q[$m]] - @semiprimes_count[@P[$m] - 1]; 47 | } 48 | 49 | return @semiprimes; 50 | } 51 | 52 | -------------------------------------------------------------------------------- /Lesson 11 - Sieve of Eratosthenes/CountSemiprimes/task.txt: -------------------------------------------------------------------------------- 1 | A prime is a positive integer X that has exactly two distinct divisors: 1 and X. The first few prime integers are 2, 3, 5, 7, 11 and 13. 2 | 3 | A semiprime is a natural number that is the product of two (not necessarily distinct) prime numbers. The first few semiprimes are 4, 6, 9, 10, 14, 15, 21, 22, 25, 26. 4 | 5 | You are given two non-empty zero-indexed arrays P and Q, each consisting of M integers. These arrays represent queries about the number of semiprimes within specified ranges. 6 | 7 | Query K requires you to find the number of semiprimes within the range (P[K], Q[K]), where 1 ≤ P[K] ≤ Q[K] ≤ N. 8 | 9 | For example, consider an integer N = 26 and arrays P, Q such that: 10 | 11 | P[0] = 1 Q[0] = 26 12 | P[1] = 4 Q[1] = 10 13 | P[2] = 16 Q[2] = 20 14 | 15 | The number of semiprimes within each of these ranges is as follows: 16 | 17 | (1, 26) is 10, 18 | (4, 10) is 4, 19 | (16, 20) is 0. 20 | 21 | Write a function: 22 | 23 | sub solution { my ($N, $P, $Q)=@_; my @P=@$P; my @Q=@$Q; ... } 24 | 25 | that, given an integer N and two non-empty zero-indexed arrays P and Q consisting of M integers, returns an array consisting of M elements specifying the consecutive answers to all the queries. 26 | 27 | For example, given an integer N = 26 and arrays P, Q such that: 28 | 29 | P[0] = 1 Q[0] = 26 30 | P[1] = 4 Q[1] = 10 31 | P[2] = 16 Q[2] = 20 32 | 33 | the function should return the values [10, 4, 0], as explained above. 34 | 35 | Assume that: 36 | 37 | N is an integer within the range [1..50,000]; 38 | M is an integer within the range [1..30,000]; 39 | each element of arrays P, Q is an integer within the range [1..N]; 40 | P[i] ≤ Q[i]. 41 | 42 | Complexity: 43 | 44 | expected worst-case time complexity is O(N*log(log(N))+M); 45 | expected worst-case space complexity is O(N+M), beyond input storage (not counting the storage required for input arguments). 46 | 47 | Elements of input arrays can be modified. 48 | 49 | -------------------------------------------------------------------------------- /Lesson 11 - Sieve of Eratosthenes/Sieve.pdf: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/ghananigans/codility-lesson-solutions/e7b7115f1a1229733cf67236a24c86ad7bc4c0ea/Lesson 11 - Sieve of Eratosthenes/Sieve.pdf -------------------------------------------------------------------------------- /Lesson 12 - Euclidean algorithm/ChocolatesByNumbers/solution.py: -------------------------------------------------------------------------------- 1 | # Code written in Python 2 | # Correctness: 100 % 3 | # Performance: 100 % 4 | # Time Complexity: O(log(N + M)) 5 | # Space Complexity: O(log(N + M)) 6 | 7 | def solution(N, M): 8 | nm_gcd = gcd(N, M, 1) 9 | return N / nm_gcd 10 | 11 | def gcd(a, b, res): 12 | if (a == b): 13 | return res * a 14 | elif (a % 2 == 0) and (b % 2 == 0): 15 | return gcd(a // 2, b // 2, res * 2) 16 | elif (a % 2 == 0): 17 | return gcd(a // 2, b, res) 18 | elif (b % 2 == 0): 19 | return gcd(a, b // 2, res) 20 | elif (a > b): 21 | return gcd(a - b, b, res) 22 | else: 23 | return gcd(a, b - a, res) 24 | -------------------------------------------------------------------------------- /Lesson 12 - Euclidean algorithm/ChocolatesByNumbers/task.txt: -------------------------------------------------------------------------------- 1 | Two positive integers N and M are given. Integer N represents the number of chocolates arranged in a circle, numbered from 0 to N − 1. 2 | 3 | You start to eat the chocolates. After eating a chocolate you leave only a wrapper. 4 | 5 | You begin with eating chocolate number 0. Then you omit the next M − 1 chocolates or wrappers on the circle, and eat the following one. 6 | 7 | More precisely, if you ate chocolate number X, then you will next eat the chocolate with number (X + M) modulo N (remainder of division). 8 | 9 | You stop eating when you encounter an empty wrapper. 10 | 11 | For example, given integers N = 10 and M = 4. You will eat the following chocolates: 0, 4, 8, 2, 6. 12 | 13 | The goal is to count the number of chocolates that you will eat, following the above rules. 14 | 15 | Write a function: 16 | 17 | def solution(N, M) 18 | 19 | that, given two positive integers N and M, returns the number of chocolates that you will eat. 20 | 21 | For example, given integers N = 10 and M = 4. the function should return 5, as explained above. 22 | 23 | Assume that: 24 | 25 | N and M are integers within the range [1..1,000,000,000]. 26 | 27 | Complexity: 28 | 29 | expected worst-case time complexity is O(log(N+M)); 30 | expected worst-case space complexity is O(log(N+M)). 31 | 32 | 33 | -------------------------------------------------------------------------------- /Lesson 12 - Euclidean algorithm/CommonPrimeDivisors/solution.py: -------------------------------------------------------------------------------- 1 | # Code written in Python 2 | # Correctness: 100 % 3 | # Performance: 100 % 4 | # Time Complexity: O(Z * log(max(A) + max(B))^2) 5 | # Space Complexity: O(1) 6 | 7 | def solution(A, B): 8 | Z = len(A) 9 | 10 | counter = 0 11 | 12 | for i in xrange(Z): 13 | if commonPrimeDivisors(A[i], B[i]): 14 | counter += 1 15 | 16 | return counter 17 | 18 | 19 | def gcd(a, b): 20 | if a % b == 0: 21 | return b 22 | else: 23 | return gcd(b, a % b) 24 | 25 | 26 | def commonPrimeDivisors(a, b): 27 | ab_gcd = gcd(a, b) 28 | 29 | while a != 1: 30 | a_gcd = gcd(a, ab_gcd ) 31 | 32 | if a_gcd == 1: 33 | break 34 | 35 | a /= a_gcd 36 | 37 | if a != 1: 38 | return False 39 | 40 | while b != 1: 41 | b_gcd = gcd(b, ab_gcd ) 42 | 43 | if b_gcd == 1: 44 | break 45 | 46 | b /= b_gcd 47 | 48 | if b != 1: 49 | return False 50 | 51 | return True 52 | -------------------------------------------------------------------------------- /Lesson 12 - Euclidean algorithm/CommonPrimeDivisors/task.txt: -------------------------------------------------------------------------------- 1 | A prime is a positive integer X that has exactly two distinct divisors: 1 and X. The first few prime integers are 2, 3, 5, 7, 11 and 13. 2 | 3 | A prime D is called a prime divisor of a positive integer P if there exists a positive integer K such that D * K = P. For example, 2 and 5 are prime divisors of 20. 4 | 5 | You are given two positive integers N and M. The goal is to check whether the sets of prime divisors of integers N and M are exactly the same. 6 | 7 | For example, given: 8 | 9 | N = 15 and M = 75, the prime divisors are the same: {3, 5}; 10 | N = 10 and M = 30, the prime divisors aren't the same: {2, 5} is not equal to {2, 3, 5}; 11 | N = 9 and M = 5, the prime divisors aren't the same: {3} is not equal to {5}. 12 | 13 | Write a function: 14 | 15 | def solution(A, B) 16 | 17 | that, given two non-empty zero-indexed arrays A and B of Z integers, returns the number of positions K for which the prime divisors of A[K] and B[K] are exactly the same. 18 | 19 | For example, given: 20 | 21 | A[0] = 15 B[0] = 75 22 | A[1] = 10 B[1] = 30 23 | A[2] = 3 B[2] = 5 24 | 25 | the function should return 1, because only one pair (15, 75) has the same set of prime divisors. 26 | 27 | Assume that: 28 | 29 | Z is an integer within the range [1..6,000]; 30 | each element of arrays A, B is an integer within the range [1..2,147,483,647]. 31 | 32 | Complexity: 33 | 34 | expected worst-case time complexity is O(Z*log(max(A)+max(B))2); 35 | expected worst-case space complexity is O(1), beyond input storage (not counting the storage required for input arguments). 36 | 37 | Elements of input arrays can be modified. 38 | 39 | -------------------------------------------------------------------------------- /Lesson 12 - Euclidean algorithm/Gcd.pdf: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/ghananigans/codility-lesson-solutions/e7b7115f1a1229733cf67236a24c86ad7bc4c0ea/Lesson 12 - Euclidean algorithm/Gcd.pdf -------------------------------------------------------------------------------- /Lesson 13 - Fibonacci numbers/FibFrog/solution.cpp: -------------------------------------------------------------------------------- 1 | // Code written in C++ 2 | // Correctness: 100 % 3 | // Performance: 100 % 4 | // Time Complexity: O(N*log(N)) 5 | // Space Complexity: O(N) 6 | 7 | #include 8 | #include 9 | #include 10 | 11 | const int MAX_N_VALUE = 100000; 12 | 13 | int solution(vector &A) { 14 | const int N = A.size(); 15 | 16 | // If only 1 or 2 leaves, 17 | // can jump that in 1 jump 18 | if (N < 3) { 19 | return 1; 20 | } 21 | 22 | // Generate fibonacci numbers 23 | std::vector fibNums; 24 | fibNums.push_back(0); 25 | fibNums.push_back(1); 26 | 27 | for (int i = 2; fibNums.back() < MAX_N_VALUE; ++i) { 28 | fibNums.push_back(fibNums[i-1] + fibNums[i-2]); 29 | } 30 | 31 | // Reachable leaves vector 32 | std::vector reachable(N); 33 | for (int i = 0; i < N; ++i) { 34 | reachable[i] = 0; 35 | } 36 | 37 | int pos; 38 | 39 | // Queue of leaves positions to analyze, starting at -1 40 | std::queue leavesToLookAt; 41 | leavesToLookAt.push(-1); 42 | 43 | // Get reachable leaves after first jump 44 | for (int i = 2; i < fibNums.size(); ++i) { 45 | pos = fibNums[i] - 1; 46 | 47 | if (pos > N) { 48 | break; 49 | } else if (pos == N) { 50 | return 1; 51 | } else if (A[pos]) { 52 | reachable[pos] = 1; 53 | leavesToLookAt.push(pos); 54 | } 55 | } 56 | 57 | // Look at humps from other leaves we have reached 58 | int nextPos; 59 | int minJumps = -1; 60 | 61 | while(leavesToLookAt.size()) { 62 | pos = leavesToLookAt.front(); 63 | leavesToLookAt.pop(); 64 | 65 | for (int i = 2; i < fibNums.size(); ++i) { 66 | nextPos = pos + fibNums[i]; 67 | 68 | if (nextPos > N) { 69 | // can't jump more than N + 1 (destination) 70 | break; 71 | } else if (nextPos == N) { 72 | // Destination 73 | if ((minJumps == -1) || ((reachable[pos] + 1) < minJumps)) { 74 | // Update minJumps if found a path that is cheaper 75 | minJumps = reachable[pos] + 1; 76 | } 77 | } else if (A[nextPos] && !reachable[nextPos]) { 78 | reachable[nextPos] = reachable[pos] + 1; 79 | leavesToLookAt.push(nextPos); 80 | } 81 | } 82 | } 83 | 84 | return minJumps; 85 | } 86 | 87 | -------------------------------------------------------------------------------- /Lesson 13 - Fibonacci numbers/FibFrog/task.txt: -------------------------------------------------------------------------------- 1 | The Fibonacci sequence is defined using the following recursive formula: 2 | 3 | F(0) = 0 4 | F(1) = 1 5 | F(M) = F(M - 1) + F(M - 2) if M >= 2 6 | A small frog wants to get to the other side of a river. The frog is initially located at one bank of the river (position −1) and wants to get to the other bank (position N). The frog can jump over any distance F(K), where F(K) is the K-th Fibonacci number. Luckily, there are many leaves on the river, and the frog can jump between the leaves, but only in the direction of the bank at position N. 7 | 8 | The leaves on the river are represented in a zero-indexed array A consisting of N integers. Consecutive elements of array A represent consecutive positions from 0 to N − 1 on the river. Array A contains only 0s and/or 1s: 9 | 10 | 0 represents a position without a leaf; 11 | 1 represents a position containing a leaf. 12 | The goal is to count the minimum number of jumps in which the frog can get to the other side of the river (from position −1 to position N). The frog can jump between positions −1 and N (the banks of the river) and every position containing a leaf. 13 | 14 | For example, consider array A such that: 15 | 16 | A[0] = 0 17 | A[1] = 0 18 | A[2] = 0 19 | A[3] = 1 20 | A[4] = 1 21 | A[5] = 0 22 | A[6] = 1 23 | A[7] = 0 24 | A[8] = 0 25 | A[9] = 0 26 | A[10] = 0 27 | The frog can make three jumps of length F(5) = 5, F(3) = 2 and F(5) = 5. 28 | 29 | Write a function: 30 | 31 | int solution(int A[], int N); 32 | 33 | that, given a zero-indexed array A consisting of N integers, returns the minimum number of jumps by which the frog can get to the other side of the river. If the frog cannot reach the other side of the river, the function should return −1. 34 | 35 | For example, given: 36 | 37 | A[0] = 0 38 | A[1] = 0 39 | A[2] = 0 40 | A[3] = 1 41 | A[4] = 1 42 | A[5] = 0 43 | A[6] = 1 44 | A[7] = 0 45 | A[8] = 0 46 | A[9] = 0 47 | A[10] = 0 48 | the function should return 3, as explained above. 49 | 50 | Assume that: 51 | 52 | N is an integer within the range [0..100,000]; 53 | each element of array A is an integer that can have one of the following values: 0, 1. 54 | Complexity: 55 | 56 | expected worst-case time complexity is O(N*log(N)); 57 | expected worst-case space complexity is O(N), beyond input storage (not counting the storage required for input arguments). 58 | Elements of input arrays can be modified. 59 | -------------------------------------------------------------------------------- /Lesson 13 - Fibonacci numbers/Fibonacci.pdf: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/ghananigans/codility-lesson-solutions/e7b7115f1a1229733cf67236a24c86ad7bc4c0ea/Lesson 13 - Fibonacci numbers/Fibonacci.pdf -------------------------------------------------------------------------------- /Lesson 13 - Fibonacci numbers/Ladder/solution.cpp: -------------------------------------------------------------------------------- 1 | // Code written in C++ 2 | // Correctness: 100 % 3 | // Performance: 100 % 4 | // Time Complexity: O(L) 5 | // Space Complexity: O(L) 6 | 7 | #include 8 | #include 9 | #include 10 | 11 | vector solution(vector &A, vector &B) { 12 | const int L = A.size(); 13 | 14 | // index a has # of ways to get to rung a + 1 (fibonacci sequence) 15 | std::vector step; 16 | step.push_back(1); // only 1 way to get to first step 17 | step.push_back(2); // two ways to get to second step 18 | 19 | std::vector answer; 20 | 21 | for (int i = 0; i < L; ++i) { 22 | // Fill missing step counts 23 | for (int a = step.size(); a < A[i]; ++a) { 24 | step.push_back(step[a - 1] + step[a - 2]); 25 | } 26 | 27 | answer.push_back(step[A[i] - 1] & ((1 << B[i]) - 1)); 28 | } 29 | 30 | return answer; 31 | } 32 | -------------------------------------------------------------------------------- /Lesson 13 - Fibonacci numbers/Ladder/task.txt: -------------------------------------------------------------------------------- 1 | You have to climb up a ladder. The ladder has exactly N rungs, numbered from 1 to N. With each step, you can ascend by one or two rungs. More precisely: 2 | 3 | with your first step you can stand on rung 1 or 2, 4 | if you are on rung K, you can move to rungs K + 1 or K + 2, 5 | finally you have to stand on rung N. 6 | Your task is to count the number of different ways of climbing to the top of the ladder. 7 | 8 | For example, given N = 4, you have five different ways of climbing, ascending by: 9 | 10 | 1, 1, 1 and 1 rung, 11 | 1, 1 and 2 rungs, 12 | 1, 2 and 1 rung, 13 | 2, 1 and 1 rungs, and 14 | 2 and 2 rungs. 15 | Given N = 5, you have eight different ways of climbing, ascending by: 16 | 17 | 1, 1, 1, 1 and 1 rung, 18 | 1, 1, 1 and 2 rungs, 19 | 1, 1, 2 and 1 rung, 20 | 1, 2, 1 and 1 rung, 21 | 1, 2 and 2 rungs, 22 | 2, 1, 1 and 1 rungs, 23 | 2, 1 and 2 rungs, and 24 | 2, 2 and 1 rung. 25 | The number of different ways can be very large, so it is sufficient to return the result modulo 2P, for a given integer P. 26 | 27 | Assume that the following declarations are given: 28 | 29 | struct Results { 30 | int * C; 31 | int L; 32 | }; 33 | 34 | Write a function: 35 | 36 | struct Results solution(int A[], int B[], int L); 37 | 38 | that, given two non-empty zero-indexed arrays A and B of L integers, returns an array consisting of L integers specifying the consecutive answers; position I should contain the number of different ways of climbing the ladder with A[I] rungs modulo 2B[I]. 39 | 40 | For example, given L = 5 and: 41 | 42 | A[0] = 4 B[0] = 3 43 | A[1] = 4 B[1] = 2 44 | A[2] = 5 B[2] = 4 45 | A[3] = 5 B[3] = 3 46 | A[4] = 1 B[4] = 1 47 | the function should return the sequence [5, 1, 8, 0, 1], as explained above. 48 | 49 | Assume that: 50 | 51 | L is an integer within the range [1..30,000]; 52 | each element of array A is an integer within the range [1..L]; 53 | each element of array B is an integer within the range [1..30]. 54 | Complexity: 55 | 56 | expected worst-case time complexity is O(L); 57 | expected worst-case space complexity is O(L), beyond input storage (not counting the storage required for input arguments). 58 | Elements of input arrays can be modified. 59 | 60 | -------------------------------------------------------------------------------- /Lesson 14 - Binary search algorithm/BinarySearch.pdf: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/ghananigans/codility-lesson-solutions/e7b7115f1a1229733cf67236a24c86ad7bc4c0ea/Lesson 14 - Binary search algorithm/BinarySearch.pdf -------------------------------------------------------------------------------- /Lesson 14 - Binary search algorithm/MinMaxDivision/solution.c: -------------------------------------------------------------------------------- 1 | // Code written in C 2 | // Correctness: 100 % 3 | // Performance: 100 % 4 | // Time Complexity: O(N*log(N+M)) 5 | // Space Complexity: O(1) 6 | 7 | int check(int A[], int N, int K, int mid) { 8 | int sum = 0; 9 | int blocks = 0; 10 | 11 | for (int i = 0; i < N; ++i) { 12 | if ((sum + A[i]) > mid) { 13 | sum = A[i]; 14 | 15 | if (++blocks >= K) { 16 | return 0; 17 | } 18 | 19 | } else { 20 | sum += A[i]; 21 | } 22 | } 23 | 24 | return 1; 25 | } 26 | 27 | int solution(int K, int M, int A[], int N) { 28 | int min_sum = 0; 29 | int max_sum = 0; 30 | 31 | // Calculate sum of total array (max_sum) 32 | // and max value in array (min_sum) 33 | for (int i = 0; i < N; ++i) { 34 | max_sum += A[i]; 35 | 36 | if (A[i] > min_sum) { 37 | min_sum = A[i]; 38 | } 39 | } 40 | 41 | if (K == 1) { 42 | // Split only into 1 block so 43 | // return total sum 44 | return max_sum; 45 | } else if (K == N) { 46 | // Split into N many blocks 47 | // so return max value 48 | return min_sum; 49 | } 50 | 51 | int mid; 52 | 53 | while (min_sum <= max_sum) { 54 | mid = (min_sum + max_sum) / 2; 55 | 56 | if (check(A, N, K, mid)) { 57 | max_sum = mid - 1; 58 | } else { 59 | min_sum = mid + 1; 60 | } 61 | } 62 | 63 | return min_sum; 64 | } 65 | -------------------------------------------------------------------------------- /Lesson 14 - Binary search algorithm/MinMaxDivision/task.txt: -------------------------------------------------------------------------------- 1 | You are given integers K, M and a non-empty zero-indexed array A consisting of N integers. Every element of the array is not greater than M. 2 | 3 | You should divide this array into K blocks of consecutive elements. The size of the block is any integer between 0 and N. Every element of the array should belong to some block. 4 | 5 | The sum of the block from X to Y equals A[X] + A[X + 1] + ... + A[Y]. The sum of empty block equals 0. 6 | 7 | The large sum is the maximal sum of any block. 8 | 9 | For example, you are given integers K = 3, M = 5 and array A such that: 10 | 11 | A[0] = 2 12 | A[1] = 1 13 | A[2] = 5 14 | A[3] = 1 15 | A[4] = 2 16 | A[5] = 2 17 | A[6] = 2 18 | The array can be divided, for example, into the following blocks: 19 | 20 | [2, 1, 5, 1, 2, 2, 2], [], [] with a large sum of 15; 21 | [2], [1, 5, 1, 2], [2, 2] with a large sum of 9; 22 | [2, 1, 5], [], [1, 2, 2, 2] with a large sum of 8; 23 | [2, 1], [5, 1], [2, 2, 2] with a large sum of 6. 24 | The goal is to minimize the large sum. In the above example, 6 is the minimal large sum. 25 | 26 | Write a function: 27 | 28 | int solution(int K, int M, vector &A); 29 | 30 | that, given integers K, M and a non-empty zero-indexed array A consisting of N integers, returns the minimal large sum. 31 | 32 | For example, given K = 3, M = 5 and array A such that: 33 | 34 | A[0] = 2 35 | A[1] = 1 36 | A[2] = 5 37 | A[3] = 1 38 | A[4] = 2 39 | A[5] = 2 40 | A[6] = 2 41 | the function should return 6, as explained above. 42 | 43 | Assume that: 44 | 45 | N and K are integers within the range [1..100,000]; 46 | M is an integer within the range [0..10,000]; 47 | each element of array A is an integer within the range [0..M]. 48 | Complexity: 49 | 50 | expected worst-case time complexity is O(N*log(N+M)); 51 | expected worst-case space complexity is O(1), beyond input storage (not counting the storage required for input arguments). 52 | Elements of input arrays can be modified. 53 | 54 | -------------------------------------------------------------------------------- /Lesson 14 - Binary search algorithm/NailingPlanks/solution.c: -------------------------------------------------------------------------------- 1 | // Code written in C 2 | // Correctness: 100 % 3 | // Performance: 100 % 4 | // Time Complexity: O((N+M)*log(M)) 5 | // Space Complexity: O(M) 6 | 7 | int solution(int A[], int B[], int N, int C[], int M) { 8 | int min_nails = 1; 9 | int max_nails = M; 10 | int mid; 11 | int nails = -1; 12 | 13 | // Possible nail position is 2 * M 14 | int nailedCount = 2 * M + 1; 15 | int nailed[2 * M + 1]; 16 | 17 | while (min_nails <= max_nails) { 18 | for (int i = 0; i < nailedCount; ++i) { 19 | nailed[i] = 0; 20 | } 21 | 22 | mid = (min_nails + max_nails) / 2; 23 | 24 | for (int i = 0; i < mid; ++i) { 25 | nailed[C[i]]++; 26 | } 27 | 28 | for (int i = 0; i < nailedCount; ++i) { 29 | nailed[i + 1] += nailed[i]; 30 | } 31 | 32 | int missing = 0; 33 | for (int i = 0; i < N; ++i) { 34 | if (nailed[A[i] - 1] == nailed[B[i]]) { 35 | // No nail exists for board i 36 | missing = 1; 37 | break; 38 | } 39 | } 40 | 41 | if (missing) { 42 | min_nails = mid + 1; 43 | } else { 44 | max_nails = mid - 1; 45 | nails = mid; 46 | } 47 | } 48 | 49 | return nails; 50 | } 51 | -------------------------------------------------------------------------------- /Lesson 14 - Binary search algorithm/NailingPlanks/task.txt: -------------------------------------------------------------------------------- 1 | You are given two non-empty zero-indexed arrays A and B consisting of N integers. These arrays represent N planks. More precisely, A[K] is the start and B[K] the end of the K−th plank. 2 | 3 | Next, you are given a non-empty zero-indexed array C consisting of M integers. This array represents M nails. More precisely, C[I] is the position where you can hammer in the I−th nail. 4 | 5 | We say that a plank (A[K], B[K]) is nailed if there exists a nail C[I] such that A[K] ≤ C[I] ≤ B[K]. 6 | 7 | The goal is to find the minimum number of nails that must be used until all the planks are nailed. In other words, you should find a value J such that all planks will be nailed after using only the first J nails. More precisely, for every plank (A[K], B[K]) such that 0 ≤ K < N, there should exist a nail C[I] such that I < J and A[K] ≤ C[I] ≤ B[K]. 8 | 9 | For example, given arrays A, B such that: 10 | 11 | A[0] = 1 B[0] = 4 12 | A[1] = 4 B[1] = 5 13 | A[2] = 5 B[2] = 9 14 | A[3] = 8 B[3] = 10 15 | four planks are represented: [1, 4], [4, 5], [5, 9] and [8, 10]. 16 | 17 | Given array C such that: 18 | 19 | C[0] = 4 20 | C[1] = 6 21 | C[2] = 7 22 | C[3] = 10 23 | C[4] = 2 24 | if we use the following nails: 25 | 26 | 0, then planks [1, 4] and [4, 5] will both be nailed. 27 | 0, 1, then planks [1, 4], [4, 5] and [5, 9] will be nailed. 28 | 0, 1, 2, then planks [1, 4], [4, 5] and [5, 9] will be nailed. 29 | 0, 1, 2, 3, then all the planks will be nailed. 30 | Thus, four is the minimum number of nails that, used sequentially, allow all the planks to be nailed. 31 | 32 | Write a function: 33 | 34 | int solution(int A[], int B[], int N, int C[], int M); 35 | 36 | that, given two non-empty zero-indexed arrays A and B consisting of N integers and a non-empty zero-indexed array C consisting of M integers, returns the minimum number of nails that, used sequentially, allow all the planks to be nailed. 37 | 38 | If it is not possible to nail all the planks, the function should return −1. 39 | 40 | For example, given arrays A, B, C such that: 41 | 42 | A[0] = 1 B[0] = 4 43 | A[1] = 4 B[1] = 5 44 | A[2] = 5 B[2] = 9 45 | A[3] = 8 B[3] = 10 46 | 47 | C[0] = 4 48 | C[1] = 6 49 | C[2] = 7 50 | C[3] = 10 51 | C[4] = 2 52 | the function should return 4, as explained above. 53 | 54 | Assume that: 55 | 56 | N and M are integers within the range [1..30,000]; 57 | each element of arrays A, B, C is an integer within the range [1..2*M]; 58 | A[K] ≤ B[K]. 59 | Complexity: 60 | 61 | expected worst-case time complexity is O((N+M)*log(M)); 62 | expected worst-case space complexity is O(M), beyond input storage (not counting the storage required for input arguments). 63 | Elements of input arrays can be modified. 64 | 65 | -------------------------------------------------------------------------------- /Lesson 15 - Caterpillar method/AbsDistinct/solution.py: -------------------------------------------------------------------------------- 1 | # Code written in Python 2 | # Correctness: 100 % 3 | # Performance: 100 % 4 | # Time Complexity: O(N) 5 | # Space Complexity: O(N) 6 | 7 | def solution(A): 8 | check = {} 9 | count = 0 10 | 11 | for n in A: 12 | key = abs(n) 13 | if key not in check: 14 | count += 1 15 | check[key] = True 16 | 17 | return count 18 | -------------------------------------------------------------------------------- /Lesson 15 - Caterpillar method/AbsDistinct/task.txt: -------------------------------------------------------------------------------- 1 | A non-empty zero-indexed array A consisting of N numbers is given. The array is sorted in non-decreasing order. The absolute distinct count of this array is the number of distinct absolute values among the elements of the array. 2 | 3 | For example, consider array A such that: 4 | 5 | A[0] = -5 6 | A[1] = -3 7 | A[2] = -1 8 | A[3] = 0 9 | A[4] = 3 10 | A[5] = 6 11 | The absolute distinct count of this array is 5, because there are 5 distinct absolute values among the elements of this array, namely 0, 1, 3, 5 and 6. 12 | 13 | Write a function: 14 | 15 | def solution(A) 16 | 17 | that, given a non-empty zero-indexed array A consisting of N numbers, returns absolute distinct count of array A. 18 | 19 | For example, given array A such that: 20 | 21 | A[0] = -5 22 | A[1] = -3 23 | A[2] = -1 24 | A[3] = 0 25 | A[4] = 3 26 | A[5] = 6 27 | the function should return 5, as explained above. 28 | 29 | Assume that: 30 | 31 | N is an integer within the range [1..100,000]; 32 | each element of array A is an integer within the range [−2,147,483,648..2,147,483,647]; 33 | array A is sorted in non-decreasing order. 34 | Complexity: 35 | 36 | expected worst-case time complexity is O(N); 37 | expected worst-case space complexity is O(N), beyond input storage (not counting the storage required for input arguments). 38 | Elements of input arrays can be modified. 39 | -------------------------------------------------------------------------------- /Lesson 15 - Caterpillar method/CaterpillarMethod.pdf: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/ghananigans/codility-lesson-solutions/e7b7115f1a1229733cf67236a24c86ad7bc4c0ea/Lesson 15 - Caterpillar method/CaterpillarMethod.pdf -------------------------------------------------------------------------------- /Lesson 15 - Caterpillar method/CountDistinctSlices/solution.py: -------------------------------------------------------------------------------- 1 | # Code written in Python 2 | # Correctness: 100 % 3 | # Performance: 100 % 4 | # Time Complexity: O(N) 5 | # Space Complexity: O(M) 6 | 7 | MAX_SLICES = 1000000000 8 | 9 | def solution(M, A): 10 | N = len(A) 11 | a = 0 12 | b = 0 13 | count = 0 14 | countb = 0 15 | check = {} 16 | 17 | while a < N: 18 | while b < N and A[b] not in check: 19 | countb += 1 20 | check[A[b]] = True 21 | b += 1 22 | 23 | check.pop(A[a], None) 24 | 25 | a += 1 26 | count += countb 27 | countb -= 1 28 | 29 | if count >= MAX_SLICES: 30 | return MAX_SLICES 31 | 32 | return count 33 | -------------------------------------------------------------------------------- /Lesson 15 - Caterpillar method/CountDistinctSlices/task.txt: -------------------------------------------------------------------------------- 1 | An integer M and a non-empty zero-indexed array A consisting of N non-negative integers are given. All integers in array A are less than or equal to M. 2 | 3 | A pair of integers (P, Q), such that 0 ≤ P ≤ Q < N, is called a slice of array A. The slice consists of the elements A[P], A[P + 1], ..., A[Q]. A distinct slice is a slice consisting of only unique numbers. That is, no individual number occurs more than once in the slice. 4 | 5 | For example, consider integer M = 6 and array A such that: 6 | 7 | A[0] = 3 8 | A[1] = 4 9 | A[2] = 5 10 | A[3] = 5 11 | A[4] = 2 12 | There are exactly nine distinct slices: (0, 0), (0, 1), (0, 2), (1, 1), (1, 2), (2, 2), (3, 3), (3, 4) and (4, 4). 13 | 14 | The goal is to calculate the number of distinct slices. 15 | 16 | Write a function: 17 | 18 | def solution(M, A) 19 | 20 | that, given an integer M and a non-empty zero-indexed array A consisting of N integers, returns the number of distinct slices. 21 | 22 | If the number of distinct slices is greater than 1,000,000,000, the function should return 1,000,000,000. 23 | 24 | For example, given integer M = 6 and array A such that: 25 | 26 | A[0] = 3 27 | A[1] = 4 28 | A[2] = 5 29 | A[3] = 5 30 | A[4] = 2 31 | the function should return 9, as explained above. 32 | 33 | Assume that: 34 | 35 | N is an integer within the range [1..100,000]; 36 | M is an integer within the range [0..100,000]; 37 | each element of array A is an integer within the range [0..M]. 38 | Complexity: 39 | 40 | expected worst-case time complexity is O(N); 41 | expected worst-case space complexity is O(M), beyond input storage (not counting the storage required for input arguments). 42 | Elements of input arrays can be modified. 43 | -------------------------------------------------------------------------------- /Lesson 15 - Caterpillar method/CountTriangles/solution.py: -------------------------------------------------------------------------------- 1 | # Code written in Python 2 | # Correctness: 100 % 3 | # Performance: 100 % 4 | # Time Complexity: O(N^2) 5 | # Space Complexity: O(N) 6 | 7 | def solution(A): 8 | N = len(A) 9 | 10 | if N < 3: 11 | # Less than 3 sides so no triangles can be made 12 | return 0 13 | 14 | A.sort() 15 | num_triangles = 0 16 | 17 | for left in xrange(0, N - 2): 18 | right = left + 2 19 | 20 | for mid in xrange(left + 1, N - 1): 21 | while right < N and A[left] + A[mid] > A[right]: 22 | # Number of mid sides between mid and right is 23 | # valid since array is sorted. 24 | num_triangles += right - mid 25 | right += 1 26 | 27 | return num_triangles 28 | -------------------------------------------------------------------------------- /Lesson 15 - Caterpillar method/CountTriangles/task.txt: -------------------------------------------------------------------------------- 1 | A zero-indexed array A consisting of N integers is given. A triplet (P, Q, R) is triangular if it is possible to build a triangle with sides of lengths A[P], A[Q] and A[R]. In other words, triplet (P, Q, R) is triangular if 0 ≤ P < Q < R < N and: 2 | 3 | A[P] + A[Q] > A[R], 4 | A[Q] + A[R] > A[P], 5 | A[R] + A[P] > A[Q]. 6 | For example, consider array A such that: 7 | 8 | A[0] = 10 A[1] = 2 A[2] = 5 9 | A[3] = 1 A[4] = 8 A[5] = 12 10 | There are four triangular triplets that can be constructed from elements of this array, namely (0, 2, 4), (0, 2, 5), (0, 4, 5), and (2, 4, 5). 11 | 12 | Write a function: 13 | 14 | def solution(A) 15 | 16 | that, given a zero-indexed array A consisting of N integers, returns the number of triangular triplets in this array. 17 | 18 | For example, given array A such that: 19 | 20 | A[0] = 10 A[1] = 2 A[2] = 5 21 | A[3] = 1 A[4] = 8 A[5] = 12 22 | the function should return 4, as explained above. 23 | 24 | Assume that: 25 | 26 | N is an integer within the range [0..1,000]; 27 | each element of array A is an integer within the range [1..1,000,000,000]. 28 | Complexity: 29 | 30 | expected worst-case time complexity is O(N2); 31 | expected worst-case space complexity is O(N), beyond input storage (not counting the storage required for input arguments). 32 | Elements of input arrays can be modified. 33 | -------------------------------------------------------------------------------- /Lesson 15 - Caterpillar method/MinAbsSumOfTwo/solution.py: -------------------------------------------------------------------------------- 1 | # Code written in Python 2 | # Correctness: 100 % 3 | # Performance: 100 % 4 | # Time Complexity: O(N*log(N)) 5 | # Space Complexity: O(N) 6 | 7 | def solution(A): 8 | N = len(A) 9 | 10 | if N == 1: 11 | return abs(A[0] * 2) 12 | 13 | A.sort() 14 | 15 | front = 0 16 | back = N - 1 17 | min_sum = abs(A[front] + A[back]) 18 | 19 | while front < back: 20 | new_front = front + 1 21 | new_back = back - 1 22 | 23 | # New abs sum when front ptr is shifted to the right 24 | new_val_front = abs(A[new_front] + A[back]) 25 | 26 | # New abs sum when back ptr is shifted to the left 27 | new_val_back = abs(A[front] + A[new_back]) 28 | 29 | if new_val_front < new_val_back: 30 | # Shifting front ptr is better 31 | min_sum = min(new_val_front, min_sum) 32 | front = new_front 33 | else: 34 | # Shifting back ptr is better 35 | min_sum = min(new_val_back, min_sum) 36 | back = new_back 37 | 38 | return min_sum 39 | -------------------------------------------------------------------------------- /Lesson 15 - Caterpillar method/MinAbsSumOfTwo/task.txt: -------------------------------------------------------------------------------- 1 | Let A be a non-empty zero-indexed array consisting of N integers. 2 | 3 | The abs sum of two for a pair of indices (P, Q) is the absolute value |A[P] + A[Q]|, for 0 ≤ P ≤ Q < N. 4 | 5 | For example, the following array A: 6 | 7 | A[0] = 1 8 | A[1] = 4 9 | A[2] = -3 10 | has pairs of indices (0, 0), (0, 1), (0, 2), (1, 1), (1, 2), (2, 2). 11 | The abs sum of two for the pair (0, 0) is A[0] + A[0] = |1 + 1| = 2. 12 | The abs sum of two for the pair (0, 1) is A[0] + A[1] = |1 + 4| = 5. 13 | The abs sum of two for the pair (0, 2) is A[0] + A[2] = |1 + (−3)| = 2. 14 | The abs sum of two for the pair (1, 1) is A[1] + A[1] = |4 + 4| = 8. 15 | The abs sum of two for the pair (1, 2) is A[1] + A[2] = |4 + (−3)| = 1. 16 | The abs sum of two for the pair (2, 2) is A[2] + A[2] = |(−3) + (−3)| = 6. 17 | Write a function: 18 | 19 | def solution(A) 20 | 21 | that, given a non-empty zero-indexed array A consisting of N integers, returns the minimal abs sum of two for any pair of indices in this array. 22 | 23 | For example, given the following array A: 24 | 25 | A[0] = 1 26 | A[1] = 4 27 | A[2] = -3 28 | the function should return 1, as explained above. 29 | 30 | Given array A: 31 | 32 | A[0] = -8 33 | A[1] = 4 34 | A[2] = 5 35 | A[3] =-10 36 | A[4] = 3 37 | the function should return |(−8) + 5| = 3. 38 | 39 | Assume that: 40 | 41 | N is an integer within the range [1..100,000]; 42 | each element of array A is an integer within the range [−1,000,000,000..1,000,000,000]. 43 | Complexity: 44 | 45 | expected worst-case time complexity is O(N*log(N)); 46 | expected worst-case space complexity is O(N), beyond input storage (not counting the storage required for input arguments). 47 | Elements of input arrays can be modified. 48 | -------------------------------------------------------------------------------- /Lesson 16 - Greedy algorithms/GreedyAlgorithms.pdf: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/ghananigans/codility-lesson-solutions/e7b7115f1a1229733cf67236a24c86ad7bc4c0ea/Lesson 16 - Greedy algorithms/GreedyAlgorithms.pdf -------------------------------------------------------------------------------- /Lesson 16 - Greedy algorithms/TieRopes/solution.py: -------------------------------------------------------------------------------- 1 | # Code written in Python 2 | # Correctness: 100 % 3 | # Performance: 100 % 4 | # Time Complexity: O(N) 5 | # Space Complexity: O(1) 6 | 7 | def solution(K, A): 8 | N = len(A) 9 | ropes = 0 10 | 11 | if N == 1: 12 | return 1 if A[0] >= K else 0 13 | 14 | left = 0 15 | right = 0 16 | temp_sum = 0 17 | 18 | while right < N: 19 | temp_sum += A[right] 20 | 21 | if temp_sum >= K: 22 | ropes += 1 23 | left = right + 1 24 | right = left 25 | temp_sum = 0 26 | else: 27 | right += 1 28 | 29 | return ropes 30 | -------------------------------------------------------------------------------- /Lesson 16 - Greedy algorithms/TieRopes/task.txt: -------------------------------------------------------------------------------- 1 | There are N ropes numbered from 0 to N − 1, whose lengths are given in a zero-indexed array A, lying on the floor in a line. For each I (0 ≤ I < N), the length of rope I on the line is A[I]. 2 | 3 | We say that two ropes I and I + 1 are adjacent. Two adjacent ropes can be tied together with a knot, and the length of the tied rope is the sum of lengths of both ropes. The resulting new rope can then be tied again. 4 | 5 | For a given integer K, the goal is to tie the ropes in such a way that the number of ropes whose length is greater than or equal to K is maximal. 6 | 7 | For example, consider K = 4 and array A such that: 8 | 9 | A[0] = 1 10 | A[1] = 2 11 | A[2] = 3 12 | A[3] = 4 13 | A[4] = 1 14 | A[5] = 1 15 | A[6] = 3 16 | The ropes are shown in the figure below. 17 | 18 | 19 | 20 | We can tie: 21 | 22 | rope 1 with rope 2 to produce a rope of length A[1] + A[2] = 5; 23 | rope 4 with rope 5 with rope 6 to produce a rope of length A[4] + A[5] + A[6] = 5. 24 | After that, there will be three ropes whose lengths are greater than or equal to K = 4. It is not possible to produce four such ropes. 25 | 26 | Write a function: 27 | 28 | def solution(K, A) 29 | 30 | that, given an integer K and a non-empty zero-indexed array A of N integers, returns the maximum number of ropes of length greater than or equal to K that can be created. 31 | 32 | For example, given K = 4 and array A such that: 33 | 34 | A[0] = 1 35 | A[1] = 2 36 | A[2] = 3 37 | A[3] = 4 38 | A[4] = 1 39 | A[5] = 1 40 | A[6] = 3 41 | the function should return 3, as explained above. 42 | 43 | Assume that: 44 | 45 | N is an integer within the range [1..100,000]; 46 | K is an integer within the range [1..1,000,000,000]; 47 | each element of array A is an integer within the range [1..1,000,000,000]. 48 | Complexity: 49 | 50 | expected worst-case time complexity is O(N); 51 | expected worst-case space complexity is O(N), beyond input storage (not counting the storage required for input arguments). 52 | Elements of input arrays can be modified. 53 | 54 | Copyright 2009–2016 by Codility Limited. All Rights Reserved. Unauthorized copying, publication or disclosure prohibited. 55 | -------------------------------------------------------------------------------- /README.md: -------------------------------------------------------------------------------- 1 | My solutions to Codility's online lessons' tasks 2 | ============================================== 3 | 4 | All tasks and solutions will be sorted into the appropriate lessons, exactly like how codility organizes them. 5 | 6 | Link to Lessons and tasks: https://codility.com/programmers/lessons/ 7 | 8 | 9 | Lesson 1: Iterations 10 | -------------------- 11 | BinaryGap 12 | * Find longest sequence of zeros in binary representation of an integer. 13 | 14 | 15 | Lesson 2: Arrays 16 | ---------------- 17 | CyclicRotation 18 | * Rotate an array to the right by a given number of steps. 19 | 20 | OddOccurrencesInArray 21 | * Find value that occurs in odd number of elements. 22 | 23 | Lesson 3: Time Complexity 24 | ------------------------- 25 | TapeEquilibrium 26 | * Minimize the value |(A[0] + ... + A[P-1]) - (A[P] + ... + A[N-1])|. 27 | 28 | FrogJmp 29 | * Count minimal number of jumps from position X to Y. 30 | 31 | PermMissingElem 32 | * Find the missing element in a given permutation. 33 | 34 | 35 | Lesson 4: Counting Elements 36 | --------------------------- 37 | FrogRiverOne 38 | * Find the earliest time when a frog can jump to the other side of a river. 39 | 40 | PermCheck 41 | * Check whether array A is a permutation. 42 | 43 | MissingInteger 44 | * Find the minimal positive integer not occuring in a given sequence. 45 | 46 | MaxCounters 47 | * Calculate the values of counters after applying all alternating operations: increase counter by 1; set value of all counters to current maximum. 48 | 49 | 50 | Lesson 5: Prefix Sums 51 | -------------------- 52 | CountDiv 53 | * Compute number of integers divisible by k in range [a..b]. 54 | 55 | PassingCars 56 | * Count the number of passing cars on the road. 57 | 58 | MinAvgTwoSlice 59 | * Find the minimal average of any slice containing at least two elements. 60 | 61 | GenomicRangeQuery 62 | * Find the minimal nucleotide from a range of sequence DNA. 63 | 64 | 65 | Lesson 6: Sorting 66 | ----------------- 67 | MaxProductOfThree 68 | * Maximize A[P]\*A[Q]\*A[R] for any triplet (P, Q, R). 69 | 70 | Triangle 71 | * Determine whether a triangle can be built from a given set of edges. 72 | 73 | Distinct 74 | * Compute number of distinct values in an array. 75 | 76 | NumberOfDiscIntersections 77 | * Compute the number of intersections in a sequence of discs. 78 | 79 | 80 | Lesson 7: Stacks and Queues 81 | --------------------------- 82 | Nesting 83 | * Determine whether given string of parentheses is properly nested. 84 | 85 | StoneWall 86 | * Cover "Manhattan skyline" using the minimum number of rectangles. 87 | 88 | Brackets 89 | * Determine whether given string of parentheses is properly nested. 90 | 91 | Fish 92 | * N voracious fish are moving along a river. Calculate how many fish are still alive. 93 | 94 | 95 | Lesson 8: Leader 96 | ---------------- 97 | Dominator 98 | * Find an index of an array such that its value occurs at more than half of indices in the array. 99 | 100 | EquiLeader 101 | * Find the index S such that the leaders of the sequences A[0], A[1], ..., A[S] and A[S + 1], A[S + 2], ..., A[N - 1] are the same. 102 | 103 | 104 | Lesson 9: Maximum Slice Problem 105 | ------------------------------- 106 | MaxDoubleSliceSum 107 | * Find the maximal sum of any double slice. 108 | 109 | MaxProfit 110 | * Given a log of stock prices compute the maximum possible earning. 111 | 112 | MaxSliceSum 113 | * Find a maximum sum of a compact subsequence of array elements. 114 | 115 | 116 | Lesson 10: Prime and Composite Numbers 117 | ------------------------------------- 118 | MinPerimeterRectangle 119 | * Find the minimal perimeter of any rectangle whose area equals N. 120 | 121 | CountFactors 122 | * Count factors of a give number n. 123 | 124 | Peaks 125 | * Divide an array into the maximum number of same-sized blocks, each of which should contain an index P such that A[P - 1] < A[P] > A[P + 1]. 126 | 127 | Flags 128 | * Find the maximum number of flags that can be set on mountain peaks. 129 | 130 | 131 | Lesson 11: Sieve of Eratosthenes 132 | ------------------------------- 133 | CountSemiprimes 134 | * Count the semiprime numbers in the given range [a..b]. 135 | 136 | CountNonDivisible 137 | * Calculate the number of elements of an array that are not divisors of each element. 138 | 139 | 140 | Lesson 12: Euclidean algorithm 141 | ------------------------------ 142 | ChocolatesByNumbers 143 | * There are N chocolates in a circle. Count the number of chocolates you will eat. 144 | 145 | 146 | CommonPrimeDivisors 147 | * Check whether two numbers have the same prime divisors. 148 | --------------------------------------------------------------------------------