Inertia is the sum of squared distances from each point to its assigned centroid.
Implement:
inertia(X, labels, centroids)elbow_k(inertias): given inertias for k = 1, 2, ..., K, return the k at the "elbow": the point with the largest second difference I[k-1] - 2 I[k] + I[k+1] (only interior points qualify, so 2 <= k <= K-1; ties go to the smaller k)