Happy Neighborhood
Learn this problemProblem statement
In a neighborhood, there are N empty houses numbered from 1 to N arranged in a line. Each day, starting from day 1, one house will be occupied by residents. The sequence of occupied houses is given as a permutation of length N. On the ith day, the house with the number given by the ith element of the permutation will be occupied.
The neighborhood will be considered happy if there is at least one set of consecutive occupied houses. On which day will the neighborhood become happy?
Note: A permutation of length N is an array of N integers where each element is between 1 and N, with no repetitions.
Function
googleHappyNeighbor(N: int, M: int, house: int[]) → int
Complete the function solve. This function takes the following 3 parameters and returns the required answer:
N: Represents the number of housesM: Represents the number of consecutive houses neededhouse: Represents an array indicating the house that will be filled on each dayInput format for custom testing
Note: Use this input format if you are testing against custom input or writing code in a language where we don't provide boilerplate code.
N denoting the number of houses.M denoting the number of consecutive houses needed.house denoting the house that will be filled on each day.Output format
Print a single integer representing the first day on which the neighborhood becomes happy.
Examples
Example 1
N = 3M = 1house = [3, 2, 1]return = 1Constraints
1 ≤ M ≤ N ≤ 2 * 10^5house.length = Nhouseis a permutation of the integers from1throughN.
More Google problems
- Deduplicate Logs: Keep FirstONSITE INTERVIEW · Seen Jul 2026
- Deduplicate Logs: Keep LatestONSITE INTERVIEW · Seen Jul 2026
- Find a Template Across Binary-Tree LeavesONSITE INTERVIEW · Seen Jul 2026
- Maximum Programmer-Problem MatchingONSITE INTERVIEW · Seen Jul 2026
- Minimum Direction ViolationsONSITE INTERVIEW · Seen Jul 2026
- Stream Latest Log VersionsONSITE INTERVIEW · Seen Jul 2026
- Stream Unique Logs in Timestamp OrderONSITE INTERVIEW · Seen Jul 2026
- Top-K IP Addresses from File RecordsONSITE INTERVIEW · Seen Jul 2026