algorithm

How can I efficiently determine if two lists contain elements ordered in the same way?

I have two ordered lists of the same element type, each list having at most one element of each value (say ints and unique numbers), but otherwise with no restrictions (one may be a subset of the other, they may be completely disjunct, or share some elements but not others). How do I efficiently determine if A is ordering any two items ...

Order Integers In Ascending and Descending Orders

I'm working with Objective-C, but probably it doesn't matter the programming language for this. So basically I have an array, with say, the integers 12, 5, and 17, and I want to be able to pull out the largest number, or the smallest, or second smallest, etc. Basically I want to be able to sort them into ascending or decending order so...

Can anyone explain how this code generates combinations?

I found some Java code for generating combinations on, but I can't understand what it is doing as it does some strange operations with bits. import java.util.Collections; import java.util.LinkedList; public class Comb{ public static void main(String[] args){ System.out.println(comb(3,5)); } public static Strin...

What does this code do?

function mystery($y, $m, $d) { $a = 0; $b = 0; $c = 0; if($m < 3) { $a = $m + 10; $b = ($y-1) % 100; $c = ($y-1) / 100; } else { $a = $m - 2; $b = $y % 100; $c = $y / 100; } $w = (700 + (((26*$a)-2)/10)+$d+$b+$b/4+$c/4-(2*$c))%7; echo $w; } One of my tutorial questions asks what the functio...

Name comparison algorithm

To check if a name is inside an anti-terrorism list. In addition of the given name, also search for similar names (possible aliases). Example: given name => Bin Laden alert! given name => Ben Larden mhm.. suspicious name, matchs at xx% with Bin Laden How can I do this? using PHP names are 100% correct, since they are from offic...

Do I need to implement a b-tree search for this?

I have an array of integers, which could run into the hundreds of thousands (or more), sorted numerically ascending since that's how they were originally stacked. I need to be able to query the array to get the index of its first occurrence of a number >= some input, as efficiently as possible. The only way I would know how to do this ...

How to split a string into as few palindromes as possible ?

This is an interview question: "You're given a string, and you want to split it into as few strings as possible such that each string is a palindrome". (I guess a one char string is considered a palindrome, i.e. "abc" is split into "a", "b", "c".) How would you answer it? ...

How to compute de Bruijn sequences for non-power-of-two-sized alphabets?

I'm trying to compute de Bruijn sequences for alphabets which have a number of characters which is not a power of two. For alphabets with a 2^k characters, calculating de Bruijn sequences is easy: There are several simple rules, such as "Prefer Ones" and "Prefer Opposites" which work for generating B(2,n). B(2^k,n) is exactly the same a...

Searching location in a sentence

Hi, I'm working on a location extraction algorithm but haven't achieved anything considerable yet. For example in this sentence Riders on the B and Q lines will get some relief from construction as stations reopen, and a major project will soon begin at the Dyckman Street station. "Dyckman Street" is location information. How we e...

Double.GetHashCode algorithm or override

hello i have an application project that both managed and unmanaged code runs and i need to use the same algorithm for hashing double values in both systems. so either i will override System.Double.GetHashCode() or use its algorithm in c++ code. i could not find the double.gethashcode algorithm and decided to override the function. but ...

calculate prefix sum

I have following code to accomplish prefix sum task: #include <iostream> #include<math.h> using namespace std; int Log(int n){ int count=1; while (n!=0){ n>>=1; count++; } return count; } int main(){ int x[16]={39,21,20,50,13,18,2,33,49,39,47,15,30,47,24,1}; int n=size...

Lowest Common Ancestor for a given pair of nodes

Hi Guys, I am stumbled by this question. The following is my approach: Lets say the two nodes are node1 and node2 For any node (lets say node1), find the path from Root to node1 and store it in an HashMap For the other node node2, find the path from root to node2, and while traversing back, check if any of the nodes are ...

Hakulinen Method

Hi, does anybody know if there is a nice example of "Hakulinen method" (http://mayoresearch.mayo.edu/mayo/research/biostat/upload/63.pdf, some math is described here) for calculating relative survival? Preferably source code in any language. ...

What logic is best to use for counting articles read by the same person over time?

I'm planning on storing the number of times an article / page has been viewed in a database. This is so that I can have a list of "most popular posts / articles" in Wordpress. This was a good thread for similar question: http://stackoverflow.com/questions/943967/how-view-count-is-best-implemented My question is: A person may view an ar...

How to create an AVL Tree from ArrayList of values in O(n) time?

My assignment is to create an AVL Tree from a sorted array list of values in O(n) time where n is the number of values I have been working on this but I cannot get O(n) time, the best I can get is O(nlog(n)) My problem is that every time a node that causes the tree to be unbalanced is inserted, I have to do another loop to find the nod...

Implement a better Strategy pattern

I have a Chequing account and a Saving account. I'm exploring how to implement the withdraw method using a Strategy pattern. Currently, the Chequing and Saving account both inherit from Account. For Saving account, withdrawals should not cause the balance to dip below 100 dollars. With the Chequing account, withdrawals must include ...

How to get the smallest list of duplicate items from list of lists ?

Hi everyone, I'm a Python newbie, I worked with list for 2 months and I have some questions. I have some list and they have duplicate items. I can get duplicate items between 2 lists, now I want the number of lists and the deepness increased like this example: http://i219.photobucket.com/albums/cc213/DoSvn/example.png. I want to get pare...

fair task queue for j2ee

I intend to make a service where in people could submit tasks(specifically transcoding tasks) to the system and they should get serviced soon but at the same time it should not starve anyone else, ie it must be fair. If a person submits 2000 tasks the system should not cater to only him all the time but instead do a round robin or someth...

Sorting huge Number of Integers from hard disk

Given 100 GB integer Data on Hard Disk with RAM amounting to 2 GB, how to sort the integers with minimal disk operation. Here fetching one number from disk is considered as one disk operation( though in reality a block of data can be fetched). We can use additional space on the disk for temporary storage and no need to consider the oper...

Programming two trains to intersect without positional data or communication (logic puzzle)

A helicopter drops two trains, each on a parachute, onto a straight infinite railway line. There is an undefined distance between the two trains. Each faces the same direction, and upon landing, the parachute attached to each train falls to the ground next to the train and detaches. Each train has a microchip that controls its motion....