Skip to content

Instantly share code, notes, and snippets.

@shivrajbadu
Last active April 3, 2019 11:46
Show Gist options
  • Select an option

  • Save shivrajbadu/f1beb300de786441fb4bd80086772211 to your computer and use it in GitHub Desktop.

Select an option

Save shivrajbadu/f1beb300de786441fb4bd80086772211 to your computer and use it in GitHub Desktop.
C-Tests
class FindLongestBinaryGap
def solution(n)
binary_num = get_binary_number(n)
new_arr = find_position_of_one_and_get_array(binary_num)
res = no_gap(new_arr)
return 0 if res == 0
# get gap from newarr
rev_Arr = new_arr.reverse
get_max_binary_gap(rev_Arr)
end
def get_binary_number(n)
num = n.to_s(2)
arr = num.to_s.split('');
end
def find_position_of_one_and_get_array(arr)
newarr = []
arr.each_with_index do |val, i|
if val == 1.to_s
# find index
newarr.push(i);
end
end
p newarr
end
def no_gap(binary)
return 0 if binary.nil? || binary.size < 2
end
def get_max_binary_gap(rev_Arr)
diff_array = Array.new
for i in 0..(rev_Arr.size-1) do
first = rev_Arr[i].to_i
second = rev_Arr[i+1].to_i
if (first == second)
next
else
diff = (first-second)
diff_array.push(diff)
end
end
diff_array.to_a;
res = diff_array.to_a.max - 1
end
end
res = FindLongestBinaryGap.new.solution(9);
puts res
res1 = FindLongestBinaryGap.new.solution(0);
puts res1
res2 = FindLongestBinaryGap.new.solution(529);
puts res2
res3 = FindLongestBinaryGap.new.solution(20);
puts res3
res4 = FindLongestBinaryGap.new.solution(15);
puts res4
res5 = FindLongestBinaryGap.new.solution(32);
puts res5
A company has employed N developers (numbered from 0 to N−1) and wants to divide them into two teams. The first is a front-end team with F developers. The second is a back-end team with N−F developers. If the K-th developer is assigned to the front-end team then their contribution is A[K], and if they are assigned to the back-end team then their contribution is B[K]. What is the maximum sum of contributions the company can achieve?
Write a function:
def solution(a, b, f)
that, given two arrays A, B (consisting of N integers each) and the integer F, returns the maximum sum of contributions the company can achieve.
Examples:
1. Given A = [4, 2, 1], B = [2, 5, 3] and F = 2, the function should return 10. There should be two front-end developers and one back-end developer. The 0th and 2nd developers should be assigned to the front-end team (with contributions 4 and 1) and the 1st developer should be assigned to the back-end team (with contribution 5).
2. Given A = [7, 1, 4, 4], B = [5, 3, 4, 3] and F = 2, the function should return 18. The 0th and 3rd developers should be assigned to the front-end team and the 1st and 2nd developers should be assigned to the back-end team.
3. Given A = [5, 5, 5], B = [5, 5, 5] and F = 1, the function should return 15. The 0th developer can be assigned to the front-end team and the 1st and 2nd developers can be assigned to the back-end team.
Write an efficient algorithm for the following assumptions:
N is an integer within the range [1..200,000];
arrays A and B have equal lengths;
each element of array A is an integer within the range [0..1,000];
F is an integer within the range [0..N].
#### solution
# you can write to stdout for debugging purposes, e.g.
# puts "this is a debug message"
# you can write to stdout for debugging purposes, e.g.
# puts "this is a debug message"
def solution(a, b, f)
# write your code in Ruby 2.2
size_of_a = a.size
size_of_b = b.size
return 0 if size_of_a != size_of_b
n = a.size
contr_of_a = 0
contr_of_b = 0
total_contribution = 0
case n
when 1
if f == 0
contr_of_b = contr_of_b + b[0]
elsif f==1
contr_of_a = contr_of_a + a[0]
end
total_contribution = contr_of_a + contr_of_b
else
if f==1
first = a[0]
contr_of_a = contr_of_a + first
for i in (1..n-1) do
contr_of_b = contr_of_b + b[i]
end
total_contribution = contr_of_a + contr_of_b
elsif f==2
first = a[0]
last = a[n-1]
contr_of_a = contr_of_a + first + last
for i in (1..n-2) do
contr_of_b = contr_of_b + b[i]
end
total_contribution = contr_of_a + contr_of_b
else
0
end
end
return total_contribution
end
Q:
An 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 moved to the first place. For example, the rotation of array A = [3, 8, 9, 7, 6] is [6, 3, 8, 9, 7] (elements are shifted right by one index and 6 is moved to the first place).
The goal is to rotate array A K times; that is, each element of A will be shifted to the right K times.
Write a function:
def solution(a, k)
that, given an array A consisting of N integers and an integer K, returns the array A rotated K times.
For example, given
A = [3, 8, 9, 7, 6]
K = 3
the function should return [9, 7, 6, 3, 8]. Three rotations were made:
[3, 8, 9, 7, 6] -> [6, 3, 8, 9, 7]
[6, 3, 8, 9, 7] -> [7, 6, 3, 8, 9]
[7, 6, 3, 8, 9] -> [9, 7, 6, 3, 8]
For another example, given
A = [0, 0, 0]
K = 1
the function should return [0, 0, 0]
Given
A = [1, 2, 3, 4]
K = 4
the function should return [1, 2, 3, 4]
Assume that:
N and K are integers within the range [0..100];
each element of array A is an integer within the range [−1,000..1,000].
Solution:
# you can write to stdout for debugging purposes, e.g.
# puts "this is a debug message"
def solution(a, k)
return a if a.size == 0
for i in 1..k do
p_arr = a.pop();
a.unshift(p_arr);
i = i+1;
end
return a
end
A non-empty 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.
For example, in array A such that:
A[0] = 9 A[1] = 3 A[2] = 9
A[3] = 3 A[4] = 9 A[5] = 7
A[6] = 9
the elements at indexes 0 and 2 have value 9,
the elements at indexes 1 and 3 have value 3,
the elements at indexes 4 and 6 have value 9,
the element at index 5 has value 7 and is unpaired.
Write a function:
def solution(a)
that, given an array A consisting of N integers fulfilling the above conditions, returns the value of the unpaired element.
For example, given array A such that:
A[0] = 9 A[1] = 3 A[2] = 9
A[3] = 3 A[4] = 9 A[5] = 7
A[6] = 9
the function should return 7, as explained in the example above.
Write an efficient algorithm for the following assumptions:
N is an odd integer within the range [1..1,000,000];
each element of array A is an integer within the range [1..1,000,000,000];
all but one of the values in A occur an even number of times.
Solution:
# you can write to stdout for debugging purposes, e.g.
# puts "this is a debug message"
def solution(a)
# write your code in Ruby 2.2
new_arr = []
@counts = nil
a.each_with_object(Hash.new(0)) do |word, counts|
counts[word] += 1
@counts = counts
end
@counts.each do |count|
unless count[1]%2 == 0
new_arr << count[0]
end
end
new_arr.first
end
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment