You have two integer arrays, a and b, and an integer target value v. Determine whether there is a pair of numbers, where one number is taken from a and the other from b, that can be added together to get a sum of v. Return true if such a pair exists, otherwise return false.

Example

For a = [1, 2, 3]b = [10, 20, 30, 40], and v = 42, the output should be

solution(a, b, v) = true.

Input/Output

풀이

boolean solution(long[] a, long[] b, long v) {
    if (a.length == 0 || b.length == 0) return false;
    
    int idx = a.length - 1;
    
    // Hidden Test 19 시간 초과로 인한 이분탐색
    for (int i = 0; i < a.length / 2; i ++) {
        long head = a[i];
        long tail = a[idx --];
        
        for (long l : b) {
            if (head + l == v) return true;
            if (tail + l == v) return true;
            if (a.length % 2 > 0 && i == (a.length / 2) - 1) {
                if (a[i + 1] + l == v) return true;
            }
        }
    }
    
    return false;
}