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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 12, 20262026-06-12T10:57:14+00:00 2026-06-12T10:57:14+00:00

I have the following recursion: if(a%2 == 0){ f([a1,a2,…,aN],a,N) = (a1 + aN)/2 +

  • 0

I have the following recursion:

if(a%2 == 0){
f([a1,a2,...,aN],a,N) = (a1 + aN)/2 + f([a1,a2,...,a(N-1)],a+1,N-1)/2 + 
f([a2,...,aN],a+1,N-1)/2;
}
else{
f([a1,a2,...,aN],a,N) = f([a1,a2,...,a(N-1)],a+1,N-1)/2 + 
f([a2,...,aN],a+1,N-1)/2;
}

Base Case:

f([a1,a2],a,2) = (a1+a2)/2;

Obviously, there’ll be a stack overflow if I implement it recursively. How should I make use of Dynamic Programming to obtain optimal solution to this recursion?

[a1,a2,..,aN] respresents an integer array.

The limit for N is 2000 and a1,a2,..,aN <=999.

  • 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-12T10:57:16+00:00Added an answer on June 12, 2026 at 10:57 am

    This smells an awful lot like a homework problem. I would suggest that you meet with the lecturer or a TA because this is the sort of thing best learned interactively. If you use this information, make sure to cite it so you don’t commit plagiarism.

    First, observe that the results are linear in the values [a0, a1, ... aN]. Therefore, you really only need to keep track of their coefficients. For notational purposes, let’s write {b1, b2, ..., bN} to represent b1 * a1 + b2 * a2 + ... bN * aN.

    Next, work out a few of the recursions by hand:

    f([a1, a2], a, 2) = { 1/2, 1/2 } is the base case for N=2.

    Let’s look at N=3:

    f([a1, a2, a3], a, 3) for a even = {1/2, 0, 1/2} + { f([a1, a2], a+1, 2)/2, 0 } + { 0, f([a2, a3], a+1, 2)/2 } = { 1/2, 0, 1/2 } + { 1/4, 1/4, 0 } + { 0, 1/4, 1/4 } = { 3/4, 1/2, 3/4 }.

    f([a1, a2, a3], a, 3) for a odd = { f([a1, a2], a+1, 2)/2, 0 } + { 0, f([a2, a3], a+1, 2)/2 } = { 1/2, 0, 1/2 } + { 1/4, 1/4, 0 } + { 0, 1/4, 1/4 } = { 1/4, 1/2, 1/4 }.

    Now N=4:

    f([a1, a2, a3, a4], a, 4) for a even = { 1/2, 0, 0, 1/2 } + { f[a1, a2, a3], a+1, 3)/2, 0 } + { 0, f([a2, a3, a4], a+1, 3)/2 }. Since a is even, a+1 is odd, so we are in the case F([], even, 3). f([a1, a2, a3, a4], a, 4) for a even = { 1/2, 0, 0, 1/2 } + { 1/8, 1/4, 1/8, 0 } + { 0, 1/8, 1/4, 1/8 } = { 5/8, 3/8, 3/8, 5/8 }.

    f([a1, a2, a3, a4], a, 4) for a odd = { f[a1, a2, a3], even, 3)/2, 0 } + { 0, f([a2, a3, a4], even, 3)/2 } = { 3/8, 1/4, 3/8, 0 } + { 0, 3/8, 1/4, 3/8 } = { 3/8, 5/8, 5/8, 3/8 }.

    Now you can see that the coefficients depend only on N and whether a even or odd.

    This means that your dynamic programming only needs to remember the coefficients for each combination of N and a boolean. Since N is capped at 2000, this means you need only 4000 entries, which shouldn’t be too much of a burden. In fact, you could abandon the recursion and simply compute the entire table incrementally like we did above.

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

Sidebar

Related Questions

I have following fiddle: http://jsfiddle.net/BFSH4/ As you see there are two issues: The h1
I am really struggling to understand tail recursion in Erlang. I have the following
I have the following recursion: T(n) = 2*T(n/4) + T(n/2) + n and I
I have the following recursion code, at each node I call sql query to
I need information which should be collected in a recursion. I have a wpf
For an assignment, i have written the following code in recursion. It takes a
I have the following function, that is called once in my program. When there
Is it possible to do recursion with an Func delegate? I have the following,
sorry to overflow with so many questions. I have the following: (defun recursive-function (string)
I have following challenge: Implement a function that searches for a null point of

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.