Counting inversions using mergesort - javascript - javascript

The following code counts the no. of inversions in an array. It is always recursively getting divided into sub-problems until the stack error, RangeError: Maximum call stack size exceeded occurs even when the base case is defined. What could be the issue here
function mergeSort(arr) {
var n = arr.length;
var l=0,h=n-1;
if (l<h) {
var m=findMid(l,h);
var leftArr=arr.slice(l,m);
var rightArr=arr.slice(m,n);
var invCount = mergeSort(leftArr)+mergeSort(rightArr);
invCount += merge(leftArr,rightArr,n);
}
return invCount;
}
function merge(a, b,n) {
var i=0,j=0,m=[];
var splitInv=0;
for(var k=0;k<n;k++) {
if(a[i]<b[j]) m[k]=a[i++];
else if (b[j]<a[i]){
m[k]=b[j++];
splitInv+=n-i;
}
}
return splitInv;
}
function findMid(l, r) {
var m = Math.floor((l + r) / 2);
return m;
}
I had modified the above code to handle the base cases in a different way. Is the following logic correct:
function mergeSort(arr) {
var n = arr.length;
var l=0,h=n-1;
var invCount=0;
if(n<=2) {
return merge(arr[0],arr[1],n);
}else{
var m=Math.floor(n/2);
var leftArr=arr.slice(l,m);
var rightArr=arr.slice(m);
invCount += mergeSort(leftArr)+mergeSort(rightArr);
}
return invCount;
}
function merge(a, b,n) {
var i=0,j=0,m=[];
var splitInv=0;
if(typeof b=="undefined") {
return 0;
}
for(var k=0;k<n;k++) {
if(a[i]<b[j]) m[k]=a[i++];
else if (b[j]<a[i]){
m[k]=b[j++];
splitInv+=n-i;
}
}
return splitInv;
}

RangeError is coming due to too much recursive call to mergeSort.
For the size of arr 2 size of rightArr will remain 2.
Instead of
var rightArr=arr.slice(m,n);
You may do
var rightArr=arr.slice(m+1,n);

Same question posted here, Javascript implementation of the inversion-counting with merge-sort algorithm. This describes everything that you need.

Related

Project Euler #1 in JavaScript on HackerRank

