algorithm

Determine whether or not there exist two elements in Set S whose sum is exactly x - correct solution?

Taken from Introduction to Algorithms Describe a Θ(n lg n)-time algorithm that, given a set S of n integers and another integer x, determines whether or not there exist two elements in S whose sum is exactly x. This is my best solution implemented in Java so far: public static boolean test(int[] a, int val) { merge...

Find the longest repeating string and the number of times it repeats in a given string

For example, given string "abc fghi bc kl abcd lkm abcdefg", the function should return string "abcd" and the count of 2. A O(n^2) solution seems easy but I am looking for a better solution. Edited: If nothing better than O(n^2) is possible than which approach would be best performance wise. ...

Map rendering : from data to image tile :Good practice/Sample codes/Tutorial

Hi folks, I'm working on a iPhone offline map project. So i would like to generate the tiles directly in the iPhone, and I need to build a fast and efficient algorithm to render the tiles. Do you know some resources for that ? (I know some frameworks exist, like mapnik, but i need something as simple as possible, and i'll implement the...

Self-indexing (and traditional indexing) algorithms - Implementations and advice to share?

As part of a research project I'm currently looking for open-source implementations of self-indexing algorithms, i.e. a compressed form of the traditional inverted index yielding nice characteristics such as faster lookup and/or less consumed space. Do you know of any open-source implementations of self-indexing algorithms? Do you have ...

How to get unrotated display object width/height of a rotated display object?

If I create a rectangle with 100px width and 100px height and then rotate it, the size of the element's "box" will have increased. With 45 rotation, the size becomes about 143x143 (from 100x100). Doing sometimes like cos(angleRad) * currentWidth seems to work for 45 rotation, but for other bigger angles it doesn't. At the moment I am ...

How to code this algorithm in PHP ?

I have a complex algorithm that I try to code in PHP but it's a bit complicated for a beginner like me. So I need your help guys I have an array of unknown numbers starting fron t1 up to tn. I don't know its length actually.like (t1,t2,t3,t4,.....,tn) And I want to test a condition on the first elemnt if not, on the last element if no...

"Crypt Kicker Problem" (Programming Challeneges)

Hello, "Programming Challenges (The Programming Contest Training Manual)" is probably one of the nicest exercises book on algorithms. I've resolved the first 11 exercises, but now I am stuck with "Crypt Kicker" problem: Crypt Kicker A common but insecure method of encrypting text is to permute the letters of the alphabet. In o...

How can i extract rectangles from a rectangle intersection.

Having a rectangle (A) and intersecting it with another rectangle (B), how could I extract the other rectangles created through that intersection (C,D,E & F)? AAAAAAAAAAAAAA CCCFFFFDDDDDDD AAABBBBAAAAAAA CCCBBBBDDDDDDD AAABBBBAAAAAAA -> CCCBBBBDDDDDDD AAAAAAAAAAAAAA CCCEEEEDDDDDDD AAAAAAAAAAAAAA CCCEEEEDDDDDDD And could th...

Algorithm for creating a school timetable.

Hello all. I've been wondering if there are known solutions for algorithm of creating a school timetable. Basically, it's about optimizing "hour-dispersion" (both in teachers and classes case) for given class-subject-teacher associations. We can assume that we have sets of classes, lesson subjects and teachers associated with each oth...

C++ standard/de facto STL algorithm wrappers.

hello Are there any standard/de facto standard (boost) wrappers around standard algorithms which work with containers defining begin and end. Let me show you what I mean with the code: // instead of specifying begin and end std::copy(vector.begin(), vector.end(), output); // write as xxx::copy(vector, output); I know it can be writte...

How to calculate elapsed time from now with Joda Time?

I need to calculate the time elapsed from one specific date till now and display it with the same format as StackOverflow questions, i.e.: 15s ago 2min ago 2hours ago 2days ago 25th Dec 08 Do you know how to achieve it with the Java Joda Time library? Is there a helper method out there that already implements it, or should I write the...

Algorithm to find a number and its square in an array

Hello I have an array of integers. I need an O(n) algorithm to find if the array contains a number and its square; one pair is sufficient. I tried to do it myself, but I have only managed to find a solution in O(n2). Thanks Edit: like I was asked: I thought about using counting sort, but the memory usage is too big. ...

Algorithm to keep collection sorted while inserting in the middle

Let's say I have a large collection of elements. Each element has a "position" field, which is a positive integer. No two elements have the same value for the field "position". The only supported operation of the collection is: addElement(newElement, positionAfterElement), where: - newElement is the new element to be added (its posit...

What is the name of this Checksum Algorithm?

Does anyone know the formal / official name of the checksum algorithm used in the following functions? function Checksum($number, $encode = true) { if ($encode === true) { $result = 0; $number = str_split($number, 1); foreach ($number as $value) { $result = ($result + ord($value) - ...

Creating a formula for calculating device "health" based on uptime/reboots

I have a few hundred network devices that check in to our server every 10 minutes. Each device has an embedded clock, counting the seconds and reporting elapsed seconds on every check in to the server. So, sample data set looks like CheckinTime Runtime 2010-01-01 02:15:00.000 101500 2010-01-01 02:25:00.000 102100 2010-...

Link Tree nodes at each level

Given a binary tree, how would you join the nodes at each level, left to right. Say there are 5 nodes at level three, link all of them from left to right. I don't need anybody to write code for this.. but just an efficient algorithm. Thanks ...

Transact-SQL: Detect Date boundaries across multiple rows.

I am creating a stored procedure that produces reports based on data in a SQL database and stores these reports in a separate database. The data being reported is the total time over a date range that a motor was running. I want to be able to detect if the timeframe I am reporting overlaps previous reports based on date times. For ins...

Memorizing important algorithms such as binary search

Do you memorize algorithms such as binary search / quick sort / whatever. If so, do you have any tricks for doing so? ...

Computing the null space of a matrix as fast as possible

I need to compute the nullspace of several thousand small matrices (8x9, not 4x3 as I wrote previously) in parallel (CUDA). All references point to SVD but the algorithm in numerical recipes seems very expensive, and gives me lots of things other than the null space that I don't really need. Is Gaussian elimination really not an option...

What are some algorithms that will allow me to simulate planetary physics?

I'm interested in doing a "Solar System" simulator that will allow me to simulate the rotational and gravitational forces of planets and stars. I'd like to be able to say, simulate our solar system, and simulate it across varying speeds (ie, watch Earth and other planets rotate around the sun across days, years, etc). I'd like to be ab...