
- Javascript Basics Tutorial
- Javascript - Home
- Javascript - Overview
- Javascript - Syntax
- Javascript - Enabling
- Javascript - Placement
- Javascript - Variables
- Javascript - Operators
- Javascript - If...Else
- Javascript - Switch Case
- Javascript - While Loop
- Javascript - For Loop
- Javascript - For...in
- Javascript - Loop Control
- Javascript - Functions
- Javascript - Events
- Javascript - Cookies
- Javascript - Page Redirect
- Javascript - Dialog Boxes
- Javascript - Void Keyword
- Javascript - Page Printing
- JavaScript Objects
- Javascript - Objects
- Javascript - Number
- Javascript - Boolean
- Javascript - Strings
- Javascript - Arrays
- Javascript - Date
- Javascript - Math
- Javascript - RegExp
- Javascript - HTML DOM
- JavaScript Advanced
- Javascript - Error Handling
- Javascript - Validations
- Javascript - Animation
- Javascript - Multimedia
- Javascript - Debugging
- Javascript - Image Map
- Javascript - Browsers
- JavaScript Useful Resources
- Javascript - Questions And Answers
- Javascript - Quick Guide
- Javascript - Functions
- Javascript - Resources
Maximum products of two integers in linear time in JavaScript
We are required to write a JavaScript function that takes in an array of Numbers with positive as well as negative numbers and returns the maximum products of two numbers in one traversal.
Let's write the code for this function −
Example
const arr = [-1, -3, -4, 2, 0, -5]; const arr2 = [2, 3, 5, 7, -7, 5, 8, -5]; const produce = arr => arr.reduce((acc, val) => acc*val); const maximumProduct = (arr = []) => { const [first] = arr; if(!first){ return 0; }; const creds = arr.reduce((acc, val) => { const { min, max } = acc; if(val > max[0]){ max[1] = max[0]; max[0] = val; return acc; }; if(val < min[0]){ min[1] = min[0]; min[0] = val; return acc; }; if(val > max[1]){ max[1] = val; return acc; }; if(val < min[1]){ min[1] = val; return acc; }; return acc; }, { min: [first, first], max: [first, first] }); const { max, min } = creds; return produce(max) > produce(min) ? produce(max) : produce(min); }; console.log(maximumProduct(arr)); console.log(maximumProduct(arr2));
Output
The output in the console will be −
20 56
- Related Articles
- Two sum problem in linear time in JavaScript
- Maximum Product of Two Numbers in a List of Integers in JavaScript
- Maximum Possible Sum of Products in JavaScript
- Maximum Sum of Products of Two Arrays in C++
- Maximum Sum of Products of Two Array in C++ Program
- Find first duplicate item in array in linear time JavaScript
- Comparing integers by taking two numbers in JavaScript
- Maximum product of any two adjacent elements in JavaScript
- Sum of Two Integers in Python
- Random whole number between two integers JavaScript
- Finding maximum number from two arrays in JavaScript
- Maximum GCD of N integers with given product in C++
- Divide Two Integers in C++
- Maximum number of Unique integers in Sub- Array of given sizes in C++
- Find maximum XOR of given integer in a stream of integers in C++

Advertisements