I'm having some trouble with the test cases for Project Euler #1 on HackerRank and was hoping someone with some JS experience on HackerRank could help out.
Below is my code. As far as I can tell, I can't spot any obvious logic errors so I would appreciate if someone could shed some light on why I'm not passing all the test cases and why the status is "abort called". All the code above "ignore above this line" is provided by HackerRank and is not part of the solution.
process.stdin.resume();
process.stdin.setEncoding('ascii');
var input_stdin = "";
var input_stdin_array = "";
var input_currentline = 0;
process.stdin.on('data', function (data) {
input_stdin += data;
});
process.stdin.on('end', function () {
input_stdin_array = input_stdin.split("\n");
main();
});
function readLine() {
return input_stdin_array[input_currentline++];
}
/////////////// ignore above this line ////////////////////
function threeFiveMultiples(num) {
let array = [];
for (let i = 0; i < num; i++) {
if (i % 3 === 0 ||
i % 5 === 0) {
array.push(i);
}
}
return array.reduce(function(accum, currVal) {
return accum + currVal;
});
}
function main() {
var t = parseInt(readLine());
for(var a0 = 0; a0 < t; a0++){
var n = parseInt(readLine());
let res = threeFiveMultiples(n);
console.log(res);
}
}
I've also tried using a BigNumbers library (see below).
process.stdin.resume();
process.stdin.setEncoding('ascii');
var BigNumber = require('bignumber.js');
var input_stdin = "";
var input_stdin_array = "";
var input_currentline = 0;
process.stdin.on('data', function (data) {
input_stdin += data;
});
process.stdin.on('end', function () {
input_stdin_array = input_stdin.split("\n");
main();
});
function readLine() {
return input_stdin_array[input_currentline++];
}
/////////////// ignore above this line ////////////////////
function threeFiveMultiples(num) {
let array = [];
for (let i = 0; i < num; i++) {
if (i % 3 === 0 ||
i % 5 === 0) {
array.push(i);
}
}
return parseInt(array.reduce(function(accum, currVal) {
accum = new BigNumber(accum);
currVal = new BigNumber (currVal);
return accum.plus(currVal);
}));
}
function main() {
var t = parseInt(readLine());
for(var a0 = 0; a0 < t; a0++){
var n = parseInt(readLine());
let res = threeFiveMultiples(n);
console.log(res);
}
}
UPDATE:
Per the suggestion of #5ar, I replaced my reduce function with the following; however, test cases #2 and #3 are still failing:
return array.reduce(function(accum, currVal) {
return accum.plus(currVal);
}, new BigNumber(0)).toString();
Your code is fine. If you simply call console.log(threeFiveMultiples(1000));, it will give you the solution. Not sure what all the other code is about, but that will work.
Try to use Math.round() on your values (after summing them), JavaScript treats all numbers as doubles so some may not be showing correctly.
If big numbers are the problem, try to find a library that you can use that can handle big numbers. If you can't, you can implement them yourself via strings, considering that you only need summing it shouldn't be that difficult.
UPDATE:
I think the return might be the problem (you're using parseInt which probably converts the BigNumber to a String and then tries to parse it as a Number), try to do this:
return array.reduce(function(accum, currVal) {
return accum.plus(currVal);
}, new BigNumber(0)).toString();
The problem is that for really large numbers the calculation takes up a quite significant amount of time and hackerrank has a timeout of 10s for the calculation. You have to find a faster way of calculation the correct output.
A small hint: Arithmetic progression (its possible in O(1))
There are two possible reasons why your code is failing
1) Timeout error
The code logic you have written iterates each number and checks if it is divisible by 3 or 5. This logic causes the time out error when the number in the testcase is huge number.
2) Number Cannot be represented in JS
The number used in Test Case 3 & Test Case 2 is a big number. So using a library like 'bignumber.js' is necessary, since JS does not support long integers.
Below is the code snippet which will help pass all the Test cases:
process.stdin.resume();
process.stdin.setEncoding('ascii');
var input_stdin = "";
var input_stdin_array = "";
var input_currentline = 0;
process.stdin.on('data', function (data) {
input_stdin += data;
});
process.stdin.on('end', function () {
input_stdin_array = input_stdin.split("\n");
main();
});
function readLine() {
return input_stdin_array[input_currentline++];
}
/////////////// ignore above this line ////////////////////
function main() {
var BigNumber = require('bignumber.js');
var t = new BigNumber(readLine()).toNumber();
var n;
for (var a0 = 0; a0 < t; a0++) {
n = new BigNumber(readLine());
sumOfNumberDivisibleBy3and5(n);
}
}
function sumOfNumberDivisibleBy3and5(n) {
const a = n.minus(1).dividedBy(3).floor();
const b = n.minus(1).dividedBy(5).floor();
const c = n.minus(1).dividedBy(15).floor();
const sumThree = a.times(3).times(a.plus(1)).dividedBy(2);
const sumFive = b.times(5).times(b.plus(1)).dividedBy(2);
const sumFifteen = c.times(15).times(c.plus(1)).dividedBy(2);
const sumOfAll = sumThree.plus(sumFive).minus(sumFifteen);
console.log(sumOfAll.toString());
}
This worked for me
function main() {
var t = parseInt(readLine());
for(var a0 = 0; a0 < t; a0++){
var n = parseInt(readLine());
/////////////////they forgot to tell us to put code below here
let N=BigInt(n)-BigInt(1)
let i3=BigInt(N)/BigInt(3)
let i5=BigInt(N)/BigInt(5)
let i15=BigInt(N)/BigInt(15)
let m3=BigInt(3)*BigInt(i3)*(BigInt(i3)+BigInt(1))
let m5=BigInt(5)*BigInt(i5)*(BigInt(i5)+BigInt(1))
let m15=BigInt(15)*BigInt(i15)*(BigInt(i15)+BigInt(1))
let sum=(BigInt(m3)+BigInt(m5)-BigInt(m15))/BigInt(2)
console.log(sum.toString())
/////////////////they forgot to tell us to put code above here
}
}

