Java gives you an integer array arr, please judge whether there is a situation where three consecutive elements are odd numbers in the array: if it exists, please return true; otherwise, return false.

Given an integer array arr, please judge whether there is a situation where three consecutive elements are odd in the array: 
 if so, return true; otherwise, return false. 
Example 1: 
Input: arr = [2,6,4,1] 
Output: false 
Explanation: There is no case where three consecutive elements are odd. 
Example 2: 
Input: arr = [1, 2, 34, 3, 4, 5, 7, 23, 12] 
Output: true 
Explanation: There are three consecutive elements that are odd, that is [5, 7, 23] .

public class Test35 {
    public static void main(String[] args) {
        int[] arr={2,6,4,1};
        int[] arr2={1,2,34,3,4,5,7,23,12};
        isOdd(arr);
        isOdd(arr2);
    }

    private static void isOdd(int[] arr) {
        int count=0;
        for (int i = 0; i < arr.length-2; i++) {
            if(arr[i]%2!=0&&arr[i+1]%2!=0&&arr[i+2]%2!=0){
                count++;
            }
        }
        if(count>=1){
            System.out.println(true);
        }else{
            System.out.println(false);
        }
    }
}

Guess you like

Origin blog.csdn.net/m0_62218217/article/details/121444264