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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 13, 20262026-06-13T15:20:25+00:00 2026-06-13T15:20:25+00:00

This is the problem: You have two arrays A and B, of equal length.

  • 0

This is the problem:

You have two arrays A and B, of equal length. You have to partition them into two groups P and Q such that:
(1) Their difference is minimized.
(2) If A[i] goes into one of P or Q, B[i] should go into another.

Here is a link to the actual problem: http://opc.iarcs.org.in/index.php/problems/EQGIFTS

This is my logic (to solve the actual problem):

if the input is : a b c d e f g, a list of values and the index of
a,b,c,d,e,f is 0,1,2,3,4,5 respectively

if t is a index of a,b,c,d,e,f,g the program checks for t and i such
that: the value at [t] > value at [t-i] , beginning with t = 5, and i
= 1, and increasing the value of i by 1 and decreasing the value of t
by 1.

as soon as it finds a match, it swaps the values of both the indices
and sorts the values beginning from [t-1].
the resulting list of values is the output.

I don’t know what is wrong with this algorithm, but it produces a wrong answer for all the test cases.
I know it can be solved using dynamic programming, and that it is a variation of the partition problem. But i don’t know how to change the partition algorithm to solve this problem.

  • 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-06-13T15:20:26+00:00Added an answer on June 13, 2026 at 3:20 pm

    Reduce the problem to partition problem:

    Create a third array D[i] = B[i] - A[i] for each i.

    Now the problem is a classic partition problem on the array D, and you can use its DP solution to have a pseudo-polynomial time solution.

    Correctness Proof:

    If there is a solution on D (sum(D_1) = sum(D_2)) – then there are i_1,...,i_k chosen to D_1 and j_1,...,j_m chosen to D_2 (and each index is in i’s or j’s), such that:

    sum(D[i's]) = sum(D[j's])
    

    From the construction, it means:

    sum(B[i]-A[i]) = sum(B[j]-A[j]) (for each relevant i's,j's)
    

    and thus:

    sum(B[i's]) - sum(A[i's]) = sum (B[j's]) - sum(A[j's])
    

    From this:

    sum(B[i's]) + sum(A[j's]) = sum(B[j's]) + sum(A[i's])
    

    which exactly what we wanted, since each “index” is assigned to both parts, one part gets a B and the other gets A.

    The other direction is similar.

    QED


    Complexity of the problem:

    The problem is still NP-Hard with the simple reduction:

    Given an instance of Partition Problem (S=[a_1,a_2,...,a_n]), create the instance of this problem:

    A=S, B=[0,...,0]
    

    It is easy to see that the same solution that gives optimal solution to this problem will be the needed partition to the original partition problem, and thus the problem is NP-Hard.

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

Sidebar

Related Questions

This is the problem I am trying to solve: I have two arrays of
So i'm having this problem. I have two tables (Oracle), one is called Destination
Can someone explain to me how I can fix this problem. I have two
I Have two problem in this Case : I want to pass a JSON
In this problem, I have three (identically-structured) lists. Two have all numbers and the
Following up this question , I have a further problem - I have two
My problem is this: I have two threads, my UI thread, and a worker
I have had this problem w/ two seperate WYSWYG editors in my rails application
I have this little problem. My client wanted two distinct swf on a web
I have a problem with IE causing two errors: 1. Object doesn't support 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.