algorithm

Is this implementation of Bucket-Sort considered "in-place"?

Consider the following implementation of bucket-sort: Algorithm BucketSort(S) input: Sequence S of items with integer keys in range [0,N-1] output: Sequence S sorted in nondecreasing order of keys. let B be an array of N sequences, each of which is initially empty for each item x in S do let k be the key of x ...

How to permute array into a given order with O(1) auxiliary space?

How do I implement the following OrderElements function? char chars[] = {'a', 'b', 'c', 'd', 'e'}; int want_order[] = {2, 4, 3, 0, 1}; int length = 5; OrderElements(chars, want_order, length); // chars now contains: c, e, d, a, b It's easy when you can use linear extra space, but can it be done with only constant extra space, i.e., ...

PHP: How to get Sunday and Saturday given a date input?

Hi Guys, How can I get the Sunday and Saturday of the week given a specific date? For example: input: Monday, September 28, 2009 output should be: Sunday, September 27, 2009 12:00 AM - Saturday, October 3, 2009 11:59 PM I am thinking of using the date, strtotime, mktime and time php functions. If you have a usable func...

Algorithm to find longest anagram

Let's say that we have a dictionary of about 250.000 words. Algorithm should take in 12 letters as an array or a string and find the permutation (or is it variation or combination?) that matches longest word from a dictionary. So, it is not a real permutation, as not all of the letters are used. Of course, one can always brute-force it,...

Generate unique numbers from a finite range for more than one node

I'm not even sure the following is possible, but it never hurts to ask: I have two nodes running the same application. Each machine needs a sequence generator that will give it a number between 0 and 1e6. If a node has used a number, the other node must not use it. The generator should reset every night at midnight. No number should be ...

java: implementation of topological sort, from a reputable source

I'm looking for a reputable Java implementation of a topological sort, given a directed graph of dependencies (node #7 depends on node #2, node #2 depends on note #4, etc.), that will detect the presence of a cycle so I can report an error if a cycle occurs. I assume Apache ant has to do this so I'm hoping I could just make use of it, b...

Set of numbers to a grid

I have a set of numbers which I need to put on a rectangular grid as dense as possible (=the area of a grid is minimal). For example if I have to put numbers 16, 31, and 63, I can put them into 2x2-grid 1 6 3 0. I think the best way to solve the problem for an arbitrary collection of numbers is to make some kind of net or tree which t...

What is best efficient algorithm for "Given an unsorted array of positive integers and an integer N, return N if N existed in the array or the first number that is smaller than N." Problem ?

I had this question: Given an unsorted array of positive integers and an integer N, return N if N existed in the array or the first number that is smaller than N. in an interview and wanted to know what would be the best efficient algorithm to solve it? I had given two approaches using hash and sorting array but it was not correct...

Why multiply by a prime before xoring in many GetHashCode Implementations?

I understand that multiplication by a large number before xoring should help with badly distributed operands but why should the multiplier be a prime? Related: Why should hash functions use a prime number modulus? Close, but not quite a Duplicate: Why does Javas hashCode() in String use 31 as a multiplier? ...

What is the efficient Algorithm for Solving Jigshaw Puzzle ?

Hi, Yesterday I was just playing Jigshaw Puzzle and somehow wondered what would be algorithm for solving it. As human, steps which I followed where: Separate all pieces in 3 parts, single flat edge, double flat edge and no edge at all. Separate flat edge pieces as they would be corners of image Separate single edge pieces as they w...

Transforming a NxN binary matrix to a zero matrix using minimum row and column toggles.

Hello, This is about the question i posted regarding converting one NxN binary matrix to another . The question i asked is a code-challenge problem . However, a similar question was asked at http://stackoverflow.com/questions/1310590/matrix-conversion. I went through that thread,and have gained some idea about how to go about solving th...

Design an algorithm to calculate the most user viewed pages ?

Hi, I was asked to design an algorithm to calculate most user viewed pages. I answered him that we can make use of counter but that was not an efficient algorithm. What would be more efficient algorithm to calculate the most user viewed pages. Thanks. ...

Classifying Text Based on Groups of Keywords?

I have a list of requirements for a software project, assembled from the remains of its predecessor. Each requirement should map to one or more categories. Each of the categories consists of a group of keywords. What I'm trying to do is find an algorithm that would give me a score ranking which of the categories each requirement is likel...

Write a method to sort characters of two different strings and than sort an array of those strings ?

Hi, Basic idea is to sort the strings and compare signature of strings, where signature is the alphabetically sorted string. What would be the efficient algorithm to do so ? Thanks. ...

Given a string s and an array of smaller strings, T, how to design a method to search s for each small string in T ?

Hi, Given a string s and an array of smaller strings, T, design a method to search s for each small string in T. Thanks. ...

Best way to Fingerprint and Verify html structure.

Hello there, I just want to know what is your opinion about how to fingerprint/verify html/links structure. The problem I want to solve is: fingerprint for example 10 different sites, html pages. And after some time I want to have possibility to verify them, so is, if site has been changed, links changed, verification fails, othervise...

O(log N) == O(1) - Why not?

Whenever I consider algorithms/data structures I tend to replace the log(N) parts by constants. Oh, I know log(N) diverges - but does it matter in real world applications? log(infinity) < 100 for all practical purposes. I am really curious for real world examples where this doesn't hold. To clarify: I understand O(f(N)) I am cu...

Google Code Jam 2008: Round 1A Question 3

At Google Code Jam 2008 round 1A, there is problem: Calculate last three digits before the decimal point for the number (3+sqrt(5))^n n can be big number up to 1000000. For example: if n = 2 then (3+sqrt(5))^2 = 27.4164079... answer is 027. For n = 3: (3+sqrt(5))^3 = 3**935**.73982... answer is 935. One of the solution i...

How is π calculated within sas?

just curious! but I spotted that the value of π held by SAS is in fact incorrect. for instance: data _null_; x= constant('pi') * 1000000000000000000000000000; put x= 32.; run; gives a π value of (3.)141592653589792961327005696 however - π is of course (3.)1415926535897932384626433832795 ( http://www.joyofpi.com/pi.html ) - to 31 d...

How to implement an invitation code to share resource with another user?

We want to be able to share resources inside our web application with new or other users. We want to do this by implementing an invitation code. I have seen this implemented many times before in other applications (google docs for example), where you send an invitation code to another user and that other user will have whatever access ...