800. 数组元素的目标和 - AcWing题库
Question
Content
给定两个升序排序的有序数组 A 和 B ,以及一个目标值 x 。
数组下标从 0 开始。
请你求出满足 A[i]+B[j]=x 的数对 (i,j)。
数据保证有唯一解。
输入格式
第一行包含三个整数 n,m,x ,分别表示 A 的长度,B 的长度以及目标值 x 。
第二行包含 n 个整数,表示数组 A 。
第三行包含 m 个整数,表示数组 B 。
输出格式
共一行,包含两个整数 i 和 j 。
数据范围
数组长度不超过 105 。
同一数组内元素各不相同。
1 ≤ 数组元素 ≤ 109
输入样例:
4 5 6
1 2 4 7
3 4 6 8 9
输出样例:
1 1
Solution
Java
import java.util.ArrayList;
import java.util.List;
import java.util.Scanner;
import java.util.stream.IntStream;
public class Main {
public static void main(String[] args) {
Scanner scanner = new Scanner(System.in);
int n = scanner.nextInt();
int m = scanner.nextInt();
int x = scanner.nextInt();
int[] a = IntStream.range(0, n).map(e -> scanner.nextInt())
.toArray();
int[] b = IntStream.range(0, m).map(e -> scanner.nextInt())
.toArray();
List<int[]> ans = findTargetSumOfArrayElements(a, b, x);
ans.forEach(e -> {
System.out.println(e[0] + " " + e[1]);
});
}
private static List<int[]> findTargetSumOfArrayElements(int[] a, int[] b, int x) {
List<int[]> ans = new ArrayList<>();
int n = a.length, m = b.length;
for (int i = 0, j = m - 1; i < n; i++) {
while (j >= 0 && a[i] + b[j] > x) {
j--;
}
if (j >= 0 && a[i] + b[j] == x) {
ans.add(new int[]{i, j});
}
}
return ans;
}
}
Summary
Java program takes two arrays, finds pairs with sum equal to x, and outputs indices.