├── .gitignore ├── LICENSE ├── Lesson 1 ├── BinaryGap.rb └── BinaryGap2.rb ├── Lesson 2 ├── CyclicRotation.rb └── OddOccurrencesInArray.rb ├── Lesson 3 ├── FrogJmp.rb ├── PermMissingElem.rb ├── PermMissingElem2.rb └── TapeEquilibrium.rb ├── Lesson 4 ├── FrogRiverOne.rb ├── MaxCounters.rb ├── MissingInteger.rb └── PermCheck.rb ├── Lesson 5 ├── CountDiv.rb ├── GenomicRangeQuery.rb ├── GenomicRangeQuery2.rb └── PassingCars.rb ├── Problems ├── BinaryGap.md ├── CyclicRotation.md ├── FrogJmp.md ├── FrogRiverOne.md ├── MaxCounters.md ├── MissingInteger.md ├── OddOccurrencesInArray.md ├── PermCheck.md ├── PermMissingElem.md └── TapeEquilibrium.md └── README.md /.gitignore: -------------------------------------------------------------------------------- 1 | *.gem 2 | *.rbc 3 | /.config 4 | /coverage/ 5 | /InstalledFiles 6 | /pkg/ 7 | /spec/reports/ 8 | /spec/examples.txt 9 | /test/tmp/ 10 | /test/version_tmp/ 11 | /tmp/ 12 | 13 | # Used by dotenv library to load environment variables. 14 | # .env 15 | 16 | ## Specific to RubyMotion: 17 | .dat* 18 | .repl_history 19 | build/ 20 | *.bridgesupport 21 | build-iPhoneOS/ 22 | build-iPhoneSimulator/ 23 | 24 | ## Specific to RubyMotion (use of CocoaPods): 25 | # 26 | # We recommend against adding the Pods directory to your .gitignore. However 27 | # you should judge for yourself, the pros and cons are mentioned at: 28 | # https://guides.cocoapods.org/using/using-cocoapods.html#should-i-check-the-pods-directory-into-source-control 29 | # 30 | # vendor/Pods/ 31 | 32 | ## Documentation cache and generated files: 33 | /.yardoc/ 34 | /_yardoc/ 35 | /doc/ 36 | /rdoc/ 37 | 38 | ## Environment normalization: 39 | /.bundle/ 40 | /vendor/bundle 41 | /lib/bundler/man/ 42 | 43 | # for a library or gem, you might want to ignore these files since the code is 44 | # intended to run in multiple environments; otherwise, check them in: 45 | # Gemfile.lock 46 | # .ruby-version 47 | # .ruby-gemset 48 | 49 | # unless supporting rvm < 1.11.0 or doing something fancy, ignore this: 50 | .rvmrc 51 | -------------------------------------------------------------------------------- /LICENSE: -------------------------------------------------------------------------------- 1 | MIT License 2 | 3 | Copyright (c) 2017 Rocela Durazo 4 | 5 | Permission is hereby granted, free of charge, to any person obtaining a copy 6 | of this software and associated documentation files (the "Software"), to deal 7 | in the Software without restriction, including without limitation the rights 8 | to use, copy, modify, merge, publish, distribute, sublicense, and/or sell 9 | copies of the Software, and to permit persons to whom the Software is 10 | furnished to do so, subject to the following conditions: 11 | 12 | The above copyright notice and this permission notice shall be included in all 13 | copies or substantial portions of the Software. 14 | 15 | THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR 16 | IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, 17 | FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE 18 | AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER 19 | LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, 20 | OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE 21 | SOFTWARE. 22 | -------------------------------------------------------------------------------- /Lesson 1/BinaryGap.rb: -------------------------------------------------------------------------------- 1 | # you can write to stdout for debugging purposes, e.g. 2 | # puts "this is a debug message" 3 | 4 | def solution(n) 5 | # write your code in Ruby 2.2 6 | bin = n.to_s(2) 7 | binary_gap = 0 8 | one = bin.index('1') 9 | 10 | for i in 1..(bin.size-1) 11 | current_string = bin[one..i].to_s 12 | #puts "string #{current_string}, count #{current_string.count('0')} " 13 | if bin[i] == '1' 14 | one = i 15 | count_zeros = current_string.count('0') 16 | if binary_gap < count_zeros 17 | binary_gap = count_zeros 18 | end 19 | end 20 | end 21 | return binary_gap 22 | end -------------------------------------------------------------------------------- /Lesson 1/BinaryGap2.rb: -------------------------------------------------------------------------------- 1 | # you can write to stdout for debugging purposes, e.g. 2 | # puts "this is a debug message" 3 | 4 | def solution(n) 5 | # write your code in Ruby 2.2 6 | bin = n.to_s(2) 7 | return 0 unless bin =~ /0/i 8 | max = 0 9 | count = 0 10 | 11 | bin.each_char do |c| 12 | if c == '1' 13 | max = count if count > max 14 | count = 0 15 | else 16 | count = count + 1 17 | end 18 | end 19 | max 20 | end -------------------------------------------------------------------------------- /Lesson 2/CyclicRotation.rb: -------------------------------------------------------------------------------- 1 | # you can write to stdout for debugging purposes, e.g. 2 | # puts "this is a debug message" 3 | 4 | def solution(a, k) 5 | # write your code in Ruby 2.2 6 | 7 | unless a.empty? 8 | for i in 1..k 9 | last = a.pop 10 | a.insert(0, last) 11 | end 12 | end 13 | 14 | return a 15 | end -------------------------------------------------------------------------------- /Lesson 2/OddOccurrencesInArray.rb: -------------------------------------------------------------------------------- 1 | # you can write to stdout for debugging purposes, e.g. 2 | # puts "this is a debug message" 3 | 4 | def solution(a) 5 | # write your code in Ruby 2.2 6 | unique_numbers = a.uniq 7 | 8 | unique_numbers.each do |element| 9 | if a.count(element).odd? 10 | return element 11 | end 12 | end 13 | end -------------------------------------------------------------------------------- /Lesson 3/FrogJmp.rb: -------------------------------------------------------------------------------- 1 | # you can write to stdout for debugging purposes, e.g. 2 | # puts "this is a debug message" 3 | 4 | def solution(x, y, d) 5 | # write your code in Ruby 2.2 6 | if x == y 7 | return 0 8 | else 9 | return (Float(y-x)/d).ceil 10 | end 11 | end -------------------------------------------------------------------------------- /Lesson 3/PermMissingElem.rb: -------------------------------------------------------------------------------- 1 | # you can write to stdout for debugging purposes, e.g. 2 | # puts "this is a debug message" 3 | 4 | def solution(a) 5 | # write your code in Ruby 2.2 6 | numbers = Array(1..(a.size + 1)) 7 | res = numbers - a 8 | res[0] 9 | end -------------------------------------------------------------------------------- /Lesson 3/PermMissingElem2.rb: -------------------------------------------------------------------------------- 1 | # you can write to stdout for debugging purposes, e.g. 2 | # puts "this is a debug message" 3 | 4 | def solution(a) 5 | # write your code in Ruby 2.2 6 | total_sum = (1..(a.count+1)).reduce(:+) 7 | total_arr = a.reduce(:+) || 0 8 | total_sum - total_arr 9 | end -------------------------------------------------------------------------------- /Lesson 3/TapeEquilibrium.rb: -------------------------------------------------------------------------------- 1 | # you can write to stdout for debugging purposes, e.g. 2 | # puts "this is a debug message" 3 | 4 | def solution(a) 5 | # write your code in Ruby 2.2 6 | left = 0 7 | right = a.reduce(:+) 8 | difference = [] 9 | 10 | for i in 1..(a.size-1) do 11 | left += a[i-1] 12 | right -= a[i-1] 13 | absolute = (left - right).abs 14 | difference.push(absolute) 15 | end 16 | #puts "#{difference}" 17 | return difference.min 18 | end 19 | -------------------------------------------------------------------------------- /Lesson 4/FrogRiverOne.rb: -------------------------------------------------------------------------------- 1 | # you can write to stdout for debugging purposes, e.g. 2 | # puts "this is a debug message" 3 | 4 | def solution(x, a) 5 | # write your code in Ruby 2.2 6 | path = {} 7 | 8 | a.each_with_index do |element, i| 9 | path[element] = true 10 | return i if path.size == x 11 | end 12 | -1 13 | end 14 | 15 | require 'minitest/autorun' 16 | class Tests < Minitest::Test 17 | def test_example_input 18 | assert_equal 6, solution(5, [1, 3, 1, 4, 2, 3, 5, 4]) 19 | end 20 | 21 | def test_not_possible 22 | assert_equal(-1, solution(5, [1, 2, 1, 2, 1, 4, 5])) 23 | end 24 | 25 | def test_immediately 26 | assert_equal 0, solution(1, [1, 1]) 27 | end 28 | end 29 | -------------------------------------------------------------------------------- /Lesson 4/MaxCounters.rb: -------------------------------------------------------------------------------- 1 | # you can write to stdout for debugging purposes, e.g. 2 | # puts "this is a debug message" 3 | 4 | def solution(n, a) 5 | # write your code in Ruby 2.2 6 | array = [0] * n 7 | a.each do |element| 8 | index = element - 1 9 | # puts index 10 | if index >= n 11 | array = [array.max] * n 12 | else 13 | array[index] += 1 14 | end 15 | end 16 | # puts array 17 | array 18 | end 19 | -------------------------------------------------------------------------------- /Lesson 4/MissingInteger.rb: -------------------------------------------------------------------------------- 1 | # you can write to stdout for debugging purposes, e.g. 2 | # puts "this is a debug message" 3 | 4 | def solution(a) 5 | # write your code in Ruby 2.2 6 | a = a.uniq.select { |i| i if i > 0 } 7 | return 1 if a.empty? 8 | 9 | b = Array(1..a.size) 10 | res = b - a 11 | 12 | if res.empty? 13 | a.max + 1 14 | else 15 | res.min 16 | end 17 | end 18 | 19 | require 'minitest/autorun' 20 | class Tests < Minitest::Test 21 | def test_negatives 22 | assert_equal 1, solution([-1]) 23 | end 24 | 25 | def test_two_val 26 | assert_equal 2, solution([1]) 27 | end 28 | 29 | def test_one_val 30 | assert_equal 1, solution([2, 3, 4, 2]) 31 | end 32 | 33 | def test_large_2 34 | array = Array(1..100_000) 35 | assert_equal 100_001, solution(array) 36 | end 37 | 38 | def test_positives 39 | assert_equal 5, solution([1, 3, 6, 4, 1, 2]) 40 | end 41 | end 42 | -------------------------------------------------------------------------------- /Lesson 4/PermCheck.rb: -------------------------------------------------------------------------------- 1 | # you can write to stdout for debugging purposes, e.g. 2 | # puts "this is a debug message" 3 | 4 | def solution(a) 5 | # write your code in Ruby 2.2 6 | permutation = Array(1..a.size) 7 | # puts permutation 8 | return 1 if permutation - a == [] 9 | 0 10 | end 11 | -------------------------------------------------------------------------------- /Lesson 5/CountDiv.rb: -------------------------------------------------------------------------------- 1 | # you can write to stdout for debugging purposes, e.g. 2 | # puts "this is a debug message" 3 | 4 | def solution(a, b, k) 5 | # write your code in Ruby 2.2 6 | count = 0 7 | for i in a..b do 8 | count = count + 1 if i%k == 0 9 | end 10 | count 11 | end -------------------------------------------------------------------------------- /Lesson 5/GenomicRangeQuery.rb: -------------------------------------------------------------------------------- 1 | # you can write to stdout for debugging purposes, e.g. 2 | # puts "this is a debug message" 3 | 4 | def solution(s, p, q) 5 | # write your code in Ruby 2.2 6 | a = [] 7 | p.each_with_index do |e, i| 8 | cut = s[p[i]..q[i]] 9 | if cut.index('A') 10 | a.push(1) 11 | elsif cut.index('C') 12 | a.push(2) 13 | elsif cut.index('G') 14 | a.push(3) 15 | else 16 | a.push(4) 17 | end 18 | end 19 | a 20 | end -------------------------------------------------------------------------------- /Lesson 5/GenomicRangeQuery2.rb: -------------------------------------------------------------------------------- 1 | # you can write to stdout for debugging purposes, e.g. 2 | # puts "this is a debug message" 3 | 4 | def solution(s, p, q) 5 | # write your code in Ruby 2.2 6 | hash = { 'A'=> 1, 'C'=> 2, 'G'=> 3, 'T'=> 4 } 7 | array = [] 8 | p.each_with_index do |e, i| 9 | low = 4 10 | cut = s[p[i]..q[i]] 11 | cut.each_char { |c| low = hash[c] if hash[c] < low } 12 | array.push(low) 13 | end 14 | array 15 | end -------------------------------------------------------------------------------- /Lesson 5/PassingCars.rb: -------------------------------------------------------------------------------- 1 | # you can write to stdout for debugging purposes, e.g. 2 | # puts "this is a debug message" 3 | 4 | def solution(a) 5 | # write your code in Ruby 2.2 6 | count = 0 7 | a.each_with_index do |v,i| 8 | return -1 if count > 1000000000 9 | return count if a[i..-1].count(0) == 0 10 | count = count + a[i..-1].count(1) if v == 0 11 | end 12 | count 13 | end 14 | 15 | require 'minitest/autorun' 16 | class Tests < Minitest::Test 17 | def test_example_1 18 | assert_equal 5, solution([0, 1, 0, 1, 1]) 19 | end 20 | 21 | def test_example_2 22 | assert_equal 0, solution([1,0]) 23 | end 24 | 25 | def test_example_3 26 | assert_equal 1, solution([0,1]) 27 | end 28 | end 29 | -------------------------------------------------------------------------------- /Problems/BinaryGap.md: -------------------------------------------------------------------------------- 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 | ```ruby 8 | def solution(n) 9 | ``` 10 | 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. 11 | 12 | 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. 13 | 14 | Assume that: 15 | - N is an integer within the range [1..2,147,483,647]. 16 | 17 | Complexity: 18 | - expected worst-case time complexity is O(log(N)); 19 | - expected worst-case space complexity is O(1). 20 | -------------------------------------------------------------------------------- /Problems/CyclicRotation.md: -------------------------------------------------------------------------------- 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 | ```ruby 7 | def solution(a, 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 | 18 | In your solution, focus on correctness. The performance of your solution will not be the focus of the assessment. 19 | -------------------------------------------------------------------------------- /Problems/FrogJmp.md: -------------------------------------------------------------------------------- 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 | ```ruby 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 | 22 | Assume that: 23 | 24 | - X, Y and D are integers within the range [1..1,000,000,000]; 25 | - X ≤ Y. 26 | 27 | Complexity: 28 | 29 | - expected worst-case time complexity is O(1); 30 | - expected worst-case space complexity is O(1). 31 | -------------------------------------------------------------------------------- /Problems/FrogRiverOne.md: -------------------------------------------------------------------------------- 1 | A small frog wants to get to the other side of a river. The frog is initially located on one bank of the river (position 0) and wants to get to the opposite bank (position X+1). Leaves fall from a tree onto the surface of the river. 2 | 3 | You are given a 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 (that is, we want to find the earliest moment when all the positions from 1 to X are covered by leaves). 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 | In second 6, a leaf falls into position 5. This is the earliest time when leaves appear in every position across the river. 18 | 19 | Write a function: 20 | ```ruby 21 | def solution(x, a) 22 | ``` 23 | 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. 24 | 25 | If the frog is never able to jump to the other side of the river, the function should return −1. 26 | 27 | For example, given X = 5 and array A such that: 28 | 29 | A[0] = 1 30 | A[1] = 3 31 | A[2] = 1 32 | A[3] = 4 33 | A[4] = 2 34 | A[5] = 3 35 | A[6] = 5 36 | A[7] = 4 37 | the function should return 6, as explained above. 38 | 39 | Assume that: 40 | 41 | - N and X are integers within the range [1..100,000]; 42 | - each element of array A is an integer within the range [1..X]. 43 | Complexity: 44 | 45 | - expected worst-case time complexity is O(N); 46 | - expected worst-case space complexity is O(X), beyond input storage (not counting the storage required for input arguments). 47 | Elements of input arrays can be modified. 48 | -------------------------------------------------------------------------------- /Problems/MaxCounters.md: -------------------------------------------------------------------------------- 1 | 2 | Task description 3 | You are given N counters, initially set to 0, and you have two possible operations on them: 4 | 5 | increase(X) − counter X is increased by 1, 6 | max counter − all counters are set to the maximum value of any counter. 7 | A non-empty zero-indexed array A of M integers is given. This array represents consecutive operations: 8 | 9 | if A[K] = X, such that 1 ≤ X ≤ N, then operation K is increase(X), 10 | if A[K] = N + 1 then operation K is max counter. 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 | the values of the counters after each consecutive operation will be: 21 | 22 | (0, 0, 1, 0, 0) 23 | (0, 0, 1, 1, 0) 24 | (0, 0, 1, 2, 0) 25 | (2, 2, 2, 2, 2) 26 | (3, 2, 2, 2, 2) 27 | (3, 2, 2, 3, 2) 28 | (3, 2, 2, 4, 2) 29 | The goal is to calculate the value of every counter after all operations. 30 | 31 | Write a function: 32 | 33 | ```ruby 34 | def solution(n, a) 35 | ``` 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 | For example, given: 46 | 47 | A[0] = 3 48 | A[1] = 4 49 | A[2] = 4 50 | A[3] = 6 51 | A[4] = 1 52 | A[5] = 4 53 | A[6] = 4 54 | the function should return [3, 2, 2, 4, 2], as explained above. 55 | 56 | Assume that: 57 | 58 | - N and M are integers within the range [1..100,000]; 59 | - each element of array A is an integer within the range [1..N + 1]. 60 | Complexity: 61 | 62 | - expected worst-case time complexity is O(N+M); 63 | - expected worst-case space complexity is O(N), beyond input storage (not counting the storage required for input arguments). 64 | Elements of input arrays can be modified. 65 | -------------------------------------------------------------------------------- /Problems/MissingInteger.md: -------------------------------------------------------------------------------- 1 | Write a function: 2 | ```ruby 3 | def solution(a) 4 | ``` 5 | 6 | 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. 7 | 8 | For example, given: 9 | 10 | A[0] = 1 11 | A[1] = 3 12 | A[2] = 6 13 | A[3] = 4 14 | A[4] = 1 15 | A[5] = 2 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 | Elements of input arrays can be modified. 28 | -------------------------------------------------------------------------------- /Problems/OddOccurrencesInArray.md: -------------------------------------------------------------------------------- 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 | 13 | Write a function: 14 | ```ruby 15 | def solution(a) 16 | ``` 17 | that, given an array A consisting of N integers fulfilling the above conditions, returns the value of the unpaired element. 18 | 19 | For example, given array A such that: 20 | 21 | A[0] = 9 A[1] = 3 A[2] = 9 22 | A[3] = 3 A[4] = 9 A[5] = 7 23 | A[6] = 9 24 | the function should return 7, as explained in the example above. 25 | 26 | Assume that: 27 | 28 | - N is an odd integer within the range [1..1,000,000]; 29 | - each element of array A is an integer within the range [1..1,000,000,000]; 30 | - all but one of the values in A occur an even number of times. 31 | 32 | Complexity: 33 | 34 | - expected worst-case time complexity is O(N); 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 | -------------------------------------------------------------------------------- /Problems/PermCheck.md: -------------------------------------------------------------------------------- 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 | is a permutation, but array A such that: 12 | 13 | A[0] = 4 14 | A[1] = 1 15 | A[2] = 3 16 | is not a permutation, because value 2 is missing. 17 | 18 | The goal is to check whether array A is a permutation. 19 | 20 | Write a function: 21 | ```ruby 22 | def solution(a) 23 | ``` 24 | that, given a zero-indexed array A, returns 1 if array A is a permutation and 0 if it is not. 25 | 26 | For example, given array A such that: 27 | 28 | A[0] = 4 29 | A[1] = 1 30 | A[2] = 3 31 | A[3] = 2 32 | the function should return 1. 33 | 34 | Given array A such that: 35 | 36 | A[0] = 4 37 | A[1] = 1 38 | A[2] = 3 39 | the function should return 0. 40 | 41 | Assume that: 42 | 43 | - N is an integer within the range [1..100,000]; 44 | - each element of array A is an integer within the range [1..1,000,000,000]. 45 | 46 | Complexity: 47 | 48 | - expected worst-case time complexity is O(N); 49 | - expected worst-case space complexity is O(N), beyond input storage (not counting the storage required for input arguments). 50 | 51 | Elements of input arrays can be modified. 52 | -------------------------------------------------------------------------------- /Problems/PermMissingElem.md: -------------------------------------------------------------------------------- 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 | ```ruby 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 | 25 | Complexity: 26 | 27 | - expected worst-case time complexity is O(N); 28 | - expected worst-case space complexity is O(1), beyond input storage (not counting the storage required for input arguments). 29 | 30 | Elements of input arrays can be modified. 31 | -------------------------------------------------------------------------------- /Problems/TapeEquilibrium.md: -------------------------------------------------------------------------------- 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 | 23 | 24 | Write a function: 25 | ```ruby 26 | def solution(a) 27 | ``` 28 | that, given a non-empty zero-indexed array A of N integers, returns the minimal difference that can be achieved. 29 | 30 | For example, given: 31 | 32 | A[0] = 3 33 | A[1] = 1 34 | A[2] = 2 35 | A[3] = 4 36 | A[4] = 3 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 [−1,000..1,000]. 43 | Complexity: 44 | 45 | - expected worst-case time complexity is O(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 | -------------------------------------------------------------------------------- /README.md: -------------------------------------------------------------------------------- 1 | # codility-ruby-solution 2 | 3 | My ruby solutions for [Codility](https://codility.com/programmers/) lessons and challenges. 4 | Not all algorithms in this repository solve with 100 percent the problem presented in codility. 5 | --------------------------------------------------------------------------------