
- 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
Largest rectangle sum smaller than num in JavaScript
Problem
We are required to write a JavaScript function that takes in a 2-D array of Numbers as the first argument and a target sum number as the second argument.
Our function should find out that rectangle from the 2-D array which has the greatest sum among all rectangles in the array but just less than or equal to the target sum specified by the second argument to the function.
The function should then finally return that largest sum. For example, if the input to the function is −
const arr = [ [1, 0, 1], [0, -2, 3] ]; const num = 2;
Then the output should be −
const output = 2;
Output Explanation:
Because the smallest rectangle is −
[ [0, 1] [-2, 3] ]
Example
The code for this will be −
const arr = [ [1, 0, 1], [0, -2, 3] ]; const num = 2; const maxSum = (arr = [], num = 1) => { const rows = arr.length; const cols = arr[0].length; let maxSum = -Infinity; for(let l = 0; l < rows; l++) { const dp = Array(cols).fill(0); for(let r = l; r < rows; r++) { let sum = 0, max = -Infinity; for(let c = 0; c < cols; c++) { dp[c] += arr[r][c]; if(sum < 0) sum = 0; sum += dp[c]; max = Math.max(max, sum); } if(max <= num) maxSum = Math.max(max, maxSum); else { max = -Infinity; for(let c = 0; c < cols; c++) { sum = 0; for(let d = c; d < cols; d++) { sum += dp[d]; if(sum <= num) max = Math.max(sum, max); } } maxSum = Math.max(max, maxSum); } if(maxSum === num) return num; } } return maxSum; }; console.log(maxSum(arr, num));
Output
And the output in the console will be −
2
- Related Articles
- JavaScript Program to Count triplets with sum smaller than a given value
- Find largest number smaller than N with same set of digits in C++
- Largest number smaller than or equal to N divisible by K in C++
- Largest sum of subarrays in JavaScript
- Numbers smaller than the current number JavaScript
- Largest subarray having sum greater than k in C++
- Max Sum of Rectangle No Larger Than K in C++
- Largest number less than N with digit sum greater than the digit sum of N in C++
- Largest Rectangle in Histogram in Python
- Count numbers (smaller than or equal to N) with given digit sum in C++
- Counting How Many Numbers Are Smaller Than the Current Number in JavaScript
- Checking if all array values are smaller than a limit using JavaScript
- Minimum numbers which is smaller than or equal to N and with sum S in C++
- Cube Free Numbers smaller than n
- Sum of perimeter of all the squares in a rectangle using JavaScript

Advertisements