Problem · Array

Happy Neighbourhood

Learn this problem
MediumHAHackerearthOA

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 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 neighbourhood will be considered happy if there is at least one set of consecutive occupied houses. On which day will the neighbourhood 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

solve(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 houses
  • M: Represents the number of consecutive houses needed
  • house: Represents an array indicating the house that will be filled on each day
  • Input 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.

  • The first line contains N denoting the number of houses.
  • The second line contains M denoting the number of consecutive houses needed.
  • The third line contains an array house denoting the house that will be filled on each day.
  • Output format

    Print a single integer representing the first day on which the neighbourhood becomes happy.

    Note:

    Your code must be able to print the sample output from the provided sample input. However, your code is run against multiple hidden test cases. Therefore, your code must pass these hidden test cases to solve the problem statement.

    Limits

    • Time Limit: 1.0 sec(s) for each input file
    • Memory Limit: 256 MB
    • Source Limit: 1024 KB

    Scoring

    Score is assigned if any testcase passes

    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
    🌺 Will add once find reliable reference:)

    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
    🌺 Will add once find reliable reference:)

    Example 4

    N = 5M = 5house = [5, 4, 1, 2, 3]return = 5
    🌺 Will add once find reliable reference:)

    Constraints

    1 ≤ N ≤ 10^5
    1 ≤ M ≤ N
    1 ≤ house[i] ≤ N

    More Hackerearth problems

    drafts saved locally
    public int solve(int N, int M, int[] house) {
      // write your code here
    }
    
    N3
    M1
    house[3, 2, 1]
    expected1
    checking account