A o(n^2)time c solution


  • 0
    H
    bool containsNearbyDuplicate(int* nums, int numsSize, int k) {
        int max;
        for(int i=0;i<numsSize;i++)
        {
            if(i+k+1<=numsSize)
                max=i+k+1;
            else
                max=numsSize;
            for(int j=i+1;j<max;j++)
                if(nums[i]==nums[j])
                    return true;
        }
        return false;
    }
    

Log in to reply
 

Looks like your connection to LeetCode Discuss was lost, please wait while we try to reconnect.