Longest vacation after converting at most k work days into days off

Quick Overview

Given a binary array of holidays and work days and a budget of k extra days off, find the longest stretch of consecutive days off you can create. It tests the sliding window technique for the longest subarray with at most k zeros and why the window never has to move backward.

Longest vacation after converting at most k work days into days off

Role: Software Engineer

Category: Coding & Algorithms

Difficulty: medium

Interview Round: Other

You are given a binary array `days`, where `days[i] = 1` means day `i` is a holiday and `days[i] = 0` means it is a work day. You are also given an integer `k`, the maximum number of extra days off you can take. You may convert at most `k` work days into days off. A vacation period is a contiguous run of days in which every day is either a holiday or a converted work day. Return the length of the longest vacation period you can achieve. ### Function Signature ```python def longest_vacation(days: list[int], k: int) -> int: ``` ### Rules - You may convert fewer than `k` work days, including none. - If no day can be a day off (every day is a work day and `k = 0`), return 0. ### Constraints - `1 <= len(days) <= 10^5` - `days[i]` is 0 or 1 - `0 <= k <= len(days)` ### Examples #### Example 1 ```text Input: days = [1, 1, 0, 0, 1, 1, 1, 0, 1], k = 2 Output: 7 ``` Converting the work days at indices 2 and 3 turns indices 0 to 6 into one run of 7 days off. Converting indices 3 and 7 instead gives a run from index 3 to 8 of length 6, and every other choice is shorter. #### Example 2 ```text Input: days = [0, 0, 0, 1, 1, 0], k = 1 Output: 3 ``` Converting index 2 gives the run at indices 2 to 4, and converting index 5 gives the run at indices 3 to 5. Both have length 3, and one conversion cannot do better. #### Example 3 ```text Input: days = [0, 0, 0], k = 0 Output: 0 ``` There are no holidays and no extra days to spend, so no day can be part of a vacation.

Overview: Given a binary array of holidays and work days and a budget of k extra days off, find the longest stretch of consecutive days off you can create. It tests the sliding window technique for the longest subarray with at most k zeros and why the window never has to move backward.

Nov 25, 2026
mediumSoftware EngineerOtherCoding & Algorithms
0
0

You are given a binary array days, where days[i] = 1 means day i is a holiday and days[i] = 0 means it is a work day. You are also given an integer k, the maximum number of extra days off you can take.

You may convert at most k work days into days off. A vacation period is a contiguous run of days in which every day is either a holiday or a converted work day. Return the length of the longest vacation period you can achieve.

Function Signature

def longest_vacation(days: list[int], k: int) -> int:

Rules

  • You may convert fewer than k work days, including none.
  • If no day can be a day off (every day is a work day and k = 0 ), return 0.

Constraints

  • 1 <= len(days) <= 10^5
  • days[i] is 0 or 1
  • 0 <= k <= len(days)

Examples

Example 1

Input: days = [1, 1, 0, 0, 1, 1, 1, 0, 1], k = 2
Output: 7

Converting the work days at indices 2 and 3 turns indices 0 to 6 into one run of 7 days off. Converting indices 3 and 7 instead gives a run from index 3 to 8 of length 6, and every other choice is shorter.

Example 2

Input: days = [0, 0, 0, 1, 1, 0], k = 1
Output: 3

Converting index 2 gives the run at indices 2 to 4, and converting index 5 gives the run at indices 3 to 5. Both have length 3, and one conversion cannot do better.

Example 3

Input: days = [0, 0, 0], k = 0
Output: 0

There are no holidays and no extra days to spend, so no day can be part of a vacation.