FastPrepFastPrep
Problem Brief

Happy Neighbourhood

OA

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 Description

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

    1Example 1

    Input
    N = 3, M = 1, house = [3, 2, 1]
    Output
    1
    Explanation
    On day 1, house 3 is filled. Only 1 consecutive house is needed, so the neighbourhood becomes happy.

    2Example 2

    Input
    N = 10, M = 8, house = [8, 4, 9, 10, 3, 1, 7, 2, 6, 5]
    Output
    10
    Explanation
    🌺 Will add once find reliable reference:)

    3Example 3

    Input
    N = 20, M = 2, house = [16, 12, 14, 7, 11, 13, 15, 4, 1, 20, 9, 3, 10, 8, 6, 2, 17, 19, 18, 5]
    Output
    5
    Explanation
    🌺 Will add once find reliable reference:)

    4Example 4

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

    Constraints

    Limits and guarantees your solution can rely on.

    1 ≤ N ≤ 10^5
    1 ≤ M ≤ N
    1 ≤ house[i] ≤ N
    public int solve(int N, int M, int[] house) {
      // write your code here
    }
    
    Input

    N

    3

    M

    1

    house

    [3, 2, 1]

    Output

    1

    Sign in to submit your solution.