> For the complete documentation index, see [llms.txt](https://mayanktyagi3111.gitbook.io/interview-prep/llms.txt). Markdown versions of documentation pages are available by appending `.md` to page URLs; this page is available as [Markdown](https://mayanktyagi3111.gitbook.io/interview-prep/hashmap-and-hashset-and-sliding-window/untitled-3.md).

# Longest Well-Performing Interval

We are given `hours`, a list of the number of hours worked per day for a given employee.

A day is considered to be a *tiring day* if and only if the number of hours worked is (strictly) greater than `8`.

A *well-performing interval* is an interval of days for which the number of tiring days is strictly larger than the number of non-tiring days.

Return the length of the longest well-performing interval.

**Example 1:**

```
Input: hours = [9,9,6,0,6,6,9]
Output: 3
Explanation: The longest well-performing interval is [9,9,6].
```

**Constraints:**

* `1 <= hours.length <= 10000`
* `0 <= hours[i] <= 16`

```java
class Solution {
    public int longestWPI(int[] hours) {
        if (hours.length == 0)
            return 0;
        int maxLen = 0;
        Map<Integer, Integer> map = new HashMap();
        // key is possible sum in hours array, value is index where that sum
        // appears for the first time
        int sum = 0; // sum at index i indicates the sum of hours[0:i] after transformation

        for (int i = 0; i < hours.length; i++) {
            sum += hours[i] > 8 ? 1 : -1; // transform each hour to 1 or -1
            if (!map.containsKey(sum))
                map.put(sum, i); // record where the sum appears for the first time

            if (sum > 0) // in hours[0:i], more 1s than -1s
                maxLen = i + 1;
            else if (map.containsKey(sum - 1))
                // get the index j where sum of hours[0:j] is sum - 1, so that sum of
                // hours[j+1:i] is 1
                maxLen = Math.max(maxLen, i - map.get(sum - 1));
        }
        return maxLen;
    }
}
```


---

# Agent Instructions
This documentation is published with GitBook. GitBook is the documentation platform designed so that both humans and AI agents can read, navigate, and reason over technical content effectively. Learn more at gitbook.com.

## Querying This Documentation
If you need additional information that is not directly available in this page, you can query the documentation dynamically by asking a question.

Perform an HTTP GET request on the current page URL with the `ask` query parameter, and the optional `goal` query parameter:

```
GET https://mayanktyagi3111.gitbook.io/interview-prep/hashmap-and-hashset-and-sliding-window/untitled-3.md?ask=<question>&goal=<endgoal>
```

`ask` is the immediate question: it should be specific, self-contained, and written in natural language.
`goal` is optional and describes the broader end goal you are ultimately trying to accomplish on behalf of the user. GitBook uses it to tailor the answer towards what is most useful for that goal.

The response will contain a direct answer to the question and relevant excerpts and sources from the documentation.

Use this mechanism when the answer is not explicitly present in the current page, you need clarification or additional context, or you want to retrieve related documentation sections.
