algorithm

Is this a bad version of the Merge Sort algorithm?

merge1(int low, int high, int S[], U[]) { int k = (high - low + 1)/2 for q (from low to high) U[q] = S[q] int j = low int p = low int i = low + k while (j <= low + k - 1) and (i <= high) do { if ( U[j] <= U[i] ) { S[p] := U[j] j := j+1 } else ...

O(log n) algorithm to find the element having rank i in union of pre-sorted lists

Given two sorted lists, each containing n real numbers, is there a O(log n) time algorithm to compute the element of rank i (where i coresponds to index in increasing order) in the union of the two lists, assuming the elements of the two lists are distinct? EDIT: @BEN: This i s what I have been doing , but I am still not getting it. ...

Find a repeated numbers out of 3 boxes

I have 3 boxes, each box contain 10 piece of numbered paper (1 - 10) but there is a number the same in all 3 boxes eg: box1 has number 4 and box2 has number 4 and box3 also has number 4. How to find that repeated number in java with an efficient/fastest way possible? ...

Depth First Search Basics

I'm trying to improve my current algorithm for the 8 Queens problem, and this is the first time I'm really dealing with algorithm design/algorithms. I want to implement a depth-first search combined with a permutation of the different Y values described here: http://en.wikipedia.org/wiki/Eight_queens_puzzle#The_eight_queens_puzzle_as_an_...

How do I pick the most beneficial combination of items from a set of items?

I'm designing a piece of a game where the AI needs to determine which combination of armor will give the best overall stat bonus to the character. Each character will have about 10 stats, of which only 3-4 are important, and of those important ones, a few will be more important than the others. Armor will also give a boost to 1 or all s...

Java Algorithm HmacSHA256 not available

Hi, I am trying an encryption-decryption code in java. When I am trying to run it with JDK and code in class with main method it is running fine. But when I am trying to run same code in Tomcat, tomcat is throwing exception: java.security.NoSuchAlgorithmException: Algorithm HmacSHA256 not available. Can anybody guide me to proper dire...

How to find rectangle of difference between two images

I have two images the same size. What is the best way to find the rectangle in which they differ. Obviously I could go through the image 4 times in different directions, but i'm wondering if there's an easier way. Example: ...

little oh notation as the limit of n goes to infinity

Hi all, I'm just trying to understand how in little o notation this is true: f(n)/g(n) as n goes to infinity = 0? Can someone explain that to me? I do get the idea that f(n) = o(g(n)) means that f(n) grows no faster then cg(n) for all constants c > 0. I just don't get the bit in bold above. ...

Merging k sorted linked lists - analysis

Hi! I am thinking about different solutions for one problem. Assume we have K sorted linked lists and we are merging them into one. All these lists together have N elements. The well known solution is to use priority queue and pop / push first elements from every lists and I can understand why it takes O(N log K) time. But let's take ...

Effective way to calculate a similarity percentage between data sets

I am currently working with User objects -- each of which have many Goal objects. The Goal objects are not User specific, that is, Users can share the same Goal. I am attempting to fashion a way to calculate a "similarity percentage" between two Users... (i.e., taking into account how many Goals they share as well as how many Goals they ...

Algorithm To Select Most Popular Places from Database

We have a website that contains a database of places. For each place our users are able to take one of the follow actions which we record: VIEW - View it's profile RATING - Rate it on a scale of 1-5 stars REVIEW - Review it COMPLETED - Mark that they've been there WISH LIST - Mark that they want to go there FAVORITE - Mark that it's on...

Find common nodes from two linked lists using recursion

I have to write a method that returns a linked list with all the nodes that are common to two linked lists using recursion, without loops. For example, first list is 2 -> 5 -> 7 -> 10 second list is 2 -> 4 -> 8 -> 10 the list that would be returned is 2 -> 10 I am getting nowhere with this.. What I have been think of was to check ...

Flag bit computation and detection

Hi all, In some code I'm working on I should take care of ten independent parameters which can take one of two values (0 or 1). This creates 2^10 distinct conditions. Some of the conditions never occur and can be left out, but those which do occur are still A LOT and making a switch to handle all cases is insane. I want to use 10 if ...

Subset sum problem

I'm having a problem with counting which is continuation of this question. I am not really a math person so it's really hard for me to figure out this subset sum problem which was suggested as resolution. I'm having 4 ArrayList in which I hold data: alId, alTransaction, alNumber, alPrice Type | Transaction | Number | Price ...

Stopping Backtracking

Is there any way in C/C++ to stop a backtracking algorithm after finding the first solution without exiting the program. I want my function to immediately exit the function,not to quit every level of recurrsion one by one stating return. ...

Implement a RSA algorithm in Java

Hello everyone. I want to implement a RSA algorithm to encrypt an image (byte[]). To generate my two keys I used this piece of code : KeyPairGenerator keygen = KeyPairGenerator.getInstance("RSA"); keygen.initialize(512); keyPair = keygen.generateKeyPair(); Once public and private key are generated, I would like to show them to the us...

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

I can't solve it: You are given 8 integers: A, B, C representing a line on a plane with equation A*x + B*y = C a, b, c representing another line x, y representing a point on a plane The two lines are not parallel therefore divide plane into 4 pieces. Point (x, y) lies inside of one these pieces. Problem: Write a fast algorithm tha...

Python script to calculate aded combinations from a dictionary

I am trying to write a script that will take a dictionary of items, each containing properties of values from 0 - 10, and add the various elements to select which combination of items achieve the desired totals. I also need the script to do this, using only items that have the same "slot" in common. For example: item_list = { 'item_1...

Average performance of binary search algorithm?

http://en.wikipedia.org/wiki/Binary_search_algorithm#Average_performance BinarySearch(int A[], int value, int low, int high) { int mid; if (high < low) return -1; mid = (low + high) / 2; if (A[mid] > value) return BinarySearch(A, value, low, mid-1); else if (A[mid] < value) return BinarySearch...

Time complexity of a recursive algorithm

How can I calculate the time complexity of a recursive algorithm? int pow1(int x,int n) { if(n==0){ return 1; } else{ return x * pow1(x, n-1); } } int pow2(int x,int n) { if(n==0){ return 1; } else if(n&1){ int p = pow2(x, (n-1)/2) return x * p * p; } else { ...