KatInterview

Possible Subspace Intersection Dimensions

Math & Bit ManipulationAuto-graded · 23 test cases

Last updated

Computes all valid integer dimensions for the intersection of two vector subspaces given ambient and subspace dimensions.

Given two vector subspaces $U$ and $W$ embedded inside an ambient vector space $V$, determine all possible values for the dimension of their intersection, $\dim(U \cap W)$.

You are given three integers:

  • dim_u: The dimension of subspace $U$ ($\dim(U)$)
  • dim_w: The dimension of subspace $W$ ($\dim(W)$)
  • dim_v: The dimension of the ambient space $V$ ($\dim(V)$)

By Grassmann's dimension formula, the linear sum $U + W$ satisfies: $$\dim(U + W) = \dim(U) + \dim(W) - \dim(U \cap W)$$

Since $U \cap W \subseteq U$ and $U \cap W \subseteq W$, the intersection dimension cannot exceed either subspace's dimension. Additionally, since $U + W$ is a subspace of $V$, its dimension cannot exceed $\dim(V)$.

Return a sorted list of all valid non-negative integer dimensions that $\dim(U \cap W)$ can take.

Constraints

  • $0 \le \text{dim_u} \le \text{dim_v}$
  • $0 \le \text{dim_w} \le \text{dim_v}$
  • $1 \le \text{dim_v} \le 10^9$

Examples

Example 1

Input: possible_intersection_dimensions(4, 4, 7)

Output: [1, 2, 3, 4]

Example 2

Input: possible_intersection_dimensions(4, 4, 8)

Output: [0, 1, 2, 3, 4]

Example 3

Input: possible_intersection_dimensions(2, 3, 4)

Output: [1, 2]

Hints

Hint 1

Apply Grassmann's formula: dim(U + W) = dim(U) + dim(W) - dim(U ∩ W).

Hint 2

What are the structural upper and lower bounds on dim(U + W)?

Hint 3

Use dim(U + W) ≤ dim(V) to find the minimum possible intersection dimension, and min(dim(U), dim(W)) for the maximum.