1D K-Means Algorithm in O(n)


You are given one-dimensional sorted data points. Write a program to cluster these points into two clusters such that the WCSS (Within-Cluster Squared Sum) metric is minimized. Try to come up with O(n) solution.

Note : Ensure the clustering results exhibit repeatability, meaning that if the program is executed multiple times, all the data points within a cluster will always remain in the same cluster. Additionally, guarantee optimality in the clustering results. These characteristics are not ensured in the standard K-Means algorithm, where cluster assignments can vary with each execution.

Example 1 : [1, 5, 7, 8, 15, 29, 100] will be clustered into
cluster 1 : [1, 5, 7, 8, 15]
cluster 2 : [29, 100]
Inputs
  • x : sorted list of data points
  • K = 2, divide into 2 clusters
Output
  • Two lists
    • cluster 1 : All the data points in cluster 1
    • Cluster 2 : All the data points in cluster 2
  • Min value of WCSS

Code Output