Question 1: You are given an array of integers.
Write a program to find the maximum
product of two distinct elements in the array.
Examples:
1. Input: [1, 2, 3, 4, 5]
Output: 20
(Explanation: The maximum product is 4 * 5 = 20)
2. Input: [7, -2, 3, -5, 10]
Output: 35
(Explanation: The maximum product is -5 * -7 = 35)
3. Input: [1, 1, 1]
Output: 1
(Explanation: The only pair possible is 1 * 1 = 1)
Code:
function maxProductOfTwo(arr) {
if (arr.length < 2) {
return null; // Return null if there aren't at least 2 elements
}
// Initialize variables
let max1 = -Infinity; // Largest number
let max2 = -Infinity; // Second largest number
let min1 = Infinity; // Smallest number
let min2 = Infinity; // Second smallest number
// Iterate through the array to find the top 2 largest and 2 smallest numbers
for (let num of arr) {
// Update max1 and max2
if (num > max1) {
max2 = max1;
max1 = num;
} else if (num > max2) {
max2 = num;
}
// Update min1 and min2
if (num < min1) {
min2 = min1;
min1 = num;
} else if (num < min2) {
min2 = num;
}
}
// Calculate the maximum product
const product1 = max1 * max2; // Product of two largest numbers
const product2 = min1 * min2; // Product of two smallest numbers (handles negatives)
return Math.max(product1, product2);
}
// Test cases
console.log(maxProductOfTwo([1, 2, 3, 4, 5])); // Output: 20
console.log(maxProductOfTwo([7, -2, 3, -5, 10])); // Output: 35
console.log(maxProductOfTwo([1, 1, 1])); // Output: 1
console.log(maxProductOfTwo([2])); // Output: null
Explanation:
1. Handle edge cases:
o If the array has less than two elements, return null since a product can't be
calculated.
2. Find the largest and smallest values:
o Use variables max1 and max2 to store the two largest numbers in the array.
o Use variables min1 and min2 to store the two smallest numbers in the array
(important for cases involving negative numbers).
3. Iterate through the array:
o Compare each number with max1 and max2 to update the largest numbers.
o Similarly, compare each number with min1 and min2 to update the smallest
numbers.
4. Calculate the maximum product:
o Compute the product of the two largest numbers (max1 * max2).
o Compute the product of the two smallest numbers (min1 * min2) to handle
cases where multiplying two negative numbers results in a larger positive
product.
o Return the maximum of these two products.
Question 2: Write a program to find the maximum product of three distinct elements in
an array.
Examples:
Input: [1, 2, 3, 4, 5]
Output: 60
(Explanation: The maximum product is 3 * 4 * 5 = 60)
Input: [-10, -20, 5, 1, 3]
Output: 1000
(Explanation: The maximum product is -10 * -20 * 5 = 1000)
Code:
function maxProductOfThree(arr) {
if (arr.length < 3) {
return null; // Not enough elements
}
arr.sort((a, b) => a - b); // Sort the array in ascending order
const n = arr.length;
const product1 = arr[n - 1] * arr[n - 2] * arr[n - 3]; // Product of the three largest numbers
const product2 = arr[0] * arr[1] * arr[n - 1]; // Product of two smallest and the largest
number
return Math.max(product1, product2);
}
// Test cases
console.log(maxProductOfThree([1, 2, 3, 4, 5])); // Output: 60
console.log(maxProductOfThree([-10, -20, 5, 1, 3])); // Output: 1000
console.log(maxProductOfThree([1, 1, 1])); // Output: 1
Question 3: Write a program to find the maximum product of any two adjacent elements in
the array.
Examples:
Input: [3, 6, -2, -5, 7, 3]
Output: 21
(Explanation: The maximum product is 7 * 3 = 21)
Input: [-1, -3, 10, 0, 5]
Output: 30
(Explanation: The maximum product is -3 * 10 = 30)
Code:
function maxAdjacentProduct(arr) {
if (arr.length < 2) {
return null; // Not enough elements
}
let maxProduct = -Infinity;
for (let i = 0; i < arr.length - 1; i++) {
const product = arr[i] * arr[i + 1];
maxProduct = Math.max(maxProduct, product);
}
return maxProduct;
}
// Test cases
console.log(maxAdjacentProduct([3, 6, -2, -5, 7, 3])); // Output: 21
console.log(maxAdjacentProduct([-1, -3, 10, 0, 5])); // Output: 30
console.log(maxAdjacentProduct([5, 5, 5])); // Output: 25
Explanation:
• The function iterates through the array and calculates the product of every pair of
adjacent elements.
• The maximum product is tracked and returned.
Question 4: Find the maximum product of k distinct elements in an array, where k is given.
Examples:
Input: arr = [1, 10, 2, 6, 5, 3], k = 3
Output: 300
(Explanation: The maximum product is 10 * 6 * 5 = 300)
Input: arr = [-1, -2, -3, -4, -5], k = 2
Output: 20
(Explanation: The maximum product is -4 * -5 = 20)
Code:
function maxProductOfK(arr, k) {
if (arr.length < k) {
return null; // Not enough elements
}
arr.sort((a, b) => Math.abs(b) - Math.abs(a)); // Sort by absolute values in descending order
let product = 1;
for (let i = 0; i < k; i++) {
product *= arr[i];
}
return product;
}
// Test cases
console.log(maxProductOfK([1, 10, 2, 6, 5, 3], 3)); // Output: 300
console.log(maxProductOfK([-1, -2, -3, -4, -5], 2)); // Output: 20
console.log(maxProductOfK([1, 1, 1], 2)); // Output: 1
Explanation:
• The array is sorted by the absolute value of each element (to handle negatives).
• The top k elements are multiplied to find the maximum product.
Question 5: Write a program to find the minimum product of two distinct elements in
the array.
Examples:
Input: [1, 2, 3, 4, 5]
Output: 2
(Explanation: The minimum product is 1 * 2 = 2)
Input: [-1, -3, 10, 0, 5]
Output: -30
(Explanation: The minimum product is -3 * 10 = -30)
Code:
function minProductOfTwo(arr) {
if (arr.length < 2) {
return null; // Not enough elements
}
arr.sort((a, b) => a - b); // Sort the array in ascending order
const n = arr.length;
// Two possible minimum products
const product1 = arr[0] * arr[1]; // Two smallest numbers
const product2 = arr[n - 2] * arr[n - 1]; // Two largest numbers (if negative)
return Math.min(product1, product2);
}
// Test cases
console.log(minProductOfTwo([1, 2, 3, 4, 5])); // Output: 2
console.log(minProductOfTwo([-1, -3, 10, 0, 5])); // Output: -30
console.log(minProductOfTwo([-10, -20, -30])); // Output: -600
Explanation:
• The array is sorted to find the smallest two elements.
• Both the smallest and largest pairs are checked (to handle negative products).
• The minimum product is returned.
For More Interview Questions:
Looking to ace your next opportunity? Prepare with our expert-curated resources!
Get Your PDF Files Instantly:
Structured learning made simple! Choose from our affordable guides:
1. Frontend Development PDF = ₹99/-
Covers ReactJS, and JavaScript.
2. MERN Stack Development PDF = ₹149/-
Comprehensive guide for Frontend, Backend, and Database development.
3. Backend Development PDF = ₹59/-
Focused on Node.js, Express.js, MongoDB, and related technologies.
Coming Soon:
500 Best Questions from LeetCode and HackerRank
Carefully selected questions frequently asked by top companies to help you ace your
interviews!
How to Get It?
• Message us on WhatsApp: 9398123134
• Pay the respective amount, and receive your PDF instantly!
Instant replies are available between 8:00 AM to 10:00 PM IST.
Join Our Online and Offline Classes
Upskill with confidence—our courses come with a
100% PLACEMENT GUARANTEE!
1. Frontend Technologies
Learn ReactJS, Angular, and modern UI/UX design.
2. Full Stack Development
Master MERN stack, Java, Python, or .NET technologies.
3. Data Science and Analytics
Gain skills in data visualization, modeling, and analysis.
4. Artificial Intelligence and Machine Learning
Explore AI tools, concepts, and practical implementations.
5. Advanced Topics
o Cloud Computing: AWS, Azure
o Cybersecurity: Ethical hacking, penetration testing
How to Enroll?
Call us at 9398123134 to get started today!