1550. Three Consecutive Odds
Difficulty: Easy
Topics: Array
Given an integer array arr
, return true
if there are three consecutive odd numbers in the array. Otherwise, return false
.
Example 1:
- Input: arr = [2,6,4,1]
- Output: false
- Explanation: There are no three consecutive odds.
Example 2:
- Input: arr = [1,2,34,3,4,5,7,23,12]
- Output: true
- Explanation: [5,7,23] are three consecutive odds.
Constraints:
1 <= arr.length <= 1000
1 <= arr[i] <= 1000
Hint:
- Check every three consecutive numbers in the array for parity.
Solution:
The problem asks us to determine if an array contains three consecutive odd numbers. If such a triplet exists, return true
; otherwise, return false
.
Key Points
-
Input: An array of integers (
arr
) where each element satisfies 1 \leq arr[i] \leq 1000. - Output: A boolean indicating the presence of three consecutive odd numbers.
- Core Operation: Checking for odd numbers and consecutive patterns.
-
Constraints:
- Array length is at least 1 and at most 1000.
Approach
We need to iterate through the array and check every triplet of consecutive numbers to see if they are all odd. If we find such a triplet, we can immediately return true
. If we traverse the entire array without finding one, return false
.
Plan
- Traverse the array from the first to the third last element, as each triplet involves three elements.
- For every element at index
i
, check if:- arr[i] % 2 ≠ 0 (odd)
- arr[i+1] % 2 ≠ 0 (odd)
- arr[i+2] % 2 ≠ 0 (odd)
- If the condition holds true, return
true
. - If the loop completes without finding any such triplet, return
false
.
Let's implement this solution in PHP: 1550. Three Consecutive Odds
<?php
/**
* @param Integer[] $arr
* @return Boolean
*/
function threeConsecutiveOdds($arr) {
...
...
...
/**
* go to ./solution.php
*/
}
// Example usage:
$arr1 = [[15, 96], [36, 2]];
$arr2 = [[1, 3, 5], [4, 1, 1], [1, 5, 3]];
$arr3 = [[2, 5, 1], [3, 4, 7], [8, 1, 2], [6, 2, 4], [3, 8, 8]];
echo threeConsecutiveOdds($arr1) . "\n"; // Output: 17
echo threeConsecutiveOdds($arr2) . "\n"; // Output: 4
echo threeConsecutiveOdds($arr3) . "\n"; // Output: 10
?>
Explanation:
- Step 1: Start iterating through the array up to index n-3, where n is the array length.
-
Step 2: For each index
i
, check if the element ati
,i+1
, andi+2
are odd numbers. -
Step 3: If found, return
true
immediately. -
Step 4: If the loop finishes without finding a triplet, return
false
.
Example Walkthrough
Example 1:
Input: arr = [2, 6, 4, 1]
-
i = 0: Triplet is
[2, 6, 4]
, none are odd. -
i = 1: Triplet is
[6, 4, 1]
, none are odd.
Output: false
Explanation: There are no three consecutive odd numbers.
Example 2:
Input: arr = [1, 2, 34, 3, 4, 5, 7, 23, 12]
-
i = 0: Triplet is
[1, 2, 34]
, not all are odd. -
i = 4: Triplet is
[4, 5, 7]
, not all are odd. -
i = 5: Triplet is
[5, 7, 23]
, all are odd.
Output: true
Explanation: The triplet [5, 7, 23]
satisfies the condition.
Time Complexity
- Loop: O(n), where n is the size of the array, as we iterate through the array once.
- Check for Odd: O(1) per triplet.
Overall Time Complexity: O(n)
Space Complexity: O(1) (No additional space used).
Output for Example
Example 1 Input:
arr = [2, 6, 4, 1]
Output:false
Example 2 Input:
arr = [1, 2, 34, 3, 4, 5, 7, 23, 12]
Output:true
The solution efficiently identifies three consecutive odd numbers by iterating through the array once. It utilizes a straightforward condition to determine odd parity and exits early when a valid triplet is found, minimizing unnecessary checks. The approach is optimal for the problem's constraints.
Contact Links
If you found this series helpful, please consider giving the repository a star on GitHub or sharing the post on your favorite social networks 😍. Your support would mean a lot to me!
If you want more helpful content like this, feel free to follow me:
Top comments (1)