Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

ArrayList in java not removing -1

So, I've been in a competition.

The question is I have to print out the 3rd smallest positive integer from an array of inputs. ignoring the negative integers and zero. so the sorted array must start at 1. not 0.

This is what input looks like.

5

1 10 1 2 3 4 5 6 7 8 9 10

2 3 -10 9 8

3 2 0 5

4 5 -2 -1 0 1 2

5 1 -1

first line of input is amount of datasets. it this case it's 5. and the rest are array of integers.

first and second integer of each array are the array number and the amount of elements. the rest are the actual array. i.e. 5 1 -1. 5 is array number 5, 1 is there is only 1 element and -1 is the element.

if there is less than 3 elements in the array. print "na". that's what it should look like (results from input above). if there's more than 3 sorted elements in the array. then print out the array number. seperated by a space and then the 3rd smallest number in the array.

1 3 na na na na

but I get 1 3 na na 4 2 na

and when I print out the 4th sorted array. I get [-1, 1, 2]. The 0 is being removed but not -1. and I don't really understand. is there any way to fix this? or it's just that I needed more skills.

public class one {
    public static void main(String[] args) {
        Scanner in = new Scanner(System.in);
        int dataset = in.nextInt();
        in.nextLine();
        for (int y = 0; y < dataset; y++) {
            int index = in.nextInt();
            int times = in.nextInt();
            ArrayList<Integer> stt = new ArrayList<>(times);
            int ct = 0;
            while (true) {
                int i = in.nextInt();
                stt.add(i);
                ct++;
                if (ct == times) {break;}
            }
            Collections.sort(stt);
            for (int d = 0; d < stt.size(); d++) {
                if (stt.get(d) <= 0) {stt.remove(d);}
            }
            System.out.println(stt);
            if (y + 1 == dataset) {
                if (stt.size() < 3) {System.out.print("na");}
                else {System.out.print((y + 1) + " " + stt.get(2));}
            }
            else {
                if (stt.size() < 3) {System.out.println("na");}
                else {System.out.println((y + 1) + " " + stt.get(2));}
            }
        }
    }
}
like image 258
grindarius Avatar asked Mar 26 '26 15:03

grindarius


2 Answers

I would use list.removeIf() method before sort since it saves you time and lines, plus you have no worries for the ConcurrentModificationException:

public static void main(String[] args) {
    List<Integer> list = Arrays.asList(1, 8, -3, 5, -6, -12, 5, -12, -95, -32, 0, 3, 9);
    list = new ArrayList<>(list); //Make the list modifiable. Ignore this
    list.removeIf(i -> i <= 0); //Remove negatives and 0s
    Collections.sort(list);
    System.out.println(list);
    if (list.size() >= 3)
        System.out.println(list.get(2));
    else
        System.out.println("na");
}

Outputs to:

[1, 3, 5, 5, 8, 9]
5

And as expected, with input: [-1, -8, -3, -5, -6, -12, 5, -12, -95, -32, 0, -3, 9] prints out:

[5, 9]
na
like image 153
George Z. Avatar answered Mar 29 '26 03:03

George Z.


You problem is in this loop:

for (int d = 0; d < stt.size(); d++) {
    if (stt.get(d) <= 0) {stt.remove(d);}
}

Removing an element from a List decrements the index of the remaining elements following that element. Therefore, each time you remove the element whose index is d, you later skip the element whose index was d+1 and became d after the removal.

You can fix it by decrementing the index after each removal:

for (int d = 0; d < stt.size(); d++) {
    if (stt.get(d) <= 0) {
        stt.remove(d);
        d--;
    }
}
like image 36
Eran Avatar answered Mar 29 '26 03:03

Eran