For n real numbers $a_{1},\, a_{2},\, \ldots\, , a_{n},$ let $d$ denote the difference between the greatest and smallest of them and $S = \sum_{i<j}\left |a_i-a_j \right|.$ Prove that \[(n-1)d\le S\le\frac{n^{2}}{4}d\] and find when each equality holds.
1. **Rearrange the Sequence:**
   Let \( s_1, s_2, \ldots, s_n \) be a permutation of indices such that \( a_{s_1} \le a_{s_2} \le \ldots \le a_{s_n} \). This means \( a_{s_1} \) is the smallest and \( a_{s_n} \) is the largest among the \( a_i \)'s. Define \( d = a_{s_n} - a_{s_1} \).

2. **Express \( S \) in Terms of Differences:**
   Consider the sum \( S = \sum_{i < j} |a_i - a_j| \). Since \( a_{s_1} \le a_{s_2} \le \ldots \le a_{s_n} \), we can write:
   \[
   S = \sum_{i < j} (a_{s_j} - a_{s_i})
   \]

3. **Break Down the Sum:**
   Define \( b_i = a_{s_{i+1}} - a_{s_i} \) for \( i = 1, 2, \ldots, n-1 \). Then:
   \[
   a_{s_j} - a_{s_i} = \sum_{k=i}^{j-1} b_k
   \]
   Therefore:
   \[
   S = \sum_{i < j} \sum_{k=i}^{j-1} b_k
   \]

4. **Count the Occurrences of Each \( b_k \):**
   Each \( b_k \) appears in the sum \( \sum_{i < j} (a_{s_j} - a_{s_i}) \) exactly \( k(n-k) \) times. Hence:
   \[
   S = \sum_{k=1}^{n-1} k(n-k) b_k
   \]

5. **Establish the Lower Bound:**
   The smallest possible value of \( S \) occurs when all \( a_i \)'s are equal except for the smallest and largest values. In this case, \( b_1 = d \) and \( b_i = 0 \) for \( i > 1 \). Thus:
   \[
   S = (n-1)d
   \]
   Therefore, \( (n-1)d \le S \).

6. **Establish the Upper Bound:**
   The largest possible value of \( S \) occurs when the sequence is split into two equal parts, each contributing maximally to the sum. This happens when \( n \) is even, and \( b_{n/2} = d \) with all other \( b_i = 0 \). Thus:
   \[
   S = \frac{n}{2} \left( \frac{n}{2} \right) d = \frac{n^2}{4} d
   \]
   Therefore, \( S \le \frac{n^2}{4} d \).

7. **Equality Conditions:**
   - For \( (n-1)d = S \), equality holds when all \( a_i \)'s are equal except for the smallest and largest values.
   - For \( S = \frac{n^2}{4} d \), equality holds when \( n \) is even, and the sequence is split into two equal parts with the middle difference \( b_{n/2} = d \).

\(\blacksquare\)

The final answer is \( \boxed{ (n-1)d \le S \le \frac{n^2}{4}d } \)