Given an array of integers, find a maximum sum of non-adjacent elements. For example, inputs [1, 0, 3, 9, 2,-1] should return 10 (1 + 9).
As far as I understand your problem:
int max = Integer.MIN_VALUE;
for(int i = 0; i < a.length - 2; ++i) {
for(int j = i + 2; j < a.length; ++j) {
max = Math.max(max, a[i] + a[j]);
}
}
This algorithm has complexity of O(n ²).
Sketch for a faster algorithm: You could sort the array values with its indices in descending order. Than you can search for the highest pair which has non-adjacent indices. This algorithm takes O(n log n) steps.