mirror of
https://github.com/TheAlgorithms/Ruby
synced 2024-12-27 21:58:57 +01:00
Add logical approach
This commit is contained in:
parent
1ef5a9ab07
commit
789f5fc03c
1 changed files with 22 additions and 1 deletions
|
@ -17,3 +17,24 @@
|
||||||
# Input: num = 9996
|
# Input: num = 9996
|
||||||
# Output: 9999
|
# Output: 9999
|
||||||
# Explanation: Changing the last digit 6 to 9 results in the maximum number.
|
# Explanation: Changing the last digit 6 to 9 results in the maximum number.
|
||||||
|
|
||||||
|
#
|
||||||
|
# Approach 1: Logical Approach
|
||||||
|
# Explanation: Changing the first available 6 to a 9 will give the max number
|
||||||
|
#
|
||||||
|
def max_number(num)
|
||||||
|
arr = num.to_s.split('')
|
||||||
|
|
||||||
|
arr.each_with_index do |num, i|
|
||||||
|
if num == '6'
|
||||||
|
arr[i] = '9'
|
||||||
|
return arr.join.to_i
|
||||||
|
end
|
||||||
|
end
|
||||||
|
end
|
||||||
|
|
||||||
|
puts max_number(9669)
|
||||||
|
# => 9969
|
||||||
|
|
||||||
|
puts max_number(9996)
|
||||||
|
# => 9999
|
Loading…
Reference in a new issue