add a memoization to recursive algorithm

I have written a function for partitioning a number:
var combinations = function (i) {
var mem = [];
function inner(n, r, m) {
for (var k = m; k <= n; k++) {
if (k == n) {
r.push(k);
mem[r] = 1;
return mem;
}
else {
var copy = r.slice(0);
copy.push(k);
inner(n - k, copy, k);
}
}
}
return inner(i, [], 1);
}
In second step I would like to add a memoization to this algorithm, but can't think of implementing it the right way, beause there is no return statement until the very end (when return is specified e.g. in faactorial or fibbinacci I can add the memoization).
Can anybody drive me to the right direction?
[edit]
I need this algorithm to be as fast as possible. This is a competition for a kata at codewars: link
There is a requirement it must be executed under 6000ms for input up to 330.
That's the best algorithm I can think of, except how to store the partial results.
Here's a much simpler code that works:
function nr_partitions(n) { return p(n, n); }
function p(sum,largest) {
if (largest == 0) { return 0; }
if (sum == 0) { return 1; }
if (sum < 0) { return 0; }
return p(sum, largest-1) + p(sum-largest, largest);
}
It uses a well-known recurrence, p(n,k) = p(n,k-1) + p(n-k, k), where p(n.k) denotes the number of partitions of n where the largest part is at most k (e.g. p(3, 2)=2 because we only count 1+1+1,1+2, but not 3). For k=n we get the number of all partitions of n.
Adding memozation involves storing dictionary mapping pairs (sum, largest) to p(sum, largest).
I would go along the lines of:
var cache = {};
var combinations = function (i) {
if ( cache[i] ){
return cache[i];
};
var mem = [];
function inner(n, r, m) {
for (var k = m; k <= n; k++) {
if (k == n) {
r.push(k);
mem[r] = 1;
return mem;
}
else {
var copy = r.slice(0);
copy.push(k);
inner(n - k, copy, k);
}
}
}
cache[i] = inner(i, [], 1);
return cache[i];
}
But you'll have to modify your algorithm to make use of that cache (compute the biggest terms first ?)
Depending on your other requirements, you might want to consider using underscore.js which has its own _.memoize function.
The secret of memoization is that it exploits how closures work. When you define a function inside another scope, it has access to everything in that scope. When you return that function to somewhere outside the scope, it carries references to everything it can see inside the scope.
So to implement memorization, you need to make a function that returns another function, one that does the memorization check before calling the inner one.
Your code will look something like this:
/**
* Because we'll be returning "a function that returns a function" below,
* this needs to be executed immediately so combinations() is just
* a standalone function.
*/
var combinations = (function(i) {
/**
* mem needs to stay outside the scope of your inner function.
* If it's in a closure like this, JavaScript will keep its value
* around as long as combinations still exists.
*/
var mem = [];
/**
* A memoization wrapper should return a memoized function
*/
return function(i) {
/**
* Check if mem[i] is set and return it if it has been
*/
if(mem[i] !== undefined) {
console.log('returning memoized value');
return mem[i];
}
function inner(n, r, m) {
for (var k = m; k <= n; k++) {
if (k == n) {
r.push(k);
mem[r] = 1;
return mem;
}
else {
var copy = r.slice(0);
copy.push(k);
inner(n - k, copy, k);
}
}
}
/**
* If the value needs to be computed, we can set it at the same time
* as we return it instead of putting it in a temporary variable.
*/
console.log('computed');
return mem[i] = inner(i, [], 1);
}
}()); /** <--- That's the rest of the automatic execution */
console.log(combinations(5));
console.log(combinations(5));

Fast nextafter function in JavaScript

