Find the difference of two sets $A = \\{a0, a1, ..., a{n-1}\\}$ and $B = \\{b0, b1, ...,…
- User
Find the difference of two sets and , .
Assistant
We need to solve: Given two sorted arrays, output elements in A that are not in B. Constraints up to 200k each, so O(n+m) works.
Approach: two pointers. i=0, j=0. While i<n and j<m: if a[i] < b[j], output a[i] and i++; else if a[i] > b[j], j++; else (equal) skip both i++, j++. After loop, output remaining a[i] to end.
Edge cases: duplicates? No.
Input format: first line n, then line of n numbers, then m, then line of m numbers.