Because of the 2 imbricated for loops in the original code,
for (int intI = 0; intI < d; intI++) {
for (int intK = 0; intK < a.Length; intK++) { ... } }
Because of the 2 imbricated for loops here:
for (int intI = 0; intI < d; intI++) { for (int intK = 0; intK < a.Length; intK++) { ... } }
your code performs d * a.Length actions. Which - which takes quadratic time when d is near a.Length/2.
So, we may ask, can we do better ?
- The answer is yes. There is a linear-time solution. It is as follows :
The answer is yes. There is a linear-time solution. It is as follows:
- "mirror" the first d
delements - mirror the whole array
- mirror the size-d
size-dfirst elements.
Implementation of mirror (reverreverse an array) is left to the reader.
Linear time complexity. 0(1) extra room needed (loop variable and temporary for exchange).
So it is clearly an improvement on the original version.