Skip to content

Latest commit

 

History

History
24 lines (23 loc) · 641 Bytes

File metadata and controls

24 lines (23 loc) · 641 Bytes

Group By Consecutive

fun longestAlternatingSubarray(nums: IntArray, threshold: Int): Int {
    var i = 0
    val n = nums.size
    var ans = 0
    while (i < n) {
        if (nums[i] % 2 != 0 || nums[i] > threshold) {
            i++
            continue
        }
        val start = i
        i++
        while (i < n && nums[i] <= threshold && nums[i - 1] % 2 != nums[i] % 2) {
            i++
        }
        val end = i
        ans = maxOf(ans, end - start)
    }
    return ans
}