I'm trying to iterate through all 32 bit floating point numbers in JavaScript to visually compare some methods for polynomial evaluation for accuracy. To do so, I've implemented the code shown below. Unfortunately, this code is way too slow.
Would there be any way to improve performance?
In C/C++ the equivalent code runs in a bit over a minute on my computer, whereas I haven't had the patience to see how long this code takes.
function nextFloat(f) {
// Note that this moves away from 0.0
// It will fail at +/- infinity and result in an NaN
var bitRepr = floatToBits(f);
bitRepr++;
return bitsToFloat(bitRepr);
}
function prevFloat(f) {
// Note that this moves towards 0.0
// This will fail at 0.0 and result in an NaN
var bitRepr = floatToBits(f);
bitRepr--;
return bitsToFloat(bitRepr);
}
function floatToBits(f) {
var buf = new ArrayBuffer(4);
(new Float32Array(buf))[0] = f;
return (new Uint32Array(buf))[0];
}
function bitsToFloat(b) {
var buf = new ArrayBuffer(4);
(new Uint32Array(buf))[0] = b;
return (new Float32Array(buf))[0];
}
Another method I might consider is using is multiplying the number by (1 + epsilon), though I believe that has edge cases that I would need to resolve at the bit level anyways.
If your code is synchronous you don't need to be calling new all the time, this means that you can keep your Uint32Array and Float32Array which are linked through the same buffer across all functions, for example
var obj = (function () {
var int = new Uint32Array(1),
float = new Float32Array(int.buffer);
return {
i2f: function (i) {
int[0] = i;
return float[0];
},
f2i: function (f) {
float[0] = f;
return int[0];
},
next: function () {
int[0] = int[0] + 1;
return float[0];
},
prev: function () {
int[0] = int[0] - 1;
return float[0];
}
};
}());
Something like this should work, and doesn't require allocating arrays:
function testall(f) {
var M = Math.pow(2,-126);
var x;
for (p = -1; p <= 1; p +=2) {
for (s = 0; s < 1<<23; s++) {
// subnormals (including zeros)
x = p*M*(s/(1<<23));
f(x);
}
for (b = M; b <= 2/M; b *= 2) {
for (s = 0; s < 1<<23; s++) {
// normals
x = p*b*(1+s/(1<<23));
f(x);
}
}
}
}
This will iterate through all real-valued floats (subnormals and normals). It won't handle Infs (there are only two of those, so I leave them up to you), or NaNs (as far as I know, there is no efficient way to iterate through all the NaN bit patterns in JavaScript).

How to sort and slice an array of objects

I have an array of shots. I have been able to take that array and loop through it to get all shots that occurred on hole #1 and then rearrange them in order based on "shot_number". I now need to do this for every hole and to create an array for each hole (ex: holeArray1, holeArray2). I have attempted a number of solutions to increment x but if I do I end up missing some shots that occurred on certain holes.
How can I refactor this function to create this array for every hole without just copying and pasting the code and changing the variable x myself? Thank you for your help. I know I should be able to figure this one out but am struggling.
$scope.createHoleShotsArrays = function () {
var i = 0;
var x = 1;
var holeArray = [];
var len = $scope.shots.length;
for (; i < len; i++) {
if ($scope.shots[i].attributes.hole == x) {
holeArray.push($scope.shots[i]);
holeArray.sort(function (a, b) {
if (a.attributes.shot_number > b.attributes.shot_number) {
return 1;
}
if (a.attributes.shot_number < b.attributes.shot_number) {
return -1;
}
// a must be equal to b
return 0;
});
}
}
console.log(holeArray);
};
Push the items you want into arrays, and sort them once. I don't have cases to test the code. You may modified it a little if something goes wrong.
$scope.createHoleShotsArrays = function() {
var holeArrays = [];
$scope.shots.forEach(function(shot) {
if (holeArrays.length < shot.attributes.hole) {
holeArrays[shot.attributes.hole - 1] = [];
}
holeArrays[shot.attributes.hole - 1].push(shot);
});
holeArrays.forEach(function(arr) {
arr.sort(function(a, b) {
return a.attributes.shot_number - b.attributes.shot_number;
});
});
console.log(holeArrays);
};

