Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- public class Solution {
- public int[] solve(int[] A, int[] B, int C) {
- int i = 0, j = B.length - 1;
- int minAbs = Integer.MAX_VALUE, minI = -1, minJ = -1;
- while (i < A.length && j >= 0) {
- int sum = A[i] + B[j];
- int abs = Math.abs(sum - C);
- if (abs < minAbs || abs == minAbs && i == minI) {
- minAbs = abs;
- minI = i;
- minJ = j;
- }
- if (sum < C) {
- i++;
- } else if (sum > C) {
- j--;
- } else { // sum == C
- break;
- }
- }
- return new int[] { A[minI], B[minJ] };
- }
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement