Monday, 26 August 2019

Find the missing elements and display the sorted elements in ruby?

Inputs:
a=[1,2,3,4,5,6,7,8,9,10]
b=[1,2,4,5,6,7,8,9,10]

missing_elements = []
for i in a.min..a.max
    missing_elements.push(i) unless b.include?(i)
end

missing_elements.each do |m|
    b.insert(a.index(m),0)
end
p a
p b

for i in 0..a.length-1
puts "#{a[i]}-#{b[i]}"
end

OUTPUT:
1-1
2-2
3-0
4-4
5-5
6-6
7-7
8-8
9-9
10-10

No comments:

Post a Comment

Interactor in Rails

What is interactor? Interactor provides a common interface for performing complex user interactions An interactor is a simple, sin...