0

I am learning how to implement linear probing in hash functions. I am getting error time limit exceeded for the code below how can I improve this code?

vector<int> linearProbing(int hashSize, int arr[], int sizeOfArray)
{
   vector<int> hash(hashSize,-1);
   for(int i=0;i<sizeOfArray;i++){
       int h = arr[i]%hashSize;
       for(int j=0;j<sizeOfArray;j++){
           if(hash[h]==-1 or arr[i]==hash[h]){
               hash[h]=arr[i];
               break;
           }
           else
               h=(h+1)%hashSize;
       }
    }
    return hash;
}

Constraints:
1 <= hashSize <= 100
1 <= sizeOfArray <= 100
0 <= arr[] <= 10^5

Expected time limit 2.28s
Anshul
  • 13
  • 7
  • Doesn't seem to “time out” error, Is it other issue? – daohu527 Dec 17 '21 at 11:42
  • I thought c++ had `||` as the logical or operator, not `or`. Otherwise, it looks like linear probing to me which can perform very poorly for even moderately loaded hash tables. Getting a time out error might be the point of the exercise. Improving the code would mean replacing linear probing with a different collision resolution algorithm. – President James K. Polk Dec 17 '21 at 13:31
  • @chux-ReinstateMonica yes, I have edited – Anshul Dec 18 '21 at 03:48
  • @PresidentJamesK.Polk 'or' works fine in c++. I have added constraints please check. – Anshul Dec 18 '21 at 03:55

0 Answers0