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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 15, 20262026-05-15T03:49:57+00:00 2026-05-15T03:49:57+00:00

There are two integer sequences A[] and B[] of length N,both unsorted. Requirement: through

  • 0

There are two integer sequences A[] and B[] of length N,both unsorted.

Requirement: through the swapping of elements between A[] and B[]( can randomly exchange, not with same index), make the difference between {the sum of all elements in A[]} and {the sum of all elements in B[]} to be minimum.

PS: actually,it is an interview question I encountered.

Many thanks

  • 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-15T03:49:58+00:00Added an answer on May 15, 2026 at 3:49 am

    This is going to be NP-hard! I believe you can do a reduction from Subset Sum to this.

    As per BlueRaja/polygene’s comments, I will try to provide a full reduction from Subset Sum.

    Here is a reduction:

    Subset Sum problem: Given integers x1, x2, …, xn, is there some non-empty subset which sums to zero?

    Our problem: Given two integer arrays of size k, find the minimum possible difference of the sum of the two arrays, assuming we can shuffle around the integers in the arrays, treating both arrays as one array.

    Say we had a polynomial time algo for our problem.

    Say now you are given integers T = {x1,x2, …,xn} (multiset)

    Let Si = x1 + x2 + …+ xn + xi.

    Let Ti = {x1, x2, …, xi-1, xi+1, …, xn } ( = T – xi)

    Define

    Ai = Array formed using Ti

    Bi = [Si, 0, …, 0] (i.e one element is Si and rest are zeroes).

    Let mi = the min difference found by our problem for arrays Ai and Bi

    (we run our problem n times).

    Claim: Some non-empty subset of T sums to zero if and only if, there is some i, for which mi = 0.

    Proof: (wlog) say x1 + x2 + .. + xk = 0

    Then

    A = [xk+1, …, xn, 0, …0]

    B = [x2, x3, …, xk, S1, 0, ..0]

    gives the minimum difference m1 to be |x2 + .. + xk + (x1 + … + xn) + x1 – (xk+1 + .. + xn)| = |2(x1+ x2 + .. xk)| = 0.

    Similarly the if part can be proved.

    In fact, this actually also follows (more easily) from Partition too: just create new array with all zeroes.

    Hoepfully I haven’t made any mistakes.

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

Sidebar

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.