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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 9, 20262026-06-09T09:40:13+00:00 2026-06-09T09:40:13+00:00

The following is an interview question which I am unable to answer in a

  • 0

The following is an interview question which I am unable to answer in a complexity less than an exponential complexity. Though it seems to be an DP problem, I am unable to form the base cases and analyze it properly. Any help is appreciated.

You are given 2 arrays of size ‘n’ each. You need to stable-merge
these arrays such that in the new array sum of product of consecutive
elements is maximized.

For example

A= { 2, 1, 5}

B= { 3, 7, 9}

Stable merging A = {a1, a2, a3} and B = {b1, b2, b3} will create an array C with 2*n elements. For example, say C = { b1, a1, a2, a3, b2, b3 } by merging (stable) A and B. Then the sum = b1*a1 + a2*a3 + b2*b3 should be a maximum.

  • 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-09T09:40:15+00:00Added an answer on June 9, 2026 at 9:40 am

    Lets define c[i,j] as solution of same problem but array start from i to end for left. And j to end for right.
    So c[0,0] will give solution to original problem.

    c[i,j] consists of.

    1. MaxValue = the max value.
    2. NeedsPairing = true or false = depending on left most element is unpaired.
    3. Child = [p,q] or NULL = defining child key which ends up optimal sum till this level.

    Now defining the optimal substructure for this DP

    c[i,j] = if(NeedsPairing) { left[i]*right[j] } + Max { c[i+1, j], c[i, j+1] }
    

    It’s captured more in detail in this code.

    if (lstart == lend)
    {
        if (rstart == rend)
        {
            nodeResult = new NodeData() { Max = 0, Child = null, NeedsPairing = false };
        }
        else
        {
            nodeResult = new NodeData()
            {
                Max = ComputeMax(right, rstart),
                NeedsPairing = (rend - rstart) % 2 != 0,
                Child = null
            };
        }
    }
    else
    {
        if (rstart == rend)
        {
            nodeResult = new NodeData()
            {
                Max = ComputeMax(left, lstart),
                NeedsPairing = (lend - lstart) % 2 != 0,
                Child = null
            };
        }
        else
        {
            var downLef = Solve(left, lstart + 1, right, rstart);
    
            var lefResNode = new NodeData()
            {
                Child = Tuple.Create(lstart + 1, rstart),
            };
    
            if (downLef.NeedsPairing)
            {
                lefResNode.Max = downLef.Max + left[lstart] * right[rstart];
                lefResNode.NeedsPairing = false;
            }
            else
            {
                lefResNode.Max = downLef.Max;
                lefResNode.NeedsPairing = true;
            }
    
            var downRt = Solve(left, lstart, right, rstart + 1);
    
            var rtResNode = new NodeData()
            {
                Child = Tuple.Create(lstart, rstart + 1),
            };
    
            if (downRt.NeedsPairing)
            {
                rtResNode.Max = downRt.Max + right[rstart] * left[lstart];
                rtResNode.NeedsPairing = false;
            }
            else
            {
                rtResNode.Max = downRt.Max;
                rtResNode.NeedsPairing = true;
            }
    
            if (lefResNode.Max > rtResNode.Max)
            {
                nodeResult = lefResNode;
            }
            else
            {
                nodeResult = rtResNode;
            }
        }
    }
    

    And we use memoization to prevent solving sub problem again.

    Dictionary<Tuple<int, int>, NodeData> memoization = new Dictionary<Tuple<int, int>, NodeData>();
    

    And in end we use NodeData.Child to trace back the path.

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

Sidebar

Related Questions

I got following question on an interview: Which SQL mechanisms allow user to browse
I came across this post , which reports the following interview question: Given two
I was asked this question in interview. Which of the following is better to
Interview question: Design a data structure which has the following features push the data
Alleged interview question and answer here . Will the following code compile (in C)?
The following is an interview question which I've tried hard to solve. The required
I encountered the following interview question on the Internet. Describe a data structure for
I was given the following as an interview question: class A { public: void
Recently I was asked the following question at interview. What are the different way
The following question was asked in one of my interview (few years back) What

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.