Click here to Skip to main content
15,912,897 members
Home / Discussions / Algorithms
   

Algorithms

 
QuestionLine intersection Pin
abalbo24-Apr-07 4:38
abalbo24-Apr-07 4:38 
AnswerRe: Line intersection Pin
Arun.Immanuel24-Apr-07 4:56
Arun.Immanuel24-Apr-07 4:56 
QuestionThreshholding ? Pin
Ray Kinsella24-Apr-07 3:19
Ray Kinsella24-Apr-07 3:19 
AnswerRe: Threshholding ? Pin
David Crow24-Apr-07 3:39
David Crow24-Apr-07 3:39 
QuestionLoop Pin
MoustafaS21-Apr-07 9:21
MoustafaS21-Apr-07 9:21 
GeneralRe: Loop Pin
MoustafaS21-Apr-07 10:27
MoustafaS21-Apr-07 10:27 
AnswerRe: Loop Pin
Frank Kerrigan2-May-07 5:45
Frank Kerrigan2-May-07 5:45 
QuestionImage Recognition Algorithm Pin
softwaremonkey19-Apr-07 5:42
softwaremonkey19-Apr-07 5:42 
AnswerRe: Image Recognition Algorithm Pin
Luc Pattyn19-Apr-07 6:24
sitebuilderLuc Pattyn19-Apr-07 6:24 
AnswerRe: Image Recognition Algorithm Pin
Nathan Addy19-Apr-07 7:14
Nathan Addy19-Apr-07 7:14 
AnswerRe: Image Recognition Algorithm [modified] Pin
Rilhas19-May-07 9:43
Rilhas19-May-07 9:43 
Questionhelp genetic algo Pin
clemzug15-Apr-07 1:12
clemzug15-Apr-07 1:12 
AnswerRe: help genetic algo Pin
cp987615-Apr-07 2:31
cp987615-Apr-07 2:31 
GeneralRe: help genetic algo Pin
clemzug15-Apr-07 2:44
clemzug15-Apr-07 2:44 
GeneralRe: help genetic algo Pin
cp987615-Apr-07 16:01
cp987615-Apr-07 16:01 
GeneralRe: help genetic algo Pin
Tim Craig15-Apr-07 18:08
Tim Craig15-Apr-07 18:08 
QuestionAnswer for this algorithm Pin
MoustafaS13-Apr-07 4:24
MoustafaS13-Apr-07 4:24 
AnswerRe: Answer for this algorithm Pin
CPallini13-Apr-07 4:37
mveCPallini13-Apr-07 4:37 
GeneralRe: Answer for this algorithm Pin
MoustafaS13-Apr-07 4:40
MoustafaS13-Apr-07 4:40 
AnswerRe: Answer for this algorithm [modified] Pin
Leslie Sanford13-Apr-07 4:46
Leslie Sanford13-Apr-07 4:46 
LongHC wrote:
Imagine you have a array of integers, and you want to get any two numbers whose sum is 51, what you'll do?


First, determine how to break the number down into two numbers whose sum is the original number. Instead of 51, let's pick a smaller number, say 11. Here are the numbers whose sum is 11:

11 + 0 = 11
10 + 1 = 11
9 + 2 = 11
8 + 3 = 11
7 + 4 = 11
6 + 5 = 11

To find these numbers, we can start with two values: the original number and zero, call these x and y. Run a loop in which 1 is subtracted from x and 1 is added to y at each iteration. Continue the loop until x is less than y:

int x = 11;
int y = 0;
 
while(x > y)
{
    x--;
    y++;   
}


Now, we need to search for these values in our array. It would be helpful to have our array sorted first. Then at each iteration through the loop, we perform a binary search for x an y and store the indexes to the numbers in a list, if they are in the array. We'll assume that the binary search returns -1 if the number isn't in the array:

Sort(array);
 
List list;
int x = 11;
int y = 0;
 
while(x > y)
{
    int index = BinarySearch(array, x);
 
    if(index >= 0)
    {
        list.Add(index);
    }
 
    index = BinarySearch(array, y);
 
    if(index >= 0)
    {
        list.Add(index);
    }
 
    x--;
    y++;   
}


Note: this algorithm is off the top of my head. There may be better ways of doing this.

[EDIT] My algorithm doesn't take into account possible duplicates in the array. D'Oh! | :doh: [/EDIT]



-- modified at 10:54 Friday 13th April, 2007
AnswerRe: Answer for this algorithm Pin
cp987613-Apr-07 4:47
cp987613-Apr-07 4:47 
GeneralRe: Answer for this algorithm Pin
MoustafaS13-Apr-07 4:51
MoustafaS13-Apr-07 4:51 
GeneralRe: Answer for this algorithm Pin
cp987613-Apr-07 4:59
cp987613-Apr-07 4:59 
GeneralRe: Answer for this algorithm Pin
MoustafaS13-Apr-07 5:02
MoustafaS13-Apr-07 5:02 
QuestionSkip List Indexing Pin
Leslie Sanford12-Apr-07 5:55
Leslie Sanford12-Apr-07 5:55 

General General    News News    Suggestion Suggestion    Question Question    Bug Bug    Answer Answer    Joke Joke    Praise Praise    Rant Rant    Admin Admin   

Use Ctrl+Left/Right to switch messages, Ctrl+Up/Down to switch threads, Ctrl+Shift+Left/Right to switch pages.