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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 26, 20262026-05-26T03:06:45+00:00 2026-05-26T03:06:45+00:00

I have a function in java which is written with a recursive algorithm that

  • 0

I have a function in java which is written with a recursive algorithm that needs to be written in Iterative form. The thing is that I dont know where to start in wrapping my mind around a new algorithm for this. This is an assignment I am working on.

Consider the following computational problem:

Suppose you work as a consultant and you have a sequence of n potential consulting jobs that pay A[0],A[1],..,A[n-1] dollars, respectively (so job 0 pays A[0] dollars, job 1 pays A[1] dollars, etc.).

Also, job i starts on day i (i = 0 ; : : : ; n 1 ).

However, each job requires 2 days, so you cannot perform any two consecutive jobs. The goal is to determine the maximum amount of money, denoted by F(n) ; you can earn from a valid job schedule selected from the n jobs A[0] through A[n-1]
:
As an example, consider the following input array:

  0 1 2 3 4 5
A 5 6 8 6 2 4

An optimal schedule is to do jobs 0 ; 2 ; and 5 ; for which the amount of money
earned, F(6) = A[0] + A [2] + A [5] = 17 ; is as large as possible. Notice that this
is a valid schedule, as no two consecutive jobs are included.

My function for the recursive version that solves this is as follows:

public static int jobScheduleRecursive(int[] A, int n) 
{
    n = A.length;
    if(n == 0){return 0;}
    else if(n == 1){return A[0];}
    else if(n >= 2){return max(jobScheduleRecursive(A, (n-1)), (A[n-1]) 
                           + jobScheduleRecursive(A, n-2));}
    else return -1;
}

To sum up, I have to come up with an iterative algorithm that does this job. The only problem is that i have no idea how to proceed. I would appreciate any advice to lead me in the right direction.

  • 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-26T03:06:46+00:00Added an answer on May 26, 2026 at 3:06 am

    sometimes, iterative solutions for a known recursive solution to problems isn’t straight forward as the recursive solution. The easiest way to achieve iterative solution is to use – Dynamic Programming basically what you want is to create a temporary array that holds the solution to all sub-problems in the way. to achieve that, create a dynamically allocated array in the size of your input. and if for instance your recursive function is int foo(int a)
    fill the array with the solutions to 1..n, where n is the input for your original problem.
    change the algorithm, that instead of calling itself recursivelly, it will check if the solution for the sub-problem allready exists in the array, if not, it fills it up. that way a sub-problem won’t compute numerus times, but only once.

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

Sidebar

Related Questions

I have a neural network written in Java which uses a sigmoid transfer function
i have written an Java Applet, which is loaded in HTML and its function
I often have to debug Java code which was written so that there is
I have a calc function in java script that takes three integer parameters, following
In my Java code I have function that gets file from the client in
I have noticed in Java that you can have a function with object... as
I have a java script function that i have called on body load mousemove()
I have a User Defined Function (UDF) written in Java to parse lines in
We have system here that uses Java JNI to call a function in a
have written a stochastic simulation in Java, which loads data from a few CSV

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.