2dbi
Home/Blinkit/Determine if a valid merged array of two arrays exists
BBlinkit·DSASDE-2DSA Phone Screen

Determine if a valid merged array of two arrays exists

Given two arrays of unique numbers, determine whether a valid merged array arr3 exists such that both input arrays appear as subsequences of it, with all elements globally unique.

Approach: Model the problem as a directed graph where each element's relative ordering in both arrays defines edges (e.g., if a precedes b in either array, add edge a → b). A valid merge exists if and only if this graph is a DAG (no cycles).

  • Use Kahn's Algorithm (BFS-based topological sort) to detect cycles
  • Time: O(N + E), Space: O(N + E) where N = total unique elements, E = edges from ordering constraints

Variations: extend to k arrays; reconstruct the actual merged array (topological order gives one valid answer).

added 11h ago
ClassementSalaire
Langue
Compte