All Questions
103 questions
0
votes
1
answer
159
views
Java code recursive and iterative methods have different answers
I'm starting to lose hope and sanity...
I'm learning Java code and trying to write a program for choosing hobby, but it must be done by two methods and mine give different numbers... I don't know ...
4
votes
2
answers
280
views
How can I learn to convert recursive solution to iterative solution while preserving the space complexity?
I am trying to solve a easy binary tree question to convert the existing binary tree to a sum tree.
The recursive solution for this is :
class Solution{
public void toSumTree(Node root){
...
1
vote
0
answers
56
views
Why does the recursive version run slower?
I wrote two versions of a helper function for a leetcode problem. I was surprised to see that the iterative version runs over twice as fast, despite being essentially the same code.
I was assuming ...
0
votes
1
answer
35
views
Recursive Iteration looping forever after first
I have very simple code, that for a SOSA STRADONITZ number returns the generation of the person:
1 is 1
2-3 is 2
4-7 is 3
8-15 is 4
16-13 is 5
(...)
The code is the follwing, but it does not return ...
1
vote
1
answer
38
views
why is this recursive method work in this BST
I'm having a hard time trying to understand why this code works
so we have a tree which we use this method to calculate the height
the problem for me is how does this method work to calculate the ...
2
votes
2
answers
387
views
Implementing CompletableFuture recursively
I have three Tasks:
createSimulationScenarios(), runSimulation(), and getSimulationOutput()
These tasks are done in an iterative manner:
createSimulationScenarios() creates new simulation files
...
0
votes
3
answers
133
views
Is there a way to allow for the number of for loops and array elements to be variable in algorithm? - java
Currently writing a program that finds the optimal ratio of 3 given numbers in an array. However the code I have currently only takes 3 numbers and arranges them to find the closest 3 number ...
0
votes
1
answer
97
views
Iterative Newton's Method to recursive (Java)
I need to use Newton's method using recursion. I have this piece of code using iterative Newton's method, but I'm very new to programming so I can't wrap my head around how to turn it into recursion. ...
-1
votes
1
answer
85
views
Translating recursive method into an iterative method
I want to change this recursive method into an iterative method using a while and or for loop but am having trouble. Here is the Recursive method I want to translate:
public static int recursiveNum (...
2
votes
1
answer
80
views
Breaking a recursive and iterative method
Can anyone help me breaking this method and return the actual value of "stop" once "stop" is true? Notice that "stop" starting with false.
private boolean handleRoot(...
1
vote
4
answers
529
views
How to convert tail-recursive function to iterative
How can you convert the following function so that it is iterative?
public static int recursion(int x, int y) {
if(x <= 0) {
return y + 13;
} else if (x == 1) {
return y;
...
0
votes
1
answer
36
views
How to find a value in a stack and put it on top using recursion?
How could I use recursion to search for a value in a stack and put it on top? I thought of this code using iteration:
package pila2;
import java.util.Stack;
public class search {
public static void ...
0
votes
1
answer
62
views
What is the time complexity for these two programs?
Can someone explain to me the time complexity for these three methods and help me understand why it is that time complexity. Both methods takes in a matrix and finds all its surrounding neighbours. ...
0
votes
1
answer
98
views
Traverse inheritance tree recursively with Java
I need to take a given record and populate a list with the identifiers of all of its child records ad infinitem (I.e. all child records of original item, all child records of children, all child ...
0
votes
1
answer
92
views
How to get array with closes subset to the target array
I need to find rows in array (x rows) with closest subset (less or equal) to the target array (1 row).
Target
A
B
Case1
5
6
Target
A
B
Case2
6
2
Candidates
A
B
Can1
1
2
Can2
7
1
Can3
1
0
Can4
1
3
Can5
...