Intuitive and Simple C solution


  • 0
    S

    Intuitive and Simple C solution
    /**

    • Return an array of size *returnSize.
    • Note: The returned array must be malloced, assume caller calls free().
      */

    int* findDuplicates(int* nums, int numsSize, int* returnSize) {
    int i=0;
    for(i=0;i<numsSize;i++)
    {
    int temp = nums[i];
    if(temp>numsSize)
    temp = temp%(numsSize+1);
    nums[temp-1]+=numsSize+1;
    // i need to modify the number
    }
    int count=0;
    // printArray(nums, numsSize);
    for(i=0;i<numsSize;i++)
    {
    if(nums[i]/(numsSize+1) >=2)
    {
    count++;
    }
    }

    *returnSize=count;
    int * result = (int *)calloc(count,sizeof(int));
    count=0;
    for(i=0;i<numsSize;i++)
    {
        if(nums[i]/(numsSize+1) >=2)
            {result[count++] = i+1;}
    }
    
    return result;
    // return nums;
    

    }


Log in to reply
 

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