not using a hash map, sorting the array first c++


  • 0
            int ** remap = new int * [nums.size()];
            for(int i=0; i<nums.size();i++){
                remap[i] = new int [2];
                remap[i][0] = nums[i]; remap[i][1] = i;
            }
            sort(remap, remap+nums.size(), [](int a[], int b[]){ return a[0] < b[0];});
            
            int start = 0;
            int end = nums.size()-1;
            while(start<end){
                while(remap[start][0] + remap[end][0] < target) start++;
                
                while(remap[start][0] + remap[end][0] > target) end--;
                
                if(remap[start][0] + remap[end][0] == target)
                    return {remap[start][1], remap[end][1]};
    
            }
    

Log in to reply
 

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