Suppose we have an array called nums, we have to check whether there are three consecutive odd numbers in nums or not.
So, if the input is like nums = [18,15,2,19,3,11,17,25,20], then the output will be True as there are three consecutive odds [3,11,17].
To solve this, we will follow these steps −
length:= size of nums
if length is same as 1 or length is same as 2, then
return False
otherwise,
for i in range 0 to size of nums - 3, do
if nums[i], nums[i+1] and nums[i+2] all are odds, then
return True
return False
Example (Python)
Let us see the following implementation to get better understanding −
def solve(nums): length=len(nums) if length==1 or length ==2: return False else: for i in range(len(nums)-2): if nums[i] % 2 != 0 and nums[i+1] % 2 != 0 and nums[i+2] % 2 != 0: return True return False nums = [18,15,2,19,3,11,17,25,20] print(solve(nums))
Input
[18,15,2,19,3,11,17,25,20]
Output
True