Embark on a journey of knowledge! Take the quiz and earn valuable credits.
Take A QuizChallenge yourself and boost your learning! Start the quiz now to earn credits.
Take A QuizUnlock your potential! Begin the quiz, answer questions, and accumulate credits along the way.
Take A Quiz
🧠 Problem Statement
Given:
An array of integers (can include both positive and negative numbers).
Task:
Find the maximum product that can be obtained by multiplying any two
integers in the array.
🧾 Sample Input/Output
Input:
int[] arr = {1, 20, -5, 4, -6};
Output:
120
Explanation:
Let’s do this clean.
✔️ Correct Sample:
Input:
int[] arr = {-10, -3, 5, 6, -2}
Output:
60
Explanation:
5 * 6 = 30, -10 * -3 = 30, max is 30
🔍 Brute Force Approach:
All Pair Products
public
static int maxProductBrute(int[] arr) {
int max = Integer.MIN_VALUE;
for (int i = 0; i < arr.length; i++) {
for (int j = i + 1; j < arr.length;
j++) {
max = Math.max(max, arr[i] *
arr[j]);
}
}
return max;
}
⛔ Time Complexity: O(n²)
✅
Easy to understand, but slow for large inputs
✅ Optimized Approach: Sort + Pick
Extremes
Sort the array, then consider:
import
java.util.Arrays;
public
class MaxProduct {
public static int maxProduct(int[] arr) {
Arrays.sort(arr);
int n = arr.length;
int product1 = arr[0] * arr[1]; // smallest two
int product2 = arr[n - 1] * arr[n -
2]; // largest two
return Math.max(product1, product2);
}
public static void main(String[] args) {
int[] arr = {-10, -3, 5, 6, -2};
System.out.println(maxProduct(arr)); //
Output: 30
}
}
✅ Time: O(n log n)
✅
Space: O(1) extra (aside from sorting)
🔁 Super Optimized:
One-Pass Scan (No Sorting)
public
static int maxProductFast(int[] arr) {
int max1 = Integer.MIN_VALUE, max2 =
Integer.MIN_VALUE;
int min1 = Integer.MAX_VALUE, min2 =
Integer.MAX_VALUE;
for (int num : arr) {
// Max values
if (num > max1) {
max2 = max1;
max1 = num;
} else if (num > max2) {
max2 = num;
}
// Min values
if (num < min1) {
min2 = min1;
min1 = num;
} else if (num < min2) {
min2 = num;
}
}
return Math.max(max1 * max2, min1 * min2);
}
✅ Time: O(n)
✅
Space: O(1)
✅
No sort required — perfect for interviews
🧠 Edge Cases
Input |
Output |
Notes |
[1, 2] |
2 |
Only one valid product |
[-10, -3, 5, 6, -2] |
30 |
Negative * negative is positive |
[0, 0, -1, -1] |
1 |
Avoid zeros ruining your logic |
[1, 0, 3, 100, -70, -50] |
3500 |
-70 * -50 = 3500 beats 3 * 100 = 300 |
🧨 Interview Variants
✅ Summary
Approach |
Time |
Space |
Notes |
Brute Force |
O(n²) |
O(1) |
Try all pairs |
Sort & Compare |
O(n log n) |
O(1) |
Compare min² and max² |
One-pass Scan |
O(n) |
O(1) |
Optimal, uses 4 vars only |
Because
they test core programming logic, data handling, loops, and algorithm
efficiency.
int[] is a primitive array; Integer[] is an array of objects (wrappers). The latter allows null values and works with collections.
For fixed-size problems, use arrays. For dynamic data, ArrayList is better — but stick to arrays unless otherwise asked
Index out of bounds, mutating arrays while iterating, and forgetting that Java arrays have fixed size.
Use a Set for uniqueness or sort the array first and remove duplicates in-place using two pointers.
It’s great for sorted arrays, especially for problems involving pair sums, removing duplicates, and reversing data in-place.
Use reversal techniques or cyclic replacements to do it in O(1) space.
Linear search = O(n); binary search = O(log n), but only on sorted arrays.
Always consider edge cases: empty arrays, one element, all duplicates, etc. Optimize with hashmaps or prefix sums where needed.
Use Arrays.sort(), System.arraycopy(), Arrays.toString(), and Collections when applicable — but show the manual solution first in interviews.
Please log in to access this content. You will be redirected to the login page shortly.
LoginReady to take your education and career to the next level? Register today and join our growing community of learners and professionals.
Comments(0)