-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathDotproduct.java
More file actions
58 lines (42 loc) · 1.16 KB
/
Dotproduct.java
File metadata and controls
58 lines (42 loc) · 1.16 KB
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
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
import java.util.ArrayList;
import java.util.Arrays;
import java.util.Collections;
import java.util.List;
import java.util.Scanner;
public class Dotproduct {
public static void main(String[] args) {
// TODO Auto-generated method stub
Scanner scanner = new Scanner(System.in);
int n = scanner.nextInt();
int[] a = new int[n];
for (int i = 0; i < n; i++) {
a[i] = scanner.nextInt();
}
int[] b = new int[n];
for (int i = 0; i < n; i++) {
b[i] = scanner.nextInt();
}
System.out.println(minDotProduct(a, b));
}
private static long minDotProduct(int[] a, int[] b) {
//write your code here
long result = 0;
Arrays.sort(b);
for(int i=0; i<a.length-1;i++)
{
for(int j=i+1;j<a.length;j++)
{
if(a[i]<a[j])
{
int temp = a[i];
a[i] = a[j];
a[j] = temp;
}
}
}
for (int i = 0; i < a.length; i++) {
result += (long)a[i] * (long)b[i];
}
return result;
}
}