-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy path1550-Three_Consecutive_Odds.cpp
More file actions
79 lines (69 loc) · 1.66 KB
/
1550-Three_Consecutive_Odds.cpp
File metadata and controls
79 lines (69 loc) · 1.66 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
/*******************************************************************************
* 1550-Three_Consecutive_Odds.cpp
* Billy.Ljm
* 01 July 2024
*
* =======
* Problem
* =======
* https://leetcode.com/problems/three-consecutive-odds/
*
* Given an integer array arr, return true if there are three consecutive odd
* numbers in the array. Otherwise, return false.
*
* ===========
* My Approach
* ===========
* We just have to iterate through the array, counting the number of consecutive
* odd numbers along the way.
*
* This has a time complexity of O(n) and space complexity of O(1), where n is
* the length of the array
******************************************************************************/
#include <iostream>
#include <vector>
using namespace std;
/**
* << operator for vectors
*/
template <typename T>
std::ostream& operator<<(std::ostream& os, const std::vector<T>& v) {
os << "[";
for (const auto elem : v) {
os << elem << ",";
}
if (v.size() > 0) os << "\b";
os << "]";
return os;
}
/**
* Solution
*/
class Solution {
public:
bool threeConsecutiveOdds(vector<int>& arr) {
int cnt = 0;
for (int ele : arr) {
if (ele % 2 == 1) cnt++;
else cnt = 0;
if (cnt > 2) return true;
}
return false;
}
};
/**
* Test cases
*/
int main(void) {
Solution sol;
vector<int> arr;
// test case 1
arr = { 2,6,4,1 };
std::cout << "threeConsecutiveOdds(" << arr << ") = ";
std::cout << std::boolalpha << sol.threeConsecutiveOdds(arr) << std::endl;
// test case 2
arr = { 1,2,34,3,4,5,7,23,12 };
std::cout << "threeConsecutiveOdds(" << arr << ") = ";
std::cout << std::boolalpha << sol.threeConsecutiveOdds(arr) << std::endl;
return 0;
}