FastPrepHappy Neighbourhood
Problems/Happy Neighbourhood

Happy Neighbourhood

HackerEarth logoHackerEarth● MediumOA

Updated Nov 2026

QuestionFunctionExamplesConstraints

Problem statement

In a neighbourhood, there are N empty houses numbered from 1 to N, arranged in a line. Each day, starting from day 1, one house becomes occupied. The order is given as a permutation house of length N: on day i, house number house[i - 1] becomes occupied (the array is 0-indexed).

The neighbourhood is happy once there are at least M consecutive occupied houses, that is, M houses with numbers k, k + 1, ..., k + M - 1 that are all occupied. Return the first day on which the neighbourhood becomes happy.

Note: A permutation of length N is an array of N integers where each element is between 1 and N, with no repetitions.

Complete the function solve. It takes the following 3 parameters and returns the required day as an int.

  • N: the number of houses
  • M: the number of consecutive occupied houses needed
  • house: the house that becomes occupied on each day

Function

solve(N: int, M: int, house: int[]) → int

Examples

Example 1

N = 3M = 1house = [3, 2, 1]return = 1

On day 1, house 3 is filled. Only 1 consecutive house is needed, so the neighbourhood becomes happy.

Example 2

N = 10M = 8house = [8, 4, 9, 10, 3, 1, 7, 2, 6, 5]return = 10

After day 9 every house except 5 is occupied, so the longest blocks are houses 1-4 (4 houses) and 6-10 (5 houses). On day 10 house 5 joins them into one block of 10 houses, which is at least 8, so the answer is 10.

Example 3

N = 20M = 2house = [16, 12, 14, 7, 11, 13, 15, 4, 1, 20, 9, 3, 10, 8, 6, 2, 17, 19, 18, 5]return = 5

Days 1 to 4 fill houses 16, 12, 14 and 7, and no two of them are next to each other. On day 5 house 11 is filled next to house 12, giving 2 consecutive occupied houses, so the answer is 5.

Example 4

N = 5M = 5house = [5, 4, 1, 2, 3]return = 5

All 5 houses must be occupied, which first happens on day 5 when house 3 joins houses 1-2 and 4-5.

Constraints

  • 1 ≤ N ≤ 105
  • 1 ≤ M ≤ N
  • house.length = N
  • 1 ≤ house[i] ≤ N
  • house is a permutation: every number from 1 to N appears exactly once.

More HackerEarth interview questions

  • The PromOA · Seen Oct 2024

Similar questions at other companies

  • Minimum Contiguous ReplacementsAmazon · OA
  • Longest Contiguous Houses After Each BuildZipRecruiter · OAZipRecruiter interview questions
  • Neighboring House GroupsAtlassian · OAAtlassian interview questions
  • Recoverable Elements from Range-Sum MeasurementsEthos Life · OA
  • Resolve Overlapping Price RangesSquarepoint Capital · OASquarepoint Capital interview questions
  • Optimize Log FileGoogle · OAGoogle interview questions
  • Group Linked Merchant RecordsStripe · PHONE SCREENStripe interview questions
  • Radar Barrier CrossingSnap Inc. · ONSITE INTERVIEWSnap Inc. interview questions
  • Accounts MergeOracle · ONSITE INTERVIEWOracle interview questions
  • Count Islands in a Binary GridApple · PHONE SCREENApple interview questions
See HackerEarth hiring insights
Saved
class Solution {
    public int solve(int N, int M, int[] house) {
      // write your code here
    }

}
Checking account…