- Data Structure
- Networking
- RDBMS
- Operating System
- Java
- MS Excel
- iOS
- HTML
- CSS
- Android
- Python
- C Programming
- C++
- C#
- MongoDB
- MySQL
- Javascript
- PHP
- Physics
- Chemistry
- Biology
- Mathematics
- English
- Economics
- Psychology
- Social Studies
- Fashion Studies
- Legal Studies
- Selected Reading
- UPSC IAS Exams Notes
- Developer's Best Practices
- Questions and Answers
- Effective Resume Writing
- HR Interview Questions
- Computer Glossary
- Who is Who
Program to check three consecutive odds are present or not in Python
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
Advertisements