I passed all test cases except last one can anyone tell me the problem with my code


  • 0
    K
    int chk(int a,int b){
        int c=a;
        for(int i=0;i<b;i++){
            c=c/2;
        }
            return (c%2);
    
    }
    class Solution {
    public:
        vector<int> singleNumber(vector<int>& nums) {
            long long int xor1=0,num1=0,num2=0,temp,ind=0,temp2,temp3=0,temp4=0;
            vector <int> ans;
            for(int i=0;i<nums.size();i++){
                xor1=xor1^nums[i];
            
            }
            //cout << xor1 <<endl;
            temp=xor1;
            while(1){
                if(temp%2!=0){
                    break;
                }
                ind++;
                temp=temp/2;
            }
            //cout << ind <<endl;
        
            for(int i=0;i<nums.size();i++){
                if(chk(nums[i],ind)){
                    temp3=temp3^nums[i];
                }
            
            }
            //cout << temp3 <<endl;
            temp4=temp3^xor1;
            //cout << temp4 <<endl;
            ans.push_back(temp3);
            ans.push_back(temp4);
            return ans;
           
    }
    

    };


Log in to reply
 

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