1562. Find Latest Group of Size M LeetCode Solution

In this guide, you will get 1562. Find Latest Group of Size M LeetCode Solution with the best time and space complexity. The solution to Find Latest Group of Size M problem is provided in various programming languages like C++, Java, and Python. This will be helpful for you if you are preparing for placements, hackathons, interviews, or practice purposes. The solutions provided here are very easy to follow and include detailed explanations.

Table of Contents

  1. Problem Statement
  2. Complexity Analysis
  3. Find Latest Group of Size M solution in C++
  4. Find Latest Group of Size M solution in Java
  5. Find Latest Group of Size M solution in Python
  6. Additional Resources
1562. Find Latest Group of Size M LeetCode Solution image

Problem Statement of Find Latest Group of Size M

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.

Example 1:

Input: arr = [3,5,1,2,4], m = 1
Output: 4
Explanation:
Step 1: “00100”, groups: [“1”]
Step 2: “00101”, groups: [“1”, “1”]
Step 3: “10101”, groups: [“1”, “1”, “1”]
Step 4: “11101”, groups: [“111”, “1”]
Step 5: “11111”, groups: [“11111”]
The latest step at which there exists a group of size 1 is step 4.

Example 2:

Input: arr = [3,1,5,4,2], m = 2
Output: -1
Explanation:
Step 1: “00100”, groups: [“1”]
Step 2: “10100”, groups: [“1”, “1”]
Step 3: “10101”, groups: [“1”, “1”, “1”]
Step 4: “10111”, groups: [“1”, “111”]
Step 5: “11111”, groups: [“11111”]
No group of size 2 exists during any step.

Constraints:

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

Complexity Analysis

  • Time Complexity: O(n)
  • Space Complexity: O(n)

1562. Find Latest Group of Size M LeetCode Solution in C++

class Solution {
 public:
  int findLatestStep(vector<int>& arr, int m) {
    if (arr.size() == m)
      return arr.size();

    int ans = -1;
    int step = 0;
    // sizes[i] := the size of the group starting from i or ending in i
    // (1-indexed)
    vector<int> sizes(arr.size() + 2);

    for (const int i : arr) {
      ++step;
      // In the previous step, there exists a group with a size of m.
      if (sizes[i - 1] == m || sizes[i + 1] == m)
        ans = step - 1;
      const int head = i - sizes[i - 1];
      const int tail = i + sizes[i + 1];
      sizes[head] = tail - head + 1;
      sizes[tail] = tail - head + 1;
    }

    return ans;
  }
};
/* code provided by PROGIEZ */

1562. Find Latest Group of Size M LeetCode Solution in Java

class Solution {
  public int findLatestStep(int[] arr, int m) {
    if (arr.length == m)
      return arr.length;

    int ans = -1;
    int step = 0;
    // sizes[i] := the size of the group starting from i or ending in i
    // (1-indexed)
    int[] sizes = new int[arr.length + 2];

    for (final int i : arr) {
      ++step;
      // In the previous step, there exists a group with a size of m.
      if (sizes[i - 1] == m || sizes[i + 1] == m)
        ans = step - 1;
      final int head = i - sizes[i - 1];
      final int tail = i + sizes[i + 1];
      sizes[head] = tail - head + 1;
      sizes[tail] = tail - head + 1;
    }

    return ans;
  }
}
// code provided by PROGIEZ

1562. Find Latest Group of Size M LeetCode Solution in Python

class Solution:
  def findLatestStep(self, arr: list[int], m: int) -> int:
    if len(arr) == m:
      return len(arr)

    ans = -1
    step = 0
    # sizes[i] := the size of the group starting from i or ending in i
    # (1-indexed)
    sizes = [0] * (len(arr) + 2)

    for i in arr:
      step += 1
      # In the previous step, there exists a group with a size of m.
      if sizes[i - 1] == m or sizes[i + 1] == m:
        ans = step - 1
      head = i - sizes[i - 1]
      tail = i + sizes[i + 1]
      sizes[head] = tail - head + 1
      sizes[tail] = tail - head + 1

    return ans
# code by PROGIEZ

Additional Resources

Happy Coding! Keep following PROGIEZ for more updates and solutions.