Hacker Rank Solutions: Missing Numbers

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
list_1_length = $stdin.gets.to_i

list1 = $stdin.gets.chomp.split(' ')

list_2_length = $stdin.gets.to_i

list2 = $stdin.gets.chomp.split(' ')

list1hash = {}
list2hash = {}

times = list_1_length > list_2_length ? list_1_length : list_2_length

times.times do |i|
  if list1[i] != nil
    n = list1[i]
    if list1hash[n] == nil
      list1hash[n] = 1
    else
      list1hash[n] += 1
    end
  end

  if list2[i] != nil
    n = list2[i]
    if list2hash[n] == nil
      list2hash[n] = 1
    else
      list2hash[n] += 1
    end

  end
end

if list1hash.length > list2hash.length
  biggest = list1hash
  smallest = list2hash
else
  smallest = list1hash
  biggest = list2hash
end

arr = []
biggest.each do |k, v|
  arr << k.to_i if smallest[k] != v
end

puts arr.sort.join(' ')

Hacker Rank Solutions: Game of Thrones

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
def palindrome?(string)
  hash = {}

  string.each_char do |c|
    if hash[c] == nil
      hash[c] = 1
    else
      hash[c] += 1
    end
  end

  odd_chars = 0

  hash.each do |k,v|
    unless v % 2 == 0
      odd_chars += 1
      if odd_chars >= 2
        puts 'NO'
        return
      end
    end
  end

  puts 'YES'
end

string = $stdin.gets.chomp

palindrome?(string)

Hacker Rank Solutions: Angry Children

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
  n = gets.to_i
  k = gets.to_i
  candy = []
  n.times do |i|
    candy[i] = gets.to_i
  end

  candy.sort!

  # sliding widow
  j = k
  i = 0
  lowest_dif = Float::INFINITY

  while j <= candy.length do
    dif = candy[j - 1] - candy[i]
    lowest_dif = dif if dif < lowest_dif

    break if lowest_dif == 0

    j += 1
    i += 1
  end

  puts lowest_dif

Hacker Rank Solutions: Sherlock and The Beast

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
def largest_decent_number(n)
  return -1 if n == 1 || n == 2

  j = 0
  loop do
    break if n - j <= 2

    if (n - j) % 3 == 0
      return "#{'5' * (n - j) }#{'3' *  j }"
    end
    j += 5
  end

  if (n % 5 == 0)
    '3' * n
  else
    -1
  end
end

test_cases = $stdin.gets.to_i

test_cases.times do
  value = $stdin.gets.to_i

  $stdout.puts largest_decent_number(value)
end

Hacker Rank Solutions: Is Fibo?

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
def fibonachi_upto(n)
  return 0 if n == 0
  return 1 if n == 1
  return 1 if n == 2
  prev2 = 1
  prev1 = 1

  a = [0, 1, 1]
  i = 3

  while prev1 <= n do
    tmp = prev1 + prev2
    a << tmp if tmp <= n
    prev2 = prev1
    prev1 = tmp
    i += 1
  end

  return a
end


fibos = fibonachi_upto(10000000000)

test_cases = $stdin.gets.to_i

test_cases.times do
  value = $stdin.gets.to_i
  if fibos.include?(value)
    puts 'IsFibo'
  else
    puts 'IsNotFibo'
  end
end