Problem Statement: Given an integer array, find if an integer p exists in the array such that the number of integers greater than p in the array equals to p
If such an integer is found return 1 else return -1.
My code:
public int solve(ArrayList<Integer> A) {
Collections.sort(A);
for(int i=A.size()-1;i>=0; i--){
if(A.get(i) == (A.size()-i-1))
return 1;
}
return -1;
}
But it is giving wrong output for the some input which I am unable to understand intuitively.i.e, returning 1 when it should return -1. Can anyone point out my mistake(s)?
See Question&Answers more detail:
os 与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…