
- 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
Finding peculiar pairs in array in JavaScript
Problem
We are required to write a JavaScript function that takes in an array of Integers, arr, as the first and the only argument.
Our function should count the occurrence of all such index pairs (i, j) that satisfy the following conditions −
I < j, and
arr[i] > 2 * arr[j]
For example, if the input to the function is −
const input = [2, 4, 3, 5, 1];
Then the output should be −
const output = 3;
Output Explanation:
Because the three desired pairs are −
[4, 1], [3, 1] and [5, 1]
Example
The code for this will be −
const arr = [2, 4, 3, 5, 1]; const peculiarPairs = (arr = []) => { let count = 0; let copy = arr.slice().sort((a,b)=> a - b); let bit = new Array(arr.length+1).fill(0); for (const num of arr){ count += search(bit, indexed(copy, 2*num+1)); bit = insert(bit, indexed(copy, num)); }; return count; }; const search = (bit, i) => { let sum = 0; while (i < bit.length){ sum += bit[i]; i += i & -i; } return sum; } const insert = (bit, i) => { while (i > 0){ bit[i] += 1; i -= i & -i; } return bit; } const indexed = (arr, val) => { let l = 0, r = arr.length-1, m = 0; while (l <= r) { m = l + ((r-l) >> 1); if (arr[m] >= val){ r = m-1; }else{ l = m+1; } } return l+1; } console.log(peculiarPairs(arr));
Code Explanation
We have here used a Binary Indexed Tree (BIT) −
Binary Indexed Tree or BIT is represented as an array. Let the array be BITree[]. Each node of the Binary Indexed Tree stores the sum of some elements of the input array. The size of the Binary Indexed Tree is equal to the size of the input array.
Output
And the output in the console will be −
3
- Related Articles
- Finding upper elements in array in JavaScript
- Beginning and end pairs in array - JavaScript
- Finding unlike number in an array - JavaScript
- Finding median index of array in JavaScript
- Finding unique string in an array in JavaScript
- Finding special array - JavaScript
- Finding a number of pairs from arrays with smallest sums in JavaScript
- Finding first unique element in sorted array in JavaScript
- Finding desired numbers in a sorted array in JavaScript
- Finding the longest substring uncommon in array in JavaScript
- Finding average in mixed data type array in JavaScript
- Finding the longest string in an array in JavaScript
- Finding minimum time difference in an array in JavaScript
- Finding the maximum in a nested array - JavaScript
- Finding the rotation of an array in JavaScript

Advertisements