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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 18, 20262026-05-18T10:25:20+00:00 2026-05-18T10:25:20+00:00

I have to find the lowest possible sum from numbers’ difference. Let’s say I

  • 0

I have to find the lowest possible sum from numbers’ difference.

Let’s say I have 4 numbers. 1515, 1520, 1500 and 1535. The lowest sum of difference is 30, because 1535 – 1520 = 15 && 1515 – 1500 = 15 and 15 + 15 = 30. If I would do like this: 1520 – 1515 = 5 && 1535 – 1500 = 35 it would be 40 in sum.

Hope you got it, if not, ask me.

Any ideas how to program this? I just found this online, tried to translate from my language to English. It sounds interesting. I can’t do bruteforce, because it would take ages to compile. I don’t need code, just ideas how to program or little fragment of code.

Thanks.

Edit:
I didn’t post everything… One more edition:

I have let’s say 8 possible numbers. But I have to take only 6 of them to make the smallest sum. For instance, numbers 1731, 1572, 2041, 1561, 1682, 1572, 1609, 1731, the smallest sum will be 48, but here I have to take only 6 numbers from 8.

  • 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-18T10:25:20+00:00Added an answer on May 18, 2026 at 10:25 am

    The solution by marcog is a correct, non-recursive, polynomial-time solution to the problem — it’s a pretty standard DP problem — but, just for completeness, here’s a proof that it works, and actual code for the problem. [@marcog: Feel free to copy any part of this answer into your own if you wish; I’ll then delete this.]

    Proof

    Let the list be x1, …, xN. Assume wlog that the list is sorted. We’re trying to find K (disjoint) pairs of elements from the list, such that the sum of their differences is minimised.

    Claim: An optimal solution always consists of the differences of consecutive elements.
    Proof: Suppose you fix the subset of elements whose differences are taken. Then by the proof given by Jonas Kölker, the optimal solution for just this subset consists of differences of consecutive elements from the list. Now suppose there is a solution corresponding to a subset that does not comprise pairs of consecutive elements, i.e. the solution involves a difference xj-xi where j>i+1. Then, we can replace xj with xi+1 to get a smaller difference, since
    xi ≤ xi+1 ≤ xj ⇒ xi+1-xi ≤ xj-xi.
    (Needless to say, if xi+1=xj, then taking xi+1 is indistinguishable from taking xj.) This proves the claim.

    The rest is just routine dynamic programming stuff: the optimal solution using k pairs from the first n elements either doesn’t use the nth element at all (in which case it’s just the optimal solution using k pairs from the first n-1), or it uses the nth element in which case it’s the difference xn-xn-1 plus the optimal solution using k-1 pairs from the first n-2.

    The whole program runs in time O(N log N + NK), as marcog says. (Sorting + DP.)

    Code

    Here’s a complete program. I was lazy with initializing arrays and wrote Python code using dicts; this is a small log(N) factor over using actual arrays.

    '''
    The minimum possible sum|x_i - x_j| using K pairs (2K numbers) from N numbers
    '''
    import sys
    def ints(): return [int(s) for s in sys.stdin.readline().split()]
    
    N, K = ints()
    num = sorted(ints())
    
    best = {} #best[(k,n)] = minimum sum using k pairs out of 0 to n
    def b(k,n):
        if best.has_key((k,n)): return best[(k,n)]
        if k==0: return 0
        return float('inf')
    
    for n in range(1,N):
        for k in range(1,K+1):
            best[(k,n)] = min([b(k,n-1),                      #Not using num[n]
                               b(k-1,n-2) + num[n]-num[n-1]]) #Using num[n]
    
    print best[(K,N-1)]
    

    Test it:

    Input
    4 2
    1515 1520 1500 1535
    Output
    30
    
    Input
    8 3
    1731 1572 2041 1561 1682 1572 1609 1731
    Output
    48
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

No related questions found

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.