There are many solutions to this problem extant, each rediscovered many times. Search "statistical clustering methods" or "cluster analysis." A particularly nice one for your problem is called "K-means". This was rediscovered by cartographers (!) where it is called "Jenks' method" (and is worth mentioning because it's used by some popular mapping software, so perhaps more people have heard of this than of any other method). The idea behind K-means is to minimize the population-weighted sum of variances of the subsets.
BTW, the challenge in most clustering algorithms lies in determining the number of clusters; usually that needs to be specified (as $N = 4$ in this example) or at least hinted at by the user. For instance, there always exists a minimum K-means solution: partition $S$ into singletons. If you require $i < n$, merge the two nearest neighbors but leave all the other singletons as they are: the sum of variances is then just one-quarter the square of the smallest difference among the $x_i$.
The Knapsack problem is NP, and any problem in NP can be reduced to an NP complete problem (Cook's Theorem). So to show that the knapsack problem is NP complete it is sufficient to show that an NP-complete problem is reducible to the Knapsack problem. We want to use the exact cover problem to show this. A lot of such reductions can be found in the paper of Karp[1], including the reduction that is described here.
My notation conforms mostly to the notation in this paper.
Assume that $\{S_j,j=1,\ldots,p\}$ is a family of sets that covers the set
$U=\{u_i,i=1,\ldots,t\}$, so
$$\cup_{j=1}^{m} S_j=U$$
We want to check if there is a subfamily $\{S_{j_h},h=1,\ldots,q\}$ of $\{S_j,j=1,\ldots,p\}$ that covers $U$ and where the members of these family are pairwise disjoint. Let $d$ be an arbitrary positive integer. We can assign the number
$$\; \epsilon_{i,j}= \begin{cases}
0,& u_i \not \in S_j \\
1, & u_i \in S_j
\end{cases} \\
a_j=\sum_{j=1}^m \epsilon_{i,j} d^{i-1} \tag{1}$$
to a subset $S_j$.
So $a_j$ is a number that can be written only with digits $0$ and $1$ in a number system with base $d$. It has the digit $1$ on position $i-1$ if and only if $u_i \in S_j$. If $a_{j_1}$ and $a_{j_2}$ are to values assigned to two sets of an exact cover, then there is no position in the representation as an $d$ base number, where both numbers have the digit $1$. The sum $b$ of all the numbers assigned to an exact cover is
$$b=\sum_{i=1}^{t}d^{i-1} \tag{2}$$
if $\{S_j,j=1,\ldots,p\}$ has an exact cover $\{S_{j_h},h=1,\ldots,q\}$ then $x=(x_1,\cdots,x_p)$ with
$$ x_j=
\begin{cases}
1 & \text{if}\; j=j_h, \text{for an } h \in \{1,...,q\} \\
0 & \text{if}\; j\ne j_h, \text{for all } h \in \{1,...,q\} \\
\end{cases}$$
is one solution of the
of the 0-1-Knaspack problem
$$\sum_{i=1}^{p} a_i x_i=b$$
where $a_j$ and $b$ are defined in $(1)$ and $(2)$.
If we select the base $d=p+1$ (or larger) fo all the $2^p$ 0-1-sums no carry will occurr when adding them. So when ome of them sum up to be, there summands con't have 1s on the same place.
[1] Richard M. Karp: Reducibility Among Combinatorial Problems
In: R. E. Miller and J. W. Thatcher (Hrsg.): Complexity of Computer Computations.
Plenum Press, New York, 1972,
p. 85–103
Best Answer
EDIT: Realized my prior algorithm was flawed. This new one should be right.
EDIT 2: And a second flaw was pointed out years later (despite its simplicity). Thanks @gfppoy
There is an $O(n)$ algorithm broken down into two cases. (Note that figuring out which of the two cases we're in boils down to figuring out whether an array is strictly decreasing, which is itself $O(n)$)
Case 1: The array is strictly decreasing. In this scenario, we simply compare pairs of adjacent numbers and return the value corresponding to the "closest" two.
Case 2: Everything else
Loop through the array, keeping track of the maximum element. The catch is, now we "reset" the maximum every time we find a new minimum element. Something like this in pseudocode:
That way, for each new "maximum" we find, we associate it with the smallest element to its left, and for that minimum element, we are only considering maxima to its right (and if we don't compare it with a maximum to its right, it will be because we found a smaller element that is still to the left of that maximum).
NB: It's possible to modify the pseudocode to make it cover both cases and still be $O(n)$. I've not done so since reasoning out the correctness of that code becomes unnecessarily complicated; the algorithm presented here is just easier to read and reason about.