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 6971569
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 27, 20262026-05-27T16:51:25+00:00 2026-05-27T16:51:25+00:00

I saw this question in a programming interview blog. If pairwise sums of n

  • 0

I saw this question in a programming interview blog.

If pairwise sums of n numbers are given in non-decreasing order identify the individual numbers. If the sum is corrupted print -1.

Example:

i/p: 4 5 7 10 12 13 

o/p: 1 3 4 9

A hint would suffice.

  • 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-27T16:51:26+00:00Added an answer on May 27, 2026 at 4:51 pm

    Let B be the list of pairwise sums, with B[0] <= B[1] <= ... <= B[m-1] and let A be the original list of numbers that we’re trying to find, with A[0] < A[1] < ... < A[n-1], where m = n(n-1)/2.

    Given A[0], compute A in polynomial time

    Build A up from smallest element to largest. Suppose that we already know A[0]. Then, since B[0] is the smallest element in B, it can only arise as A[0] + A[1]. Similarly, B[1] must equal A[0] + A[2]. Therefore, if we know A[0], we can compute A[1] and A[2].

    After that, however, this pattern breaks down. B[2] could either be A[0] + A[3] or A[1] + A[2] and without prior knowledge, we cannot know which one it is. However, if we know A[0], we can compute A[1] and A[2] as described above, and then remove A[1] + A[2] from B. The next smallest element is then guaranteed to be A[0] + A[3], which allows us to find A[3]. Continuing like this, we can find all of A without ever backtracking. The algorithm looks something like this:

    for i from 1 to n-1 {
        // REMOVE SEEN SUMS FROM B
        for j from 0 to i-2 {
            remove A[j]+A[i-1] from B
        }
        // SOLVE FOR NEXT TERM
        A[i] = B[0] - A[0]
    }
    return A
    

    Here’s how this works from your example where B = [4,5,7,10,12,13] if we know A[0]=1:

    start
        B = [4,5,7,10,12,13]
        A[0] = 1
    
    i=1: 
        B = [4,5,7,10,12,13]
        A[1] = 4-1 = 3
    
    i=2:
        Remove 1+3 from B
        B = [5,7,10,12,13]
        A[2] = 5-1 = 4
    
    i=3:
        Remove 1+4 and 3+4 from B
        B = [10,12,13]
        A[3] = 10-1 = 9
    
    end
        Remove 1+9 and 3+9 and 4+9 from B
        B = []
        A = [1,3,4,9]
    

    So it all comes down to knowing A[0], from which we can compute the rest of A.

    Compute A[0] in polynomial time

    We can now simply try every possibility for A[0]. Since we know B[0] = A[0] + A[1], we know A[0] must be an integer between 0 and B[0]/2 - 1. We also know that

    B[0] = A[0] + A[1]
    B[1] = A[0] + A[2]
    

    Moreover, there is some index i with 2 <= i <= n-1 such that

    B[i] = A[1] + A[2]
    

    Why? Because the only entries potentially smaller than A[1]+A[2] are of the form A[0] + A[j], and there are at most n-1 such expressions. Therefore we also know that

    A[0] = (B[0]+B[1] - B[i])/2
    

    for some 2 <= i <= n-1. This, together with the fact that A[0] lies between 0 and B[0]/2-1 gives only a few possibilities for A[0] to test.

    For the example, there are two possibilities for A[0]: 0 or 1. If we try the algorithm with A[0]=0, here’s what happens:

    start
        B = [4,5,7,10,12,13]
        A[0] = 0
    
    i=1: 
        B = [4,5,7,10,12,13]
        A[1] = 4-0 = 4
    
    i=2:
        Remove 0+4 from B
        B = [5,7,10,12,13]
        A[2] = 5-0 = 5
    
    i=3:
        Remove 0+5 and 4+5 from B
        B = !!! PROBLEM, THERE IS NO 9 IN B!
    
    end
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

Saw this question recently: Given 2 arrays, the 2nd array containing some of the
I saw this question that asks given a string smith;rodgers;McCalne how can you produce
I saw this question and it motivated me to look again (without success) at
I saw this question Inject into private, package or public field or provide a
I saw this question and it reminded me of AutoGenerateColumns in the old DataGrid.
I just saw this question and one of the answers looks really appealing to
After reading this question , i saw the answer by Naveen containing a link
So I saw this post which helped answer part of my question however now
I just saw this dating site on builtwithbootstrap and have a question about table
I know this may seem like a math question but i just saw this

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.