algorithm

Why can Linq operations be faster than a normal loop?

A friend and I were a bit perplexed during a programming discussion today. As an example we created a fictive problem of having a List<int> of n random integers (typically 1.000.000), and wanted to create a function that returned the set of all integers that there were more than one of. Pretty straightforward stuff. We created one linq s...

given a number p , find two elements in array whose product = P.

I am looking for solution for : Given a array and a number P , find two numbers in array whose product equals P. Looking for solution better than O(n*2) . I am okay with using extra space or other datastructure .Any help is appreciated ? ...

Scaling down image to a fixed dimension of 135x135

Hey Guys, I have to scale down image of any dimension to a fixed dimension of 135x135, most imp thing I have to maintain good quality of scaled down image. I'm not much familiar with Image Processing algos. Can you guys suggest me any algorithm. ...

System Time change detection on linux

Hi, I was reading about the licensing of software and one question that came to my mind is that "how software detect the change in system time and block themselves if someone changes system time?". Since there is no other reference available(provided we don't have internet connection, otherwise we can use time servers as reference), how...

Using PHP, randomly pair up group of items, not pairing any with itself, no direct pairings

Assume you have a set of items in an array. A, B, C, D, E, F, G, H Using PHP, how would you randomly pair the letters together without pairing them with a duplicate of themselves? Such as this: A->pairedLetter = G B->pairedLetter = C C->pairedLetter = E D->pairedLetter = A E->pairedLetter = B F->pairedLetter = D G->pairedLette...

Iterative Algorithm for Red-Black Tree

Can anyone please suggest me any pointer to an iterative algorithm for insertion and deletion into a Red-Black Tree? All the algorithms available in .Net/C# are based on recursion, which I can't trust for handling very large number of data (hence large number of recursion depth for insertion/deletion). Does anybody have one based on iter...

SQL querying over multiple tables

I don't have much experience in SQL so I think this is not a dumb question. I have 2 tables like this. A .. G are the members of a hierarchy. Now my requirement is as follows. I need to filter out the members which has status = 0 from Members table. But, If the selected set contains children which has a parent with st...

Give two strings s1 and s2 , give an algorithm to find all characters from S1 which are also in S2.

Example : S1 : abcde S2: cdef Answer : cde ...

How to stay under GAE quotas? Algorithm design

Hi there I have a function in my app that uses a lot of resources, and takes time to execute. This is normal and control, however I often get errors due to GAE limit of 30 secs/request. My function takes the argument and returns several results one after the other, decreasing the size of the argument (a unicode string) Summary: def m...

Smoothing values over time: moving average or something better?

I'm coding something at the moment where I'm taking a bunch of values over time from a hardware compass. This compass is very accurate and updates very often, with the result that if it jiggles slightly, I end up with the odd value that's wildly inconsistent with its neighbours. I want to smooth those values out. Having done some readin...

Insert and search quickly

Hi, What is the best data-structure for doing following operation quickly Insert Find. Thanks Avinash ...

Algorithm to optimize # threads used in a calculation

I'm performing an operation, lets call it CalculateSomeData. CalculateSomeData operates in successive "generations", numbered 1..x. The number of generations in the entire run is fixed by the input parameters to CalculateSomeData and is known a priori. A single generation takes anywhere from 30 minutes to 2 hours to complete. Some of...

C# Algorithm that Re-arranges Chars in a String

I would like a C# algorithm that re-arranges the chars in a string that is dynamic in length. Having trouble finding one and I know there has to be one out there. The algorithm has to realign elements to form new strings in all possible combinations. For instance, "cat" would produce the following: cat cta tca tac act atc ...

Finding minimum subset of objects with atributes.

I have algorithmic problem. I don't know how to solve it. Maybe someone can help me? I have objects. Each object has the same features. It could be illustrated in table: Feature1 Feature2 Feature3 Feature4 Object1 1 0 1 1 Object2 0 0 0 ...

Programming Pearls: finding sub-array with max sum

Hi, I was reading "programming pearls" book and stuck in some place. The (best) original solution for the problem (finding sub-array with max sum) is: maxsofar = О maxendinghere = О for i = [0. n) { maxendinghere = max(maxendinghere + x[i], 0) maxsofar = max(maxsofar, maxendinghere) } Then the problems is changed as foll...

Where can I learn more about the Google search "did you mean" algorithm?

Possible Duplicate: How do you implement a Did you mean? I am writing an application where I require functionality similar to Google's "did you mean?" feature used by their search engine: Is there source code available for such a thing or where can I find articles that would help me to build my own? ...

C# Diff Algorithm for Text

I'm looking for a diff algorithm that will produce results like SO's edit revisions page. I've more or less just started looking and I'm not opposed to doing it myself but I don't need to reinvent the wheel. I'll be using C# 4.0. I'll basically have two strings, and old one and a new one. I want to know what has changed in the new ...

Find the x smallest integers in a list of length n

You have a list of n integers and you want the x smallest. For example, x_smallest([1, 2, 5, 4, 3], 3) should return [1, 2, 3]. I'll vote up unique runtimes within reason and will give the green check to the best runtime. I'll start with O(n * x): Create an array of length x. Iterate through the list x times, each time pulling out th...

Weighted Average with a negative number

Let's say you need to display a graphical representation of how well a baseball team is doing (my software problem is not related to sports but...). Let say you chose that 25% of a gauge is related to the percentage of batters who hit during the first time at bat. The next 25% related to the percentage of pitchers on the team who threw ...

Applying a Logarithm to Navigate a Tree

I had once known of a way to use logarithms to move from one leaf of a tree to the next "in-order" leaf of a tree. I think it involved taking a position value (rank?) of the "current" leaf and using it as a seed for a fresh traversal from the root down to the new target leaf - all the way using a log function test to determine whether t...