Getting run time error here, though running correctly on ideone on the same test case. Done using k mod n hashing


  • -1
    S

    test case it fails on here: [3,2,4], 6

    struct node{
        int info;
        node * link;
    };
    
    int swap(int &a, int &b)
    {
        int temp=a;
        a=b;
        b=temp;
    }
    
    vector <int> twoSum (vector <int> &nums, int target)
    {
       int sizeofnums=nums.size(),i,index,index1,index2,flag=0;
       vector <int> vec;
       if(sizeofnums==0 || sizeofnums==1)
        return vec;
       node *arr[800011],*ptr,*newnode;
       for(i=0;i<800011;i++)
        arr[i]=NULL;
    
       for(i=0;i<sizeofnums;i++)
       {
            index=abs((target-nums[i])) %   800011;
            for(flag=0,ptr=arr[index];ptr!=NULL;ptr=ptr->link)
            {
                if(ptr->info==target-nums[i])
                {
                    flag=1;
                    break;
                }
            }
            if(flag==1)
                break;
    
            index=abs(nums[i]) % 800011;
            newnode=new node();
            newnode->info=nums[i];
            newnode->link=arr[index];
            arr[index]=newnode;
        }
        if(flag==1)
        {
            index1=i;
            for(i=0;i<sizeofnums;i++)
            {
                if(nums[i]==target-nums[index1])
                    {
                        index2=i;
                        break;
                    }
            }
            if(index1>index2) swap(index1,index2);
            vec.push_back(index1);
            vec.push_back(index2);
        }
        return vec;
    }

Log in to reply
 

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