You need to iterate the array from the beginning (i=0) and the end (j=array.length-1) and multiply the items at these indices as follows:
int [] array = new int [] {1, 1, 2, 3, 4, 5};
int sum = 0;
for (int i = 0, j = array.length-1; i <= j; i++, j--) {
sum += array[i] * array[j];
}
System.out.println(sum); //15
In case of an odd number of items and you want to count the middle once without multiplying it by itself (use above solution if not):
int [] array = new int [] {1, 1, 2, 2, 3, 4, 5};
int sum = 0;
for (int i = 0, j = array.length-1; i <= j; i++, j--) {
sum += (i!=j) ? array[i] * array[j] : array[i];
}
System.out.println(sum); //17
与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…