Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- def comfortableNumbers(l, r)
- a = l
- b = a + 1
- a_sum = 0
- b_sum = 0
- pairs = 0
- while (a < r)
- string = a.to_s
- x = 0
- while (x < string.length)
- a_sum = a_sum + string[x].to_i
- x = x + 1
- end
- while (b <= r)
- string = b.to_s
- x = 0
- while (x < string.length)
- b_sum = b_sum + string[x].to_i
- x = x + 1
- end
- if b >= a - a_sum and b <= a + a_sum and a >= b - b_sum and a <= b + b_sum then
- pairs = pairs + 1
- end
- b = b + 1
- b_sum = 0
- end
- a = a + 1
- b = a + 1
- a_sum = 0
- end
- return pairs
- end
Advertisement
Add Comment
Please, Sign In to add comment