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

  • Home
  • SEARCH
  • 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 7402235
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 29, 20262026-05-29T04:41:32+00:00 2026-05-29T04:41:32+00:00

An interview question: Given two non-ordered integer sequences a and b , their size

  • 0

An interview question:

Given two non-ordered integer sequences a and b, their size is n, all
numbers are randomly chosen: Exchange the elements of a and b, such that the sum of the elements of a minus the sum of the elements of b is minimal.

Given the example:

a = [ 5 1 3 ]
b = [ 2 4 9 ]

The result is (1 + 2 + 3) – (4 + 5 + 9) = -12.

My algorithm: Sort them together and then put the first smallest n ints in a and left in b. It is O(n lg n) in time and O(n) in space. I do not know how to improve it to an algorithm with O(n) in time and O(1) in space. O(1) means that we do not need more extra space except seq 1 and 2 themselves.

Any ideas ?

An alternative question would be: What if we need to minimize the absolute value of the differences (minimize |sum(a) - sum(b)|)?

A python or C++ thinking is preferred.

  • 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-29T04:41:36+00:00Added an answer on May 29, 2026 at 4:41 am

    Revised solution:

    1. Merge both lists x = merge(a,b).

    2. Calculate median of x (complexity O(n) See http://en.wikipedia.org/wiki/Selection_algorithm )

    3. Using this median swap elements between a and b. That is, find an element in a that is less than median, find one in b that is more than median and swap them

    Final complexity: O(n)

    Minimizing absolute difference is NP complete since it is equivalent to the knapsack problem.

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

Sidebar

Related Questions

Possible Duplicate: Easy interview question got harder: given numbers 1..100, find the missing number(s)
I got following question on an interview: Given a table of natural numbers with
I came across this post , which reports the following interview question: Given two
this is a google interview question : Given a N*N Matrix. All rows are
This is an interview question . Given an array of numbers and another number,
In the following interview question : Given a number n, give me the numbers
I was stuck at one question in an interview. Given two threads, and each
This is an interview question: Given a string, find all its permutations that are
This was an interview question: Given an amount, say $167.37 find all the possible
This is an open-end interview question. Given an array with 2 numbers are duplicated

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.