My C code for two sum works in Linux GCC compiler but gives wrong answer in OJ for same input test case.Plzz help


  • 0
    S

    struct Map
    {
    int i;
    int val;
    };
    int* twoSum(int* nums, int numsSize, int target) {
    int *array=malloc(sizeof(int)*2);

    int i,rest,min=nums[0],flag=0;
    for(i=0;i<numsSize;i++)
    {
        if(nums[i]<0)
        {
            flag=1;
            min=(min<=nums[i])?min:nums[i];
        }
    }
    if(flag)
    {
        for(i=0;i<numsSize;i++)
        {
            nums[i]=nums[i]+abs(min);
        }
        
        target=target+(2*abs(min));
        flag=0;
    }
    
    struct Map map[target+1];
    for(i=0;i<numsSize;i++)
    {
        rest=target-nums[i];
        if(rest>=0 && map[rest].val==1)
        {
            array[0]=(map[rest].i)+1;
            array[1]=i+1;
           // return array;
           //break;
        }
        if(nums[i]<=target)
        {
        map[nums[i]].val=1;
        map[nums[i]].i=i;
        }
    }
    return array;
    

    }


  • 0
    S

    test case not passing is

    Input: [591,955,829,805,312,83,764,841,12,744,104,773,627,306,731,539,349,811,662,341,465,300,491,423,569,405,508,802,500,747,689,506,129,325,918,606,918,370,623,905,321,670,879,607,140,543,997,530,356,446,444,184,787,199,614,685,778,929,819,612,737,344,471,645,726], 789
    Output: [11,63]
    Expected: [11,56]


Log in to reply
 

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