Sum of 2 numbers in an array I have this problem to solve:

Given an integer \$z\$ and an array \$a\$ of distinct integers sorted in ascending order, find two numbers, \$x\$ and \$y\$, belonging to the array such that \$x + y = z\$. All this has to be done with \$O(n)\$ time performance in the worst case and \$O(1)\$ space.

I tried to jot down a trivial solution:
public static boolean find(int[] a, int z) {
    for (int i = 0; i &lt; a.length; i++) {
        for (int j = i + 1; j &lt; a.length; j++) {
            if (a[i] + a[j] == z)
                return true;
        }
    }
    return false;
}

However, it is clearly O(n^2) and I cannot find a better algorithm.
Can you help me?
Since there's no real review to do and we're apparently just posting solutions... here's a simple O(n) time O(1) space one:
public static boolean find(int[] a, int z) {
    int i = 0, j = a.length - 1;
    while (i &lt; j) {
        int sum = a[i] + a[j];
        if (sum == z)
            return true;
        if (sum &lt; z) i++; else j--;
    }
    return false;
}

Explanation: Try the ends, i.e., add the smallest and largest value. If the sum is right, bingo. If the sum is too small, then the smallest value is useless (it would need to be added with a number larger than the largest). If the sum is too large, then the largest value is useless. So remove the useless one. But since actually removing it, i.e., creating an array without it, would be expensive, just work with indexes marking the ends of the still potentially useful part of the array.