
- 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
Sum of two elements just less than n in JavaScriptn
We are required to write a JavaScript function that takes in an array of numbers, arr, as the first argument and a single number, num, as the second argument.
The function should then find two such numbers whose sum is greatest in the array but just less than the number num. If there exists no two such numbers whose sum is less than num, our function should return -1.
For example −
If the input array and the number are −
const arr = [34, 75, 33, 23, 1, 24, 54, 8]; const num = 60;
Then the output should be −
const output = 58;
because 34 + 24 is the greatest sum which is less than 60
Example
The code for this will be −
const arr = [34, 75, 33, 23, 1, 24, 54, 8]; const num = 60; const lessSum = (arr = [], num = 1) => { arr.sort((a, b) => a - b); let max = -1; let i = 0; let j = arr.length - 1; while(i < j){ let sum = arr[i] + arr[j]; if(sum < num){ max = Math.max(max,sum); i++; }else{ j--; }; }; return max; }; console.log(lessSum(arr, num));
Output
And the output in the console will be −
58
- Related Articles
- Product of subarray just less than target in JavaScript
- Two Sum Less Than K in Python
- Largest number less than N with digit sum greater than the digit sum of N in C++
- Maximum sum of n consecutive elements of array in JavaScript
- Program to find sum of two numbers which are less than the target in Python
- Count pairs with sum as a prime number and less than n in C++
- Generate a list of Primes less than n in Python
- Retaining array elements greater than cumulative sum using reduce() in JavaScript
- Subarray sum with at least two elements in JavaScript
- Alternating sum of elements of a two-dimensional array using JavaScript
- Python – Elements with factors count less than K
- Cumulative sum of elements in JavaScript
- Mask array elements less than a given value in Numpy
- Nearest prime less than given number n C++
- Smallest prime number just greater than the specified number in JavaScript

Advertisements