Click here to Skip to main content
15,888,968 members
Home / Discussions / Algorithms
   

Algorithms

 
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 
AnswerRe: Skip List Indexing Pin
Luc Pattyn12-Apr-07 9:30
sitebuilderLuc Pattyn12-Apr-07 9:30 
GeneralRe: Skip List Indexing Pin
Leslie Sanford12-Apr-07 10:44
Leslie Sanford12-Apr-07 10:44 
QuestionThe Perfect Circle Pin
Bassam Abdul-Baki10-Apr-07 4:15
professionalBassam Abdul-Baki10-Apr-07 4:15 
AnswerRe: The Perfect Circle Pin
Paddy Boyd12-Apr-07 2:57
Paddy Boyd12-Apr-07 2:57 
QuestionLp,BP,HP filters design in C# Pin
Keleistein9-Apr-07 20:44
Keleistein9-Apr-07 20:44 
AnswerRe: Lp,BP,HP filters design in C# Pin
Roger Wright10-Apr-07 8:21
professionalRoger Wright10-Apr-07 8:21 
GeneralRe: Lp,BP,HP filters design in C# Pin
cp987611-Apr-07 1:46
cp987611-Apr-07 1:46 
Questionsome probability problem ? Pin
codeprojecter_8-Apr-07 23:56
codeprojecter_8-Apr-07 23:56 
AnswerRe: some probability problem ? Pin
cp98769-Apr-07 2:34
cp98769-Apr-07 2:34 
Questionconvert arrays of number to an integer Pin
Khodadad Pakdamans8-Apr-07 22:17
Khodadad Pakdamans8-Apr-07 22:17 
AnswerRe: convert arrays of number to an integer Pin
Luc Pattyn8-Apr-07 23:38
sitebuilderLuc Pattyn8-Apr-07 23:38 
GeneralRe: convert arrays of number to an integer Pin
cp98769-Apr-07 19:26
cp98769-Apr-07 19:26 

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.