I used one loop and still got an efficiency penalty. Any help will be appreciated


#1

public class Solution {
public int threeSumClosest(ArrayList A, int B) {
int n=A.size();
int g=1000000;
int i=0,j=1,k=2,s;
while(i<n-2)
{

        s=A.get(i)+A.get(j)+A.get(k);
        if (B-s==0)
        return B;
        if (Math.abs(B-s)<Math.abs(B-g))
        {
            g=s;
            k++;
        }
        else
        k++;
        if (k==n)
        {
            if (j==n-2 )
            {
            i++;
            j=i+1;
            k=j+1;
            
            if (i==n-3)
            break;
            continue;
            }
        j++;
        k=j+1;
        }
        
    }
    return g;
}

}