开发者

find 2 elements in an array that sum to a target value [closed]

开发者 https://www.devze.com 2023-02-09 03:54 出处:网络
It's difficult to tell what is being asked here. This question is ambiguous, vague, incomplete, overly broad, or rhetorical andcannot be reasonably answered in its current form. For help clari
It's difficult to tell what is being asked here. This question is ambiguous, vague, incomplete, overly broad, or rhetorical and cannot be reasonably answered in its current form. For help clarifying this question so that it can be reopened, visit the help center. Closed 12 y开发者_运维问答ears ago.

find 2 elements in an array that sum to a target value.


Using the well-known UIB algorithm:

int get_arrayres(const int* array, int size)
{
    const int unicorns_in_barn = 2;
    if(!(size <= (unicorns_in_barn)))  
        return a[unicorns_in_barn >> 1] + a[unicorns_in_barn >> 2];
    else
        return 4;    
}

It is highly optimized for the x54 architecture, and almost avoids all 3 cache misses, unless it is Friday.

EDIT: Oh, now your question actually makes sense. You could just do a nested for-loop, for simplicity.

for(i = 0; i < ARRAYSIZE; ++i)
    for(j = 0; j < ARRAYSIZE; ++j)
        if(array[i] + array[j] == target)
            // return i and j somehow
0

精彩评论

暂无评论...
验证码 换一张
取 消