Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

In the "Two Sum" question in Leetcode, why do we have to *returnSize=2; in C language?

Example 1:

    Input: nums = [2,7,11,15], target = 9 <br>

    Output: [0,1] <br>

Explanation: Because nums[0] + nums[1] == 9, we return [0, 1].

Example 2:

    Input: nums = [3,2,4], target = 6 <br>

    Output: [1,2] <br>

Example 3:

    Input: nums = [3,3], target = 6 <br>

    Output: [0,1] <br>

Constraints:

    2 <= nums.length <= 104
    -109 <= nums[i] <= 109
    -109 <= target <= 109
    Only one valid answer exists.

This is the code I managed to get from solutions and youtube:

  /**
    * Note: The returned array must be malloced, assume caller calls free().
    */
    int* twoSum(int* nums, int numsSize, int target, int* returnSize) { 

    //uptil here, it is given

    int *returnArr=malloc(2 * sizeof(int));
    *returnSize=2; //WHY
    int i,j;
    for(i=0;i<numsSize-1;i++){
        for(j=i+1;j<numsSize;j++){
            if(nums[i]+nums[j]==target){
                returnArr[0]=i;
                returnArr[1]=j;
               return returnArr; //gets out of loop if we encounter return so program is faster if       return is here too;
            }
        }
    }
    return returnArr; //here return returnArr is mandatory;
}

So stackoverflow is telling me I should include non-code text here, it is not letting me post without it, and I have spent a lot of time indenting the code, so yes that is the solution code that works and gives the correct output but I just want to know why we have to put *returnSize=2; and why can't we just declare an array returnArr like int returnArr[100]; - why is leetcode hinting that it should be malloced?

Can I not declare elements inside a function? Can those elements not be sent to the main function? But here, it is just returning the values of the array, not the array itself? But even then, can't we return the array?

P.S. I am a beginner who already learnt(not mastered) DSA and I have spent so much time trying to understand but feel like I'm wasting time instead:(

like image 747
rachelle reena hn Avatar asked Oct 31 '25 13:10

rachelle reena hn


1 Answers

why do we have to *returnSize=2; in C language?

You must return the number of elements in the array in *returnSize because the caller expects it.

(a) Leetcode fails to state this requirement on the problem page, which is a quality problem with Leetcode.

(b) There is no theoretical reason for this requirement; since the return size in this problem is fixed at two elements, no information is gained by returning it explicitly. Returning the size may conform to a common pattern in Leetcode that serves other problems in which the return size is not fixed.

why can't we just declare an array returnArr like int returnArr[100]; - why is leetcode hinting that it should be malloced? Can I not declare elements inside a function?

When you declare int returnArr[100]; inside a function, the declared object has automatic storage duration. This means memory is reserved for it from a time in program execution associated with the declaration until execution of the code block containing the declaration ends. That execution will end when the function returns or sooner. Then the memory is no longer reserved for the object, and it may be reused for other purposes. So the caller of this function cannot rely on the memory being available. Effectively, when int returnArr[100]; is declared inside a function, you cannot return it to a calling function.

Can those elements not be sent to the main function?

No, not reliably.

But here, it is just returning the values of the array, not the array itself?

The statement return returnArr; does not return the array. When an array is used in an expression in C other than as the operand of sizeof, as the the operand of &, or as a string literal used to initialize an array, it is automatically converted to a pointer to its first element. So return returnArr; is actually return &returnArr[0];. It returns a pointer to the memory used for returnArr. However, per above, this memory is not reserved for returnArr after the function returns, so the pointer becomes invalid.

But even then, can't we return the array?

No.

like image 167
Eric Postpischil Avatar answered Nov 02 '25 04:11

Eric Postpischil