-
Notifications
You must be signed in to change notification settings - Fork 23
/
Copy pathDotProduct.java
32 lines (27 loc) · 880 Bytes
/
DotProduct.java
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
import java.util.Arrays;
import java.util.Scanner;
public class DotProduct {
private static final Scanner SCANNER = new Scanner(System.in);
private static long maxDotProduct(long[] a, long[] b) {
Arrays.sort(a);
Arrays.sort(b);
long product = 0;
for (int index = 0 ; index < a.length ; index++) {
product += a[index] * b[index];
}
return product;
}
public static void main(String[] args) {
int length = SCANNER.nextInt();
long[] a = getArray(length);
long[] b = getArray(length);
System.out.println(maxDotProduct(a, b));
}
private static long[] getArray(int length) {
long[] array = new long[length];
for (int index = 0 ; index < array.length ; index++) {
array[index] = SCANNER.nextLong();
}
return array;
}
}