Off-by-one on range boundaries
Wrong move: Loop endpoints miss first/last candidate.
Usually fails on: Fails on minimal arrays and exact-boundary answers.
Fix: Re-derive loops from inclusive/exclusive ranges before coding.
Move from brute-force thinking to an efficient approach using array strategy.
You are given a 2D integer array groups of length n. You are also given an integer array nums.
You are asked if you can choose n disjoint subarrays from the array nums such that the ith subarray is equal to groups[i] (0-indexed), and if i > 0, the (i-1)th subarray appears before the ith subarray in nums (i.e. the subarrays must be in the same order as groups).
Return true if you can do this task, and false otherwise.
Note that the subarrays are disjoint if and only if there is no index k such that nums[k] belongs to more than one subarray. A subarray is a contiguous sequence of elements within an array.
Example 1:
Input: groups = [[1,-1,-1],[3,-2,0]], nums = [1,-1,0,1,-1,-1,3,-2,0] Output: true Explanation: You can choose the 0th subarray as [1,-1,0,1,-1,-1,3,-2,0] and the 1st one as [1,-1,0,1,-1,-1,3,-2,0]. These subarrays are disjoint as they share no common nums[k] element.
Example 2:
Input: groups = [[10,-2],[1,2,3,4]], nums = [1,2,3,4,10,-2] Output: false Explanation: Note that choosing the subarrays [1,2,3,4,10,-2] and [1,2,3,4,10,-2] is incorrect because they are not in the same order as in groups. [10,-2] must come before [1,2,3,4].
Example 3:
Input: groups = [[1,2,3],[3,4]], nums = [7,7,1,2,3,4,7,7] Output: false Explanation: Note that choosing the subarrays [7,7,1,2,3,4,7,7] and [7,7,1,2,3,4,7,7] is invalid because they are not disjoint. They share a common elements nums[4] (0-indexed).
Constraints:
groups.length == n1 <= n <= 1031 <= groups[i].length, sum(groups[i].length) <= 1031 <= nums.length <= 103-107 <= groups[i][j], nums[k] <= 107Problem summary: You are given a 2D integer array groups of length n. You are also given an integer array nums. You are asked if you can choose n disjoint subarrays from the array nums such that the ith subarray is equal to groups[i] (0-indexed), and if i > 0, the (i-1)th subarray appears before the ith subarray in nums (i.e. the subarrays must be in the same order as groups). Return true if you can do this task, and false otherwise. Note that the subarrays are disjoint if and only if there is no index k such that nums[k] belongs to more than one subarray. A subarray is a contiguous sequence of elements within an array.
Start with the most direct exhaustive search. That gives a correctness anchor before optimizing.
Pattern signal: Array · Two Pointers · Greedy · String Matching
[[1,-1,-1],[3,-2,0]] [1,-1,0,1,-1,-1,3,-2,0]
[[10,-2],[1,2,3,4]] [1,2,3,4,10,-2]
[[1,2,3],[3,4]] [7,7,1,2,3,4,7,7]
Source-backed implementations are provided below for direct study and interview prep.
// Accepted solution for LeetCode #1764: Form Array by Concatenating Subarrays of Another Array
class Solution {
public boolean canChoose(int[][] groups, int[] nums) {
int n = groups.length, m = nums.length;
int i = 0;
for (int j = 0; i < n && j < m;) {
if (check(groups[i], nums, j)) {
j += groups[i].length;
++i;
} else {
++j;
}
}
return i == n;
}
private boolean check(int[] a, int[] b, int j) {
int m = a.length, n = b.length;
int i = 0;
for (; i < m && j < n; ++i, ++j) {
if (a[i] != b[j]) {
return false;
}
}
return i == m;
}
}
// Accepted solution for LeetCode #1764: Form Array by Concatenating Subarrays of Another Array
func canChoose(groups [][]int, nums []int) bool {
check := func(a, b []int, j int) bool {
m, n := len(a), len(b)
i := 0
for ; i < m && j < n; i, j = i+1, j+1 {
if a[i] != b[j] {
return false
}
}
return i == m
}
n, m := len(groups), len(nums)
i := 0
for j := 0; i < n && j < m; {
if check(groups[i], nums, j) {
j += len(groups[i])
i++
} else {
j++
}
}
return i == n
}
# Accepted solution for LeetCode #1764: Form Array by Concatenating Subarrays of Another Array
class Solution:
def canChoose(self, groups: List[List[int]], nums: List[int]) -> bool:
n, m = len(groups), len(nums)
i = j = 0
while i < n and j < m:
g = groups[i]
if g == nums[j : j + len(g)]:
j += len(g)
i += 1
else:
j += 1
return i == n
// Accepted solution for LeetCode #1764: Form Array by Concatenating Subarrays of Another Array
/**
* [1764] Form Array by Concatenating Subarrays of Another Array
*
* You are given a 2D integer array groups of length n. You are also given an integer array nums.
* You are asked if you can choose n disjoint subarrays from the array nums such that the i^th subarray is equal to groups[i] (0-indexed), and if i > 0, the (i-1)^th subarray appears before the i^th subarray in nums (i.e. the subarrays must be in the same order as groups).
* Return true if you can do this task, and false otherwise.
* Note that the subarrays are disjoint if and only if there is no index k such that nums[k] belongs to more than one subarray. A subarray is a contiguous sequence of elements within an array.
*
* Example 1:
*
* Input: groups = [[1,-1,-1],[3,-2,0]], nums = [1,-1,0,1,-1,-1,3,-2,0]
* Output: true
* Explanation: You can choose the 0^th subarray as [1,-1,0,<u>1,-1,-1</u>,3,-2,0] and the 1^st one as [1,-1,0,1,-1,-1,<u>3,-2,0</u>].
* These subarrays are disjoint as they share no common nums[k] element.
*
* Example 2:
*
* Input: groups = [[10,-2],[1,2,3,4]], nums = [1,2,3,4,10,-2]
* Output: false
* Explanation: Note that choosing the subarrays [<u>1,2,3,4</u>,10,-2] and [1,2,3,4,<u>10,-2</u>] is incorrect because they are not in the same order as in groups.
* [10,-2] must come before [1,2,3,4].
*
* Example 3:
*
* Input: groups = [[1,2,3],[3,4]], nums = [7,7,1,2,3,4,7,7]
* Output: false
* Explanation: Note that choosing the subarrays [7,7,<u>1,2,3</u>,4,7,7] and [7,7,1,2,<u>3,4</u>,7,7] is invalid because they are not disjoint.
* They share a common elements nums[4] (0-indexed).
*
*
* Constraints:
*
* groups.length == n
* 1 <= n <= 10^3
* 1 <= groups[i].length, sum(groups[i].length) <= 10^<span style="font-size: 10.8333px;">3</span>
* 1 <= nums.length <= 10^3
* -10^7 <= groups[i][j], nums[k] <= 10^7
*
*/
pub struct Solution {}
// problem: https://leetcode.com/problems/form-array-by-concatenating-subarrays-of-another-array/
// discuss: https://leetcode.com/problems/form-array-by-concatenating-subarrays-of-another-array/discuss/?currentPage=1&orderBy=most_votes&query=
// submission codes start here
impl Solution {
pub fn can_choose(groups: Vec<Vec<i32>>, nums: Vec<i32>) -> bool {
let n = groups.len();
let m = nums.len();
(0..n)
.try_fold(0, |first_possible, i| {
let group_len = groups[i].len();
(first_possible..=m - group_len).find_map(|j| {
nums[j..j + group_len]
.iter()
.zip(groups[i].iter())
.all(|(&a, &b)| a == b)
.then(|| j + group_len)
})
})
.is_some()
}
}
// submission codes end
#[cfg(test)]
mod tests {
use super::*;
#[test]
fn test_1764_example_1() {
let groups = vec![vec![1, -1, -1], vec![3, -2, 0]];
let nums = vec![1, -1, 0, 1, -1, -1, 3, -2, 0];
let result = true;
assert_eq!(Solution::can_choose(groups, nums), result);
}
#[test]
fn test_1764_example_2() {
let groups = vec![vec![10, -2], vec![1, 2, 3, 4]];
let nums = vec![1, 2, 3, 4, 10, -2];
let result = false;
assert_eq!(Solution::can_choose(groups, nums), result);
}
#[test]
fn test_1764_example_3() {
let groups = vec![vec![1, 2, 3], vec![3, 4]];
let nums = vec![1, 2, 3, 4, 10, -2];
let result = false;
assert_eq!(Solution::can_choose(groups, nums), result);
}
}
// Accepted solution for LeetCode #1764: Form Array by Concatenating Subarrays of Another Array
// Auto-generated TypeScript example from java.
function exampleSolution(): void {
}
// Reference (java):
// // Accepted solution for LeetCode #1764: Form Array by Concatenating Subarrays of Another Array
// class Solution {
// public boolean canChoose(int[][] groups, int[] nums) {
// int n = groups.length, m = nums.length;
// int i = 0;
// for (int j = 0; i < n && j < m;) {
// if (check(groups[i], nums, j)) {
// j += groups[i].length;
// ++i;
// } else {
// ++j;
// }
// }
// return i == n;
// }
//
// private boolean check(int[] a, int[] b, int j) {
// int m = a.length, n = b.length;
// int i = 0;
// for (; i < m && j < n; ++i, ++j) {
// if (a[i] != b[j]) {
// return false;
// }
// }
// return i == m;
// }
// }
Use this to step through a reusable interview workflow for this problem.
Two nested loops check every pair of elements. The outer loop picks one element, the inner loop scans the rest. For n elements that is n × (n−1)/2 comparisons = O(n²). No extra memory — just two loop variables.
Each pointer traverses the array at most once. With two pointers moving inward (or both moving right), the total number of steps is bounded by n. Each comparison is O(1), giving O(n) overall. No auxiliary data structures are needed — just two index variables.
Review these before coding to avoid predictable interview regressions.
Wrong move: Loop endpoints miss first/last candidate.
Usually fails on: Fails on minimal arrays and exact-boundary answers.
Fix: Re-derive loops from inclusive/exclusive ranges before coding.
Wrong move: Advancing both pointers shrinks the search space too aggressively and skips candidates.
Usually fails on: A valid pair can be skipped when only one side should move.
Fix: Move exactly one pointer per decision branch based on invariant.
Wrong move: Locally optimal choices may fail globally.
Usually fails on: Counterexamples appear on crafted input orderings.
Fix: Verify with exchange argument or monotonic objective before committing.