Skip to content

Practice Find Latest Group of Size M

Read the task, work through the examples, and check your approach against the constraints. Use the original LeetCode task for coding and submissions.

Medium#1562
LeetCode Problem

Find Latest Group of Size M

Problem Statement

Given an array arr that represents a permutation of numbers from 1 to n. You have a binary string of size n that initially has all its bits set to zero. At each step i (assuming both the binary string and arr are 1-indexed) from 1 to n, the bit at position arr[i] is set to 1. You are also given an integer m. Find the latest step at which there exists a group of ones of length m. A group of ones is a contiguous substring of 1's such that it cannot be extended in either direction. Return the latest step at which there exists a group of ones of length exactly m. If no such group exists, return -1.

Source: Find Latest Group of Size M on LeetCode. Use the original task for its coding environment and submissions.

Examples

# Example 1

Input
arr = [3,5,1,2,4], m = 1
Output
4

# Example 2

Input
arr = [3,1,5,4,2], m = 2
Output
-1

Constraints

n == arr.length
1 <= m <= n <= 10^5
1 <= arr[i] <= n
All integers in arr are distinct.

Related LeetCode Problems

Practice more problems similar to Find Latest Group of Size M.

Review your practice with PhantomCodeAI

After trying the problem yourself, use the app to discuss your approach, complexity, and edge cases.