- Trending Categories
Data Structure
Networking
RDBMS
Operating System
Java
iOS
HTML
CSS
Android
Python
C Programming
C++
C#
MongoDB
MySQL
Javascript
PHP
Physics
Chemistry
Biology
Mathematics
English
Economics
Psychology
Social Studies
Fashion Studies
Legal Studies
- Selected Reading
- UPSC IAS Exams Notes
- Developer's Best Practices
- Questions and Answers
- Effective Resume Writing
- HR Interview Questions
- Computer Glossary
- Who is Who
Finding median for every window in JavaScript
Median
Median in mathematics, median is the middle value in an ordered(sorted) integer list.
If the size of the list is even, and there is no middle value. Median is the mean (average) of the two middle values.
Problem
We are required to write a JavaScript function that takes in an array of Integers, arr, as the first argument and a number num (num <= length of array arr) as the second argument.
Now for each window of size num in the array arr, our function should calculate the median and push that median value into a new array and finally at the end of iteration return that median array.
For example, if the input to the function is −
const arr = [5, 3, 7, 5, 3, 1, 8, 9, 2, 4, 6, 8]; const num = 3;
Then the output should be −
const output = [5, 5, 5, 3, 3, 8, 8, 4, 4, 6];
Output Explanation:
Starting Index | Current Window | Current Sorted window | Median |
---|---|---|---|
0 | [5, 3, 7] | [3, 5, 7] | 5 |
1 | [3, 7, 5] | [3, 5, 7] | 5 |
2 | [7, 5, 3] | [3, 5, 7] | 5 |
3 | [5, 3, 1] | [1, 3, 5] | 3 |
4 | [3, 1, 8] | [1, 3, 8] | 3 |
5 | [1, 8, 9] | [1, 8, 9] | 8 |
6 | [8, 9, 2] | [2, 8, 9] | 8 |
7 | [9, 2, 4] | [2, 4, 9] | 4 |
8 | [2, 4, 6] | [2, 4, 6] | 4 |
9 | [4, 6, 8] | [4, 6, 8] | 6 |
Example
The code for this will be −
const arr = [5, 3, 7, 5, 3, 1, 8, 9, 2, 4, 6, 8]; const num = 3; const binarySearch = (arr, target, l, r) => { while (l < r) { const mid = Math.floor((l + r) / 2); if (arr[mid] < target) l = mid + 1; else if (arr[mid] > target) r = mid; else return mid; }; if (l === r) return arr[l] >= target ? l : l + 1; } const medianSlidingWindow = (arr = [], num = 1) => { let l = 0, r = num - 1, res = []; const window = arr.slice(l, num); window.sort((a, b) => a - b); while (r < arr.length) { const median = num % 2 === 0 ? (window[Math.floor(num / 2) - 1] + window[Math.floor(num / 2)]) / 2 : window[Math.floor(num / 2)]; res.push(median); let char = arr[l++]; let index = binarySearch(window, char, 0, window.length - 1); window.splice(index, 1); char = arr[++r]; index = binarySearch(window, char, 0, window.length - 1); window.splice(index, 0, char); } return res; }; console.log(medianSlidingWindow(arr, num));
Code Explanation:
Idea behind this solution is to use binary search to insert right number and remove left number when moving the sliding window to the right.
Output
And the output in the console will be −
[5, 5, 5, 3, 3, 8, 8, 4, 4, 6 ]
- Related Articles
- Finding median index of array in JavaScript
- Sliding Window Median in C++
- Finding sum of every nth element of array in JavaScript
- Find maximum of minimum for every window size in a given array in C++
- Finding Mean, Median, Mode in Python without Libraries
- Finding Median in a Sorted Linked List in C++
- Getting every child widget of a Tkinter window
- Finding value of a sequence for numbers in JavaScript
- Finding next greater node for each node in JavaScript
- First negative integer in every window of size k in C++
- Finding number that appears for odd times - JavaScript
- Finding power set for a set in JavaScript Power Set
- Min window substring in JavaScript
- JavaScript BOM Window Screen
- JavaScript example for Capturing mouse positions after every given interval
