-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathMaxProduct.java
More file actions
41 lines (34 loc) · 1.17 KB
/
Copy pathMaxProduct.java
File metadata and controls
41 lines (34 loc) · 1.17 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
import java.util.Scanner;
class MaxProduct {
public static void main(String[] args) {
// put your code here
Scanner scanner = new Scanner(System.in);
int num = scanner.nextInt();
int[] arr = new int[num];
for(int i = 0; i < arr.length; i++){
arr[i] = scanner.nextInt();
}
int maxProduct;
int nextProduct;
while(true){
if(arr.length == 2){
System.out.println(arr[0] * arr[1]);
break;
} else {
for(int i = 0; i < arr.length; i++) {
for (int j = i + 1; j < arr.length; j++) {
maxProduct = arr[i] * arr[j];
nextProduct = arr[j] * arr[j + 1];
if (maxProduct < nextProduct) {
maxProduct = nextProduct;
System.out.println(maxProduct);
break;
} else if(maxProduct > nextProduct){
System.out.println(maxProduct);
}
}
}
}
}
}
}