Is it me or the editorial is wrong?


#1

public class Solution {
public int solve(ArrayList A) {
// Total runtime: O(n log n) due to sort
Collections.sort(A);

    for(int i = 0; i < A.size(); i++) {
        // Handle duplicates (only check for rightmost duplicate), skip others
        if(i < A.size() - 1 && A.get(i) == A.get(i + 1)) {
            continue;
        }
        // Check if the remaining values to the right are equal to the current value
        if(A.size() - i - 1 == A.get(i)) {
            return 1;
        }
    }
    
    return -1;
}

}

test case: [3, 10, 10, 10]

this should give me 1 as far as my understanding of the question question. However its giving -1 as the output. Is there something that I am missing ?


#2

When you are handling duplicates use while instead of if. So replace your if statement with this
while(i<A.size()-1 && A.get[i]==A.get(i+1))
i++;