How to get all 24 rotations of a 3-dimensional array?

A die (half a pair of dice) is handy for observing the 24 different orientations, and can suggest operation sequences to generate them. You will see that any of six faces can be uppermost, and the sides below can be rotated into four different cardinal directions. Let us denote two operations: “turn” and “roll”, where … Read more

Best way to retrieve K largest elements from large unsorted arrays?

Another way of solving this is using Quickselect. This should give you a total average time complexity of O(n). Consider this: Find the kth largest number x using Quickselect (O(n)) Iterate through the array again (or just through the right-side partition) (O(n)) and save all elements ≥ x Return your saved elements (If there are … Read more

How can I calculate what date Catholic Good Friday falls on, given a year?

Here’s a great article that should help you build your algorithm http://www.codeproject.com/KB/datetime/christianholidays.aspx Based on this example, you should be able to write: DateTime goodFriday = EasterSunday(DateTime.Now.Year).AddDays(-2); Full Example: public static DateTime EasterSunday(int year) { int day = 0; int month = 0; int g = year % 19; int c = year / 100; int … Read more

How does a ‘diff’ algorithm work, e.g. in VCDIFF and DiffMerge? [closed]

An O(ND) Difference Algorithm and its Variations (1986, Eugene W. Myers) is a fantastic paper and you may want to start there. It includes pseudo-code and a nice visualization of the graph traversals involved in doing the diff. Section 4 of the paper introduces some refinements to the algorithm that make it very effective. Successfully … Read more

Given two lines on a plane, how to find integer points closest to their intersection?

alt text http://imagebin.ca/img/yhFOHb.png Diagram After you find intersection of lines L1:Ax+By=C and L2:ax+by=c i.e. point A(x1,y1). Define two more lines y = ceil(y1) and y = floor(y1) parallel to X-axis and find their intersection with L1 and L2 i.e. Points B(x2,y2) and C(x3,y3). Then point you require is D or E whichever is closer to … Read more