Why its not working?


#1

int Solution::solve(vector &A) {
int p,t;
sort (A.begin(), A.end());
int n = A.size();

    for (int i =0;i<n;i++)
    {
        p=A[i];
        
        
        
        if ( n-i-1== abs(p) && A[i] !=A[i+1] || A[i] ==0)
        {
           
            return 1;
            t=0;
            
            break;
            
        }
     
       
        
    }

if (t!=0)

return -1;

}


#2

For test case
-6 -1 4
your program will return 1 which should not be the case so you should replace absĀ§ to p only
also you have to use parenthesis in your if statement . you can see your corrected code below
int Solution::solve(vector &A) {
int p,t;
sort (A.begin(), A.end());
int n = A.size();

for (int i =0;i<n;i++)
{
    p=A[i];
    
    
    
    if ( n-i-1== p && (A[i] !=A[i+1] || A[i] ==0))
    {
       
        return 1;
        
    }
 
   
    
}

return -1;

}