Category "algorithm"

Find the closest integer with same weight O(1)

I am solving this problem: The count of ones in binary representation of integer number is called the weight of that number. The following algorithm finds the

Grouping/Bucketing latitude and longitude

This is more of a logical problem than the technical one. So request you guys to please not to flag it. I want to write a method in python, that takes two para

Detecting loops in tree structures (graphs)

I'm writing a library which is configured using a recursive structure. For the sake of this discussion I'm calling these graph structures a "tree" since ther

3-PARTITION problem

here is another dynamic programming question (Vazirani ch6) Consider the following 3-PARTITION problem. Given integers a1...an, we want to determine whether it

How to merge 3 sorted arrays into 1 sorted array in Big-O(N) time?

Trying to merge 3 arrays into one so that the final array is in order. Given int[] a = {1,3}; int[] b = {2,4}; int[] c = {1,5}; Merge the arrays so that t

Algorithm for pow(float, float)

I need an efficent algorithm to do math::power function between two floats, do you have any idea how to do this, (i need the algorithm not to use the function

Determining if a number is prime

I have perused a lot of code on this topic, but most of them produce the numbers that are prime all the way up to the input number. However, I need code which

Quicksort with Python

I am totally new to python and I am trying to implement quicksort in it. Could someone please help me complete my code? I do not know how to concatenate the th

Testing whether a polygon is simple or complex

For a polygon defined as a sequence of (x,y) points, how can I detect whether it is complex or not? A complex polygon has intersections with itself, as shown:

What string similarity algorithms are there?

I need to compare 2 strings and calculate their similarity, to filter down a list of the most similar strings. e.g. searching for "dog" would return dog doggone

Optimizing Fixed-Point Sqrt

I made what I think is a good fixed-point square root algorithm: template<int64_t M, int64_t P> typename enable_if<M + P == 32, FixedPoint<M, P>

Can we construct a BST from a pre-order traversal simply by inserting the elements in preorder traversal in sequence into an empty tree?

Given a preorder traversal of a BST.I have to construct the BST. Can I construct the BST from the preorder traversal simply by creating a empty BST and then ins

Longest path on a grid, without revisiting grid cells

I am looking for an algorithm to fidn the longest path between two points on a grid, with the added restriction that you cannot revisit a cell on the grid. (Als

Can hash tables really be O(1)?

It seems to be common knowledge that hash tables can achieve O(1), but that has never made sense to me. Can someone please explain it? Here are two situations

Can hash tables really be O(1)?

It seems to be common knowledge that hash tables can achieve O(1), but that has never made sense to me. Can someone please explain it? Here are two situations