-
Notifications
You must be signed in to change notification settings - Fork 0
/
maximumPairwise.java
102 lines (74 loc) · 2.75 KB
/
maximumPairwise.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
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
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
// Input: A sequence of non-negative numbers.
// Output: The maximum value that can be obtained by multiplying two different numbers from the input sequence.
// =>, For example, I’m giving you a sequence of non-negative numbers: a1 . . . . an
// Input format: The first line contains a single number whose value is equal to n. The next line contains n non-negative numbers a1 . . . . an.
// Output format: The maximum pairwise product.
// Sample 1
// Input
// :
// 3
// 1 2 3
// Output
// :
// 6
// Sample 2
// Input:
// 10
// 7 5 14 2 8 8 10 1 2 3
// Output:
// 140
// Naive Method :
// A naive approach is to solve the Maximum Pairwise Product Question is to find all the possible pairs from the sequence which is inputted by the user. A[1 . . . . n] =[a1 . . . . an] and then we have to find the largest product value.
// Logic :
// First, we have to find the two largest values from the inputted sequence. Because we know that the product of the largest value is the maximum product we can get.
// Note: All inputted numbers in the sequence is must be non-negative numbers.
import java.util.*;
import java.io.*;
public class MaxPairwiseProduct {
static long getMaxPairwiseProductFast(int[] numbers) {
int n_Size = numbers.length;
int max_index1 = -1;
for (int p = 0; p < n_Size; p++) {
if ((max_index1 == -1) || (numbers[p] > numbers[max_index1]))
max_index1 = i;
}
int max_index2 = -1;
for (int k = 0; k < n_Size; k++) {
if ((k != max_index1) && ((max_index2 == -1) || (numbers[k] > numbers[max_index2])))
max_index2 = j;
}
return (long)numbers[max_index1] * numbers[max_index2];
}
public static void main(String[] args) {
FastScanner scanner = new FastScanner(System.in);
int n = scanner.nextInt();
int[] numbers = new int[n];
for (int i = 0; i < n; i++) {
numbers[i] = scanner.nextInt();
}
System.out.println(getMaxPairwiseProductFast(numbers));
static class FastScanner {
BufferedReader br;
StringTokenizer st;
FastScanner(InputStream stream) {
try {
br = new BufferedReader(new InputStreamReader(stream));
} catch (Exception e) {
e.printStackTrace();
}
}
String next() {
while (st == null || !st.hasMoreTokens()) {
try {
st = new StringTokenizer(br.readLine());
} catch (IOException e) {
e.printStackTrace();
}
}
return st.nextToken();
}
int nextInt() {
return Integer.parseInt(next());
}
}
}