public int solution03(int[] A) {
final int N = A.length;
double minSlc = (A[0] + A[1]) / 2.0;
int indMinSlc = 0;
for (int i = 0; i < N - 2; i++) {
double slc = (A[i] + A[i + 1]) / 2.0;
if (A[i + 2] < slc) {
slc = (A[i] + A[i + 1] + A[i + 2]) / 3.0;
}
if (slc < minSlc) {
minSlc = slc;
indMinSlc = i;
}
}
if ((A[N - 2] + A[N - 1]) / 2.0 < minSlc) {
indMinSlc = N - 2;
}
return indMinSlc;
}
=============
Explanation:
1. First slice consist of two elements.
2. You can lover the value of original slice by adding some new elements.
3. In order to lover the value of original slice, added elements have to have "slice value" lesser than original slice.
4. If pt. 3 is fullfilled than value of new slice created that way is bigger than slice of added elements.
5. So it has no sense try to create a new slice with lesser value by adding more than ONE element, because added elements will create a slice of even lesser value than one we try to create.
6. You can omit using double and dividing, by using multiplying and type int. But solution will less clear.