Diffk II

https://www.interviewbit.com/old/problems/diffk-ii/

 int Solution::diffPossible(const vector<int> &A, int B) {
    if (A.size() < 2 || B < 0)
        return 0;
        
    unordered_set<int> st;
    
    for(auto i = 0; i < A.size(); i++)
    {
        int Aj = A[i] - B;
        
        int Ai = A[i] + B;
        
        if (st.find(Aj) != st.end())
            return 1;
        if (st.find(Ai) != st.end())
            return 1;
            
        st.insert(A[i]);
    }
    
    return 0;
}

Comments

Popular posts from this blog

Perfect Peak of Array

Is Rectangle?

Sort array with squares!