So I'm trying to solve this problem:
The first 50 natural numbers are written on a board. You apply the following operation 49 times, until you arrive at one final number:
Select any two numbers from the board, a and b.
Erase those two numbers, and replace them with |a-b|.
Determine the sum of all possible values for the final value remaining number on the board.
I've written the following code:
package brillianorg;
import java.util.ArrayList;
public final class AComplexFunction {
ArrayList<Integer> integerArray;
AComplexFunction()
{
this.integerArray = new ArrayList<>();
for(int i = 1;i<=4;i++)
{
integerArray.add(i);
}
System.out.println(returnPathSum(integerArray));
}
int returnPathSum(ArrayList<Integer> newArray)
{
int pathSum = 0;
System.out.println(newArray);
if(newArray.size() == 1)
{
pathSum+=newArray.get(0);
}
else
{
for(int index1 = 0;index1<newArray.size();index1++)
{
System.out.println("iterating through first loop");
for(int index2 = index1+1;index2<newArray.size();index2++)
{
System.out.println("index 1:"+index1+"index 2:"+index2+"array size:"+newArray.size());
ArrayList<Integer>tempArray = newArray;
int difference = Math.abs(newArray.get(index1)-newArray.get(index2));
tempArray.set(index2, difference);
tempArray.remove(index1);
pathSum+=returnPathSum(tempArray);
}
}
}
return pathSum;
}
}
And am a bit confused as to why although it does run, the for loops seem to not be iterating. I'm a bit confused as to why.
Can you please tell me why it isn't iterating? (Please do not solve the problem for me, however, unless fixing the iteration issue solves the problem)