[Solved] The code isn’t efficient

There are a few tricks you can use to speed this up. The nth triangle number is n*(n+1)/2. For all integers n, n and n+1 are co-prime. This means that the number of divisors of n*(n+1) is the number of divisors n multiplied by the number of divisors of n+1. For an even number k, … Read more

[Solved] High-performance merging of ordered sets

This is not going to be the fastest data structure & algorithm for your particular purpose I guess, but it may be fast enough. Test it yourself. Note that a std::forward_list or even a std::vector might be faster depending on the actual scenario (-> constant factors in big-O-notation). tmyklebu mentioned another approach in the comments: … Read more

[Solved] Split arrays into multiple arrays based on string characters [closed]

This is a compact version by using a closure for the sorted character array. var array = [‘car’, ‘incl’, ‘arc’, ‘linc’, ‘rca’, ‘icnl’, ‘meta’, ‘tame’], result = Object.values( array.reduce( (r, s) => (a => ((r[a] = r[a] || []).push(s), r))([…s].sort()), {} ) ); console.log(result); 1 solved Split arrays into multiple arrays based on string characters … Read more

[Solved] Need to implement an algorithm to obtain the average value of the fields of the graph structure( graph tree)

You could create recursive function with for…in loop and first calculate total sum and total number of nodes and then use those 2 values to calculate average. var graph_structure = {“val”:74,”child”:[{“val”:17,”child”:[{“val”:34,”child”:[{“val”:34,”child”:[{“val”:65,”child”:[{“val”:28,”child”:[{“val”:85},{“val”:30,”child”:[{“val”:68},{“val”:10,”child”:[{“val”:100,”child”:[{“val”:21,”child”:[{“val”:21},{“val”:64}]},{“val”:86}]}]}]}]}]},{“val”:22,”child”:[{“val”:17,”child”:[{“val”:65}]}]}]},{“val”:53,”child”:[{“val”:3,”child”:[{“val”:98,”child”:[{“val”:90,”child”:[{“val”:76,”child”:[{“val”:87,”child”:[{“val”:52,”child”:[{“val”:56}]}]},{“val”:47},{“val”:40,”child”:[{“val”:80,”child”:[{“val”:34}]},{“val”:23,”child”:[{“val”:47},{“val”:92}]},{“val”:98,”child”:[{“val”:89},{“val”:16},{“val”:10}]}]}]}]},{“val”:35,”child”:[{“val”:89,”child”:[{“val”:76,”child”:[{“val”:50,”child”:[{“val”:51},{“val”:90}]},{“val”:69,”child”:[{“val”:93},{“val”:98},{“val”:62}]}]}]}]}]}]}]}]}]},{“val”:98,”child”:[{“val”:85},{“val”:85,”child”:[{“val”:58,”child”:[{“val”:81,”child”:[{“val”:36,”child”:[{“val”:45,”child”:[{“val”:96,”child”:[{“val”:15,”child”:[{“val”:11,”child”:[{“val”:96}]}]},{“val”:48,”child”:[{“val”:4,”child”:[{“val”:74},{“val”:1}]},{“val”:7}]}]},{“val”:84,”child”:[{“val”:9},{“val”:81,”child”:[{“val”:10,”child”:[{“val”:67}]}]}]}]},{“val”:85,”child”:[{“val”:53},{“val”:7,”child”:[{“val”:47,”child”:[{“val”:74,”child”:[{“val”:30},{“val”:7},{“val”:12}]},{“val”:22}]},{“val”:56,”child”:[{“val”:51,”child”:[{“val”:45}]},{“val”:54,”child”:[{“val”:20},{“val”:62}]}]}]}]}]}]}]},{“val”:62,”child”:[{“val”:36,”child”:[{“val”:39,”child”:[{“val”:20}]},{“val”:10,”child”:[{“val”:91,”child”:[{“val”:81,”child”:[{“val”:59,”child”:[{“val”:19,”child”:[{“val”:59},{“val”:16}]},{“val”:35,”child”:[{“val”:30}]},{“val”:6,”child”:[{“val”:27}]}]},{“val”:89,”child”:[{“val”:60,”child”:[{“val”:59}]}]}]}]}]}]}]}]}]},{“val”:8,”child”:[{“val”:56,”child”:[{“val”:55,”child”:[{“val”:41,”child”:[{“val”:17,”child”:[{“val”:15,”child”:[{“val”:40,”child”:[{“val”:55,”child”:[{“val”:50},{“val”:99,”child”:[{“val”:86},{“val”:90}]}]}]},{“val”:85,”child”:[{“val”:36,”child”:[{“val”:39,”child”:[{“val”:45}]}]}]},{“val”:78,”child”:[{“val”:24,”child”:[{“val”:93,”child”:[{“val”:8}]},{“val”:26,”child”:[{“val”:5}]}]},{“val”:36}]}]},{“val”:13}]}]},{“val”:10,”child”:[{“val”:0,”child”:[{“val”:77,”child”:[{“val”:46,”child”:[{“val”:72,”child”:[{“val”:17,”child”:[{“val”:10},{“val”:67}]},{“val”:48},{“val”:60}]},{“val”:98,”child”:[{“val”:12,”child”:[{“val”:61},{“val”:27}]}]}]}]}]}]}]}]}]}]} function calc(data) { return (function repeat(data, res) { for(let i in data) { if(data.val) { res.nodes++; res.total += data.val if(!res.min) res.min = data else … Read more

[Solved] Ball Lottery Algorithm [closed]

You don’t need to store ranges, only probabilities, or in your case the number of balls they have. Player 1 has 60 balls. You can store that directly as an int 60. Player 2 has 20, so store 20. And so on. Then count the total number of balls (once, or hardcode it if it … Read more

[Solved] Having trouble implementing a nested class comparator

You should provide inner class instance into Arrays.sort to compare points from view of parent class instance. To do it you should not create new instance in main() function, but get it from Point instance. So, in main function you should use something like this: Point pivot; … // set up pivot point here Arrays.sort(myPoints, … Read more

[Solved] Modulus of a very large number

BigInteger has the divideAndRemainder(…) method, one that returns a BigInteger array, the first item the division result, and the second, the remainder (which is what mod really does in Java). Update Including comment by Mark Dickinson to other answer: There’s a much simpler linear-time algorithm: set acc to 0, then for each digit d in … Read more