O(nlogn)time complexity Java Solution


#1

public class Solution {
public int solve(ArrayList A) {

    Collections.sort(A);
    int last= A.size()-1;
    for(int i=0;i<A.size();i++){
        int j=i;
        if(j<last && A.get(j) == A.get(j+1)){
            while(j<last && A.get(j) == A.get(j+1)){
                j++;
            }
            i=j;
            if((last-j) == A.get(j))return 1;
        }else{
            if(A.get(i) == (last-i)){
                return 1;
            }
        }
    }
   return -1; 
}

}