Largest Rectangle Area under Histogram using JavaScript | Without using Stacks Last Updated : 22 Sep, 2021 Comments Improve Suggest changes 4 Likes Like Report Find the largest rectangular area possible in a given histogram where the largest rectangle can be made of a number of contiguous bars. For simplicity, assume that all bars have the same width and the width is 1 unit. For example, consider the following histogram with 7 bars of heights {6, 2, 5, 4, 5, 1, 6}. The largest possible rectangle possible is 12 (see the below figure, the max area rectangle is highlighted in red) Approach: For every bar ‘x’, we calculate the area with ‘x’ as the smallest bar in the rectangle. We will find the index of the first smaller (smaller than ‘x’) bar on left of ‘x’ and the index of first smaller bar on right of ‘x’. Let us call these indexes as ‘left index’ and ‘right index’ respectively.Multiply 'x' with "right_index-left_index-1" and store it in areaReturn max area Below is the implementation of the above approach. index.js <script> function getMaxArea(arr, n) { var area = 0; for (var i = 0; i < n; i++) { var left_index; var right_index; for (var j = i; j >= 0; j--) { if (arr[j] < arr[i]) { left_index = j; break; } } left_index = j; for (var j = i; j < n; j++) { if (arr[j] < arr[i]) { right_index = j; break; } } right_index = j; area = Math.max(area, arr[i] * (right_index - left_index - 1)); } return area; } var array = [6, 2, 5, 4, 5, 1, 6]; document.write(getMaxArea(array, 5)); </script> Output: 12 Complexity: We are using linear search to find the minimum value, then the worst-case time complexity of this algorithm becomes O(n^2). Create Quiz Comment A akshitsaxenaa09 Follow 4 Improve A akshitsaxenaa09 Follow 4 Improve Article Tags : JavaScript Blogathon-2021 javascript-math JavaScript-Questions Explore JavaScript BasicsIntroduction to JavaScript4 min readVariables and Datatypes in JavaScript6 min readJavaScript Operators5 min readControl Statements in JavaScript4 min readArray & StringJavaScript Arrays7 min readJavaScript Array Methods7 min readJavaScript Strings5 min readJavaScript String Methods9 min readFunction & ObjectFunctions in JavaScript5 min readJavaScript Function Expression3 min readFunction Overloading in JavaScript4 min readObjects in JavaScript4 min readJavaScript Object Constructors4 min readOOPObject Oriented Programming in JavaScript3 min readClasses and Objects in JavaScript4 min readWhat Are Access Modifiers In JavaScript ?5 min readJavaScript Constructor Method7 min readAsynchronous JavaScriptAsynchronous JavaScript2 min readJavaScript Callbacks4 min readJavaScript Promise4 min readEvent Loop in JavaScript4 min readAsync and Await in JavaScript2 min readException HandlingJavascript Error and Exceptional Handling6 min readJavaScript Errors Throw and Try to Catch2 min readHow to create custom errors in JavaScript ?2 min readJavaScript TypeError - Invalid Array.prototype.sort argument1 min readDOMHTML DOM (Document Object Model)8 min readHow to select DOM Elements in JavaScript ?3 min readJavaScript Custom Events4 min readJavaScript addEventListener() with Examples9 min readAdvanced TopicsClosure in JavaScript4 min readJavaScript Hoisting6 min readScope of Variables in JavaScript3 min readJavaScript Higher Order Functions7 min readDebugging in JavaScript4 min read Like