Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 439891
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 12, 20262026-05-12T20:49:07+00:00 2026-05-12T20:49:07+00:00

Can you suggest an algorithm that find all pairs of nodes in a link

  • 0

Can you suggest an algorithm that find all pairs of nodes in a link list that add up to 10.
I came up with the following.

Algorithm: Compare each node, starting with the second node, with each node starting from the head node till the previous node (previous to the current node being compared) and report all such pairs.

I think this algorithm should work however its certainly not the most efficient one having a complexity of O(n2).

Can anyone hint at a solution which is more efficient (perhaps takes linear time). Additional or temporary nodes can be used by such a solution.

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-05-12T20:49:07+00:00Added an answer on May 12, 2026 at 8:49 pm

    If their range is limited (say between -100 and 100), it’s easy.

    Create an array quant[-100..100] then just cycle through your linked list, executing:

    quant[value] = quant[value] + 1
    

    Then the following loop will do the trick.

    for i = -100 to 100:
        j = 10 - i
            for k = 1 to quant[i] * quant[j]
                output i, " ", j
    

    Even if their range isn’t limited, you can have a more efficient method than what you proposed, by sorting the values first and then just keeping counts rather than individual values (same as the above solution).

    This is achieved by running two pointers, one at the start of the list and one at the end. When the numbers at those pointers add up to 10, output them and move the end pointer down and the start pointer up.

    When they’re greater than 10, move the end pointer down. When they’re less, move the start pointer up.

    This relies on the sorted nature. Less than 10 means you need to make the sum higher (move start pointer up). Greater than 10 means you need to make the sum less (end pointer down). Since they’re are no duplicates in the list (because of the counts), being equal to 10 means you move both pointers.

    Stop when the pointers pass each other.

    There’s one more tricky bit and that’s when the pointers are equal and the value sums to 10 (this can only happen when the value is 5, obviously).

    You don’t output the number of pairs based on the product, rather it’s based on the product of the value minus 1. That’s because a value 5 with count of 1 doesn’t actually sum to 10 (since there’s only one 5).

    So, for the list:

    2 3 1 3 5 7 10 -1 11
    

    you get:

    Index    a  b  c  d  e  f  g  h
    Value   -1  1  2  3  5  7 10 11
    Count    1  1  1  2  1  1  1  1
    
    • You start pointer p1 at a and p2 at h. Since -1 + 11 = 10, you output those two numbers (as above, you do it N times where N is the product of the counts). Thats one copy of (-1,11). Then you move p1 to b and p2 to g.
    • 1 + 10 > 10 so leave p1 at b, move p2 down to f.
    • 1 + 7 < 10 so move p1 to c, leave p2 at f.
    • 2 + 7 < 10 so move p1 to d, leave p2 at f.
    • 3 + 7 = 10, output two copies of (3,7) since the count of d is 2, move p1 to e, p2 to e.
    • 5 + 5 = 10 but p1 = p2 so the product is 0 times 0 or 0. Output nothing, move p1 to f, p2 to d.
    • Loop ends since p1 > p2.

    Hence the overall output was:

    (-1,11)
    ( 3, 7)
    ( 3, 7)
    

    which is correct.

    Here’s some test code. You’ll notice that I’ve forced 7 (the midpoint) to a specific value for testing. Obviously, you wouldn’t do this.

    #include <stdio.h>
    
    #define SZSRC 30
    #define SZSORTED 20
    #define SUM 14
    
    int main (void) {
        int i, s, e, prod;
        int srcData[SZSRC];
        int sortedVal[SZSORTED];
        int sortedCnt[SZSORTED];
    
        // Make some random data.
    
        srand (time (0));
        for (i = 0; i < SZSRC; i++) {
            srcData[i] = rand() % SZSORTED;
            printf ("srcData[%2d] = %5d\n", i, srcData[i]);
        }
    
        // Convert to value/size array.
    
        for (i = 0; i < SZSORTED; i++) {
            sortedVal[i] = i;
            sortedCnt[i] = 0;
        }
        for (i = 0; i < SZSRC; i++)
            sortedCnt[srcData[i]]++;
    
        // Force 7+7 to specific count for testing.
    
        sortedCnt[7] = 2;
        for (i = 0; i < SZSORTED; i++)
            if (sortedCnt[i] != 0)
                printf ("Sorted [%3d], count = %3d\n", i, sortedCnt[i]);
    
        // Start and end pointers.
    
        s = 0;
        e = SZSORTED - 1;
    
        // Loop until they overlap.
    
        while (s <= e) {
            // Equal to desired value?
    
            if (sortedVal[s] + sortedVal[e] == SUM) {
                // Get product (note special case at midpoint).
    
                prod = (s == e)
                    ? (sortedCnt[s] - 1) * (sortedCnt[e] - 1)
                    : sortedCnt[s] * sortedCnt[e];
    
                // Output the right count.
    
                for (i = 0; i < prod; i++)
                    printf ("(%3d,%3d)\n", sortedVal[s], sortedVal[e]);
    
                // Move both pointers and continue.
    
                s++;
                e--;
                continue;
            }
    
            // Less than desired, move start pointer.
    
            if (sortedVal[s] + sortedVal[e] < SUM) {
                s++;
                continue;
            }
    
            // Greater than desired, move end pointer.
    
            e--;
        }
    
        return 0;
    }
    

    You’ll see that the code above is all O(n) since I’m not sorting in this version, just intelligently using the values as indexes.

    If the minimum is below zero (or very high to the point where it would waste too much memory), you can just use a minVal to adjust the indexes (another O(n) scan to find the minimum value and then just use i-minVal instead of i for array indexes).

    And, even if the range from low to high is too expensive on memory, you can use a sparse array. You’ll have to sort it, O(n log n), and search it for updating counts, also O(n log n), but that’s still better than the original O(n2). The reason the binary search is O(n log n) is because a single search would be O(log n) but you have to do it for each value.

    And here’s the output from a test run, which shows you the various stages of calculation.

    srcData[ 0] =    13
    srcData[ 1] =    16
    srcData[ 2] =     9
    srcData[ 3] =    14
    srcData[ 4] =     0
    srcData[ 5] =     8
    srcData[ 6] =     9
    srcData[ 7] =     8
    srcData[ 8] =     5
    srcData[ 9] =     9
    srcData[10] =    12
    srcData[11] =    18
    srcData[12] =     3
    srcData[13] =    14
    srcData[14] =     7
    srcData[15] =    16
    srcData[16] =    12
    srcData[17] =     8
    srcData[18] =    17
    srcData[19] =    11
    srcData[20] =    13
    srcData[21] =     3
    srcData[22] =    16
    srcData[23] =     9
    srcData[24] =    10
    srcData[25] =     3
    srcData[26] =    16
    srcData[27] =     9
    srcData[28] =    13
    srcData[29] =     5
    Sorted [  0], count =   1
    Sorted [  3], count =   3
    Sorted [  5], count =   2
    Sorted [  7], count =   2
    Sorted [  8], count =   3
    Sorted [  9], count =   5
    Sorted [ 10], count =   1
    Sorted [ 11], count =   1
    Sorted [ 12], count =   2
    Sorted [ 13], count =   3
    Sorted [ 14], count =   2
    Sorted [ 16], count =   4
    Sorted [ 17], count =   1
    Sorted [ 18], count =   1
    (  0, 14)
    (  0, 14)
    (  3, 11)
    (  3, 11)
    (  3, 11)
    (  5,  9)
    (  5,  9)
    (  5,  9)
    (  5,  9)
    (  5,  9)
    (  5,  9)
    (  5,  9)
    (  5,  9)
    (  5,  9)
    (  5,  9)
    (  7,  7)

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

Can anyone suggest a versatile PHP encrypt/decrypt algorithm that encrypts in the following way:
What I'm trying to do is find an algorithm that can I can implement
Can someone suggest an algorithm to find a random number within say a limit
Can someone suggest a fast 2 way encryption algorithm for long ints? My candidates
Wondering if anyone can suggest a good file replication tool that will replicate across
I've searched around and I can't find a simple commandline tool that will take
I am searching stemming algorithm for Slovenian language that I can use with Sphinx
I have over 1.3milion images that I have to compare with each other, and
I came across this problem,I have got an algorithm that I need to implement
i wanted to know that is there any algorithm that can be used for

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.