0

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)

2
  • I am not sure what you are trying to accomplish but it's a bad idea to remove elements from a collection WHILE iterating through it using a for-loop. You may want to use an iterator and try again. Commented Sep 13, 2014 at 23:54
  • 2
    @AafreenSheikh on the contrary, if he'll use an iterator while modifying the list he'll get ConcurrentModificationException Commented Sep 14, 2014 at 0:01

1 Answer 1

1

You seem to be trying to make a copy of the list with

ArrayList<Integer> tempArray = newArray;

but that doesn't copy the list -- it only creates another reference to it. As a result, all of the recursive calls share (and remove items from) one list, and as the list shrinks, the loops iterate fewer times. You probably want

ArrayList<Integer> tempArray = new ArrayList<>(newArray);

instead.

Sign up to request clarification or add additional context in comments.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.