
- 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
Find the smallest sum of all indices of unique number pairs summing to a given number in JavaScript
We are required to write a function that takes in an array of numbers as the first argument and a target sum as the second argument. We then want to loop through the array and then add each value to each other (except itself + itself).
And if the sum of the two values that were looped through equals the target sum, and the pair of values hasn't been encountered before, then we remember their indices and, at the end, return the full sum of all remembered indices.
If the array is −
const arr = [1, 4, 2, 3, 0, 5];
And the sum is −
const sum = 7;
Then the output should be 11, because,
4 + 3 = 7 5 + 2 = 7
Index −
4 [index: 1] 2 [index: 2] 3 [index: 3] 5 [index: 5]
i.e.
1 + 2 + 3 + 5 = 11
Example
The code for this will be −
const arr = [1, 4, 2, 3, 0, 5]; const findIndexSum = (arr = [], sum = 0) => { let copy = arr.slice(0); const used = []; let index = 0, indexFirst = 0, indexSecond, first, second; while (indexFirst < copy.length){ indexSecond = indexFirst + 1; while(indexSecond < copy.length){ first = copy[indexFirst]; second = copy[indexSecond]; if (first + second === sum){ used.push(first, second); copy = copy.filter(el => first !== el && second !== el ); indexFirst--; break; } indexSecond++; } indexFirst++; }; const indexSum = used.sort().reduce((acc, val, ind) => { const fromIndex = ind === 0 || val !== used[ind - 1] ? 0 : index + 1 index = arr.indexOf(val, fromIndex); return acc + index; }, 0); return indexSum; }; console.log(findIndexSum(arr, 7));
Output
And the output in the console will be −
11
- Related Articles
- Summing up all the digits of a number until the sum is one digit in JavaScript
- Summing all the unique values of an array - JavaScript
- Find smallest number with given number of digits and sum of digits in C++
- How to find all pairs of elements in Java array whose sum is equal to a given number?
- How to find the unique combination of sum from the given number C#?
- Program to find the sum of all digits of given number in Python
- Find the Number of Unique Pairs in an Array using C++
- Finding a number of pairs from arrays with smallest sums in JavaScript
- Find Sum of all unique subarray sum for a given array in C++
- Find smallest permutation of given number in C++
- Checking digit sum of smallest number in the array in JavaScript
- Recursive sum all the digits of a number JavaScript
- Destructively Sum all the digits of a number in JavaScript
- Program to find max number of K-sum pairs in Python
- Program to find total sum of all substrings of a number given as string in Python

Advertisements