开发者

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

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

上一篇:

下一篇:

精彩评论

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

最新问答

问答排行榜