Q:

Write a Ruby program to check whether there is a 2 in the array with a 3 somewhere later in a given array of integers

0

Write a Ruby program to check whether there is a 2 in the array with a 3 somewhere later in a given array of integers.

All Answers

need an explanation for this answer? contact us directly to get an explanation for this answer

def check_array(nums)
   found1 = false    
   i = 0
   while i < nums.length
       if(nums[i] == 2)
            found1 = true
        end            
        if(found1 && nums[i] == 3)
            return true
        end  
        i = i + 1
   end
   return false
end
print check_array([6, 2, 3, 5]),"\n"
print check_array([2, 6, 5, 3]),"\n"
print check_array([6, 2, 5, 3]),"\n"
print check_array([3, 6, 5, 2]),"\n"
print check_array([3, 3, 2, 1]),"\n"

Output:

true
true
true
false
false

need an explanation for this answer? contact us directly to get an explanation for this answer

total answers (1)

Similar questions


need a help?


find thousands of online teachers now