Call stack size within JavaScript

I want to test for large call stacks. Specifically, I want a console warning when the call stack length reaches 1000. This usually means I did something stupid, and can lead to subtle bugs.
Can I compute the call stack length within JavaScript?
Here's a function that will work in all major browsers, although it won't work in ECMAScript 5 strict mode because arguments.callee and caller have been removed in strict mode.
function getCallStackSize() {
var count = 0, fn = arguments.callee;
while ( (fn = fn.caller) ) {
count++;
}
return count;
}
Example:
function f() { g(); }
function g() { h(); }
function h() { alert(getCallStackSize()); }
f(); // Alerts 3
UPDATE 1 November 2011
In ES5 strict mode, there is simply no way to navigate the call stack. The only option left is to parse the string returned by new Error().stack, which is non-standard, not universally supported and obviously problematic, and even this may not be possible for ever.
UPDATE 13 August 2013
This method is also limited by the fact that a function that is called more than once in a single call stack (e.g. via recursion) will throw getCallStackSize() into an infinite loop (as pointed out by #Randomblue in the comments). An improved version of getCallStackSize() is below: it keeps track of functions it has seen before to avoid going into an infinite loop. However, the returned value is the number of different function objects in the callstack before encountering a repeat rather than the true size of the complete call stack. This is the best you can do, unfortunately.
var arrayContains = Array.prototype.indexOf ?
function(arr, val) {
return arr.indexOf(val) > -1;
} :
function(arr, val) {
for (var i = 0, len = arr.length; i < len; ++i) {
if (arr[i] === val) {
return true;
}
}
return false;
};
function getCallStackSize() {
var count = 0, fn = arguments.callee, functionsSeen = [fn];
while ( (fn = fn.caller) && !arrayContains(functionsSeen, fn) ) {
functionsSeen.push(fn);
count++;
}
return count;
}
You can use this module:
https://github.com/stacktracejs/stacktrace.js
Calling printStackTrace returns the stack trace inside an array, then you can check its length:
var trace = printStackTrace();
console.log(trace.length());
A different approach is measuring the available size on the stack in the top-level stack frame and then determining the used space on the stack by observing how much less space is available. In code:
function getRemainingStackSize()
{
var i = 0;
function stackSizeExplorer() {
i++;
stackSizeExplorer();
}
try {
stackSizeExplorer();
} catch (e) {
return i;
}
}
var baselineRemStackSize = getRemainingStackSize();
var largestSeenStackSize = 0;
function getStackSize()
{
var sz = baselineRemStackSize - getRemainingStackSize();
if (largestSeenStackSize < sz)
largestSeenStackSize = sz;
return sz;
}
For example:
function ackermann(m, n)
{
if (m == 0) {
console.log("Stack Size: " + getStackSize());
return n + 1;
}
if (n == 0)
return ackermann(m - 1, 1);
return ackermann(m - 1, ackermann(m, n-1));
}
function main()
{
var m, n;
for (var m = 0; m < 4; m++)
for (var n = 0; n < 5; n++)
console.log("A(" + m + ", " + n + ") = " + ackermann(m, n));
console.log("Deepest recursion: " + largestSeenStackSize + " (" +
(baselineRemStackSize-largestSeenStackSize) + " left)");
}
main();
There are of course two major downsides to this approach:
(1) determining the used up stack space is a potentially an expensive operation when the VM has a large stack size and
(2) the numbers reported are not necessarily the number of recursions, but instead are a measurement of the actual space used on the stack (of course, this can also be an advantage). I've seen auto-generated code that contains functions that use the same space on the stack per recursion as 2000 recursions of the stackSizeExplorer function above.
Note: I have only tested the code above with node.js. But I assume it would work with all VMs that use a static stack size.

Categories