Skip to main content
added 42 characters in body
Source Link

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 < j) {
        int sum = a[i] + a[j];
        if (sum == z)
            return true;
        if (sum < 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.

Since there's no real review to do and we're apparently just posting solutions...

public static boolean find(int[] a, int z) {
    int i = 0, j = a.length - 1;
    while (i < j) {
        int sum = a[i] + a[j];
        if (sum == z)
            return true;
        if (sum < 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.

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 < j) {
        int sum = a[i] + a[j];
        if (sum == z)
            return true;
        if (sum < 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.

try to be nice
Source Link

Since there's no real review to do and we're apparently just posting solutions...

public static boolean find(int[] a, int z) {
    int i = 0, j = a.length - 1;
    while (i < j) {
        int sum = a[i] + a[j];
        if (sum == z)
            return true;
        if (sum < z) i++; else j--;
    }
    return false;
}

In case it's not obviousExplanation: 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.

Since there's no real review to do and we're apparently just posting solutions...

public static boolean find(int[] a, int z) {
    int i = 0, j = a.length - 1;
    while (i < j) {
        int sum = a[i] + a[j];
        if (sum == z)
            return true;
        if (sum < z) i++; else j--;
    }
    return false;
}

In case it's not obvious: 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.

Since there's no real review to do and we're apparently just posting solutions...

public static boolean find(int[] a, int z) {
    int i = 0, j = a.length - 1;
    while (i < j) {
        int sum = a[i] + a[j];
        if (sum == z)
            return true;
        if (sum < 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.

added 507 characters in body
Source Link

Since there's no real review to do and we're apparently just posting solutions...

public static boolean find(int[] a, int z) {
    int i = 0, j = a.length - 1;
    while (i < j) {
        int sum = a[i] + a[j];
        if (sum == z)
            return true;
        if (sum < z) i++; else j--;
    }
    return false;
}

In case it's not obvious: 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.

Since there's no real review to do and we're apparently just posting solutions...

public static boolean find(int[] a, int z) {
    int i = 0, j = a.length - 1;
    while (i < j) {
        int sum = a[i] + a[j];
        if (sum == z)
            return true;
        if (sum < z) i++; else j--;
    }
    return false;
}

Since there's no real review to do and we're apparently just posting solutions...

public static boolean find(int[] a, int z) {
    int i = 0, j = a.length - 1;
    while (i < j) {
        int sum = a[i] + a[j];
        if (sum == z)
            return true;
        if (sum < z) i++; else j--;
    }
    return false;
}

In case it's not obvious: 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.

Source Link
Loading