-2

I'm trying out a few contest problems and of course, rushing through them without logic to see my fastest time. However, this is the one I just can't get to work...

import java.util.ArrayList;

import java.util.Scanner;

public class Main {

    private static Scanner input = new Scanner(System.in);

    public static void main(String[] args) {

        ArrayList<Integer> values = new ArrayList<>();

        int n = input.nextInt();

        for(int i = 0; i < n; i++) {
            values.add(input.nextInt());
        }

        boolean done = false;

        while(!done) {

            System.out.println("Beginning");
            for(int i = 0; i < values.size(); i++) {
                System.out.print(values.get(i) + " ");
            }
            System.out.println();

            done = true;

            int changes = 1;

            while(changes != 0) {
                changes = 0;
                for (int i = 0; i < values.size() - 1; i++) {
                    if (values.get(i) == values.get(i + 1)) {
                        values.set(i, values.get(i) * 2);
                        values.remove(i + 1);
                        done = false;
                        changes += 1;
                    }
                }
            }

            System.out.println("Middle");
            for(int i = 0; i < values.size(); i++) {
                System.out.print(values.get(i) + " ");
            }
            System.out.println();

            changes = 1;

            while(changes != 0) {
                changes = 0;
                for(int i = 1; i < values.size() - 1; i++) {
                    System.out.println("i - 1 " + values.get(i - 1));
                    System.out.println("i + 1 = " + values.get(i + 1));
                    if(values.get(i - 1) == values.get(i + 1)) {
                        System.out.println("REACHED");
                        values.remove(i + 1);
                        values.set(i - 1, values.get(i - 1) * 2 + values.get(i));
                        values.remove(i);
                        done = false;
                        changes += 1;
                    }
                }
            }

            System.out.println("Final");
            for(int i = 0; i < values.size(); i++) {
                System.out.print(values.get(i) + " ");
            }
            System.out.println();
        }

        int max = 0;

        for(int i = 0; i < values.size(); i++) {
            int check = values.get(i);
            if(check > max) {
                max = check;
            }
        }

        System.out.println(max);
    }
}

The code that I'm trying to fix is the part where it says "REACHED", even though the values of (i - 1) and (i + 1) are indeed equal according to the console output, the program doesn't continue to execute the statements after the if statement. Is there some error with the way I've made it?

Just for reference, this is the problem I'm looking at. I've spend literally 1 hour debugging.

Alphonse has N rice balls of various sizes in a row. He wants to form the largest rice ball possible for his friend to eat. Alphonse can perform the following operations:

  • If two adjacent rice balls have the same size, Alphonse can combine them to make a new rice ball. The new rice ball’s size is the sum of the two old rice balls’ sizes. It occupies the position in the row previously occupied by the two old rice balls.
  • If two rice balls have the same size, and there is exactly one rice ball between them, Alphonse can combine all three rice balls to make a new rice ball. (The middle rice ball does not need to have the same size as the other two.) The new rice ball’s size is the sum of the three old rice balls’ sizes. It occupies the position in the row previously occupied by the three old rice balls. Alphonse can perform each operation as many times as he wants.

Determine the size of the largest rice ball in the row after performing 0 or more operations.

Input Specification The first line will contain the integer, N (1 ≤ N ≤ 400). The next line will contain N space separated integers representing the sizes of the riceballs, in order from left to right. Each integer is at least 1 and at most 1 000 000.
- For 1 of the 15 available marks, N = 4.
- For an additional 2 of the 15 available marks, N ≤ 10.
- For an additional 5 of the 15 available marks, N ≤ 50.

Output Specification Output the size of the largest riceball Alphonse can form.

OneCricketeer
  • 179,855
  • 19
  • 132
  • 245
  • 1
    Good question, but you could have boiled it down to [a few lines of code](http://stackoverflow.com/help/mcve) instead of dumping this monstrosity. – shmosel Dec 06 '16 at 04:36

2 Answers2

1

try:

if(values.get(i - 1).equals(values.get(i + 1))) {

when you use == on an object (Integer in this case), it checks to see if they point to the same thing, not whether they actually are the same value. That's where .equals comes in.

Jeremy Gurr
  • 1,613
  • 8
  • 11
0

When you try get method on any arrayList the return type is always Object.However u can convert it to primitive int just by int value = arrayList.get(index); and then you can compare. This is known as unboxing an object. Please refer tutorial for this https://docs.oracle.com/javase/tutorial/java/data/autoboxing.html