-
Notifications
You must be signed in to change notification settings - Fork 6
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
1550. Three Consecutive Odds #286
Comments
Hi, many of the issues seem resolved in main branch yet status-open here or am i mistaken? is this issue still open? as well as all the others in the list |
The problem asks us to determine if an array contains three consecutive odd numbers. If such a triplet exists, return Key Points
ApproachWe 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 Plan
Let's implement this solution in PHP: 1550. Three Consecutive Odds <?php
/**
* @param Integer[] $arr
* @return Boolean
*/
function threeConsecutiveOdds($arr) {
// Iterate through the array, checking each triplet
for ($i = 0; $i < count($arr) - 2; $i++) {
// Check if the current number and the next two numbers are odd
if ($arr[$i] % 2 != 0 && $arr[$i + 1] % 2 != 0 && $arr[$i + 2] % 2 != 0) {
// If all three are odd, return true
return true;
}
}
// If no such triplet is found, return false
return false;
}
// 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:
Example WalkthroughExample 1:Input:
Output: Example 2:Input:
Output: Time Complexity
Overall Time Complexity: O(n) Output for Example
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. |
Discussed in #285
Originally posted by mah-shamim August 9, 2024
Topics:
Array
Given an integer array
arr
, returntrue
if there are three consecutive odd numbers in the array. Otherwise, returnfalse
.Example 1:
Example 2:
Constraints:
1 <= arr.length <= 1000
1 <= arr[i] <= 1000
Hint:
The text was updated successfully, but these errors were encountered: