How to Tackle "Returnsize" in Leetcode

What is the use of - int * return size, mostly seen in C in leetcode problems.
Please help me as there are no C submissions for this problem.
Problem is very simple - Sort Array.
But I don’t know how to use "returnsize ".

problem link-https://leetcode.com/problems/sort-an-array/description/

My leet code solution - Jf3pQF - Online C Compiler & Debugging Tool - Ideone.com

Thanks

In C, an array variable is basically a pointer to the first memory location of the array. So suppose you have int a[100]. Now *a refers to a[0]. So, suppose you have to return an array, you return the pointer to the first memory location. I suggest you read about Pointers from web or books. C++ Pointer to an Array can help understand the leetcode thing atleast clearly.

I know, I’m returning pointer only.
How to use “returnSize” in my code (passed as a parameter).

Can you elaborate ? Give an example.

I have attached a link of my solution. Its not getting accepted because I dont know how to use “returnSize” which is passed as a parameter to function.
I have malloced array and returned pointer to this array.

I dont know what author is trying to imply.
int * returnSize; // as a parameter to function.

Can anyone help me !! I have posted solution but unaware of use of “returnSize”.
Can anyone correct my solution

Thanks

In C, when you return a dynamically allocated array, it has no idea how long your array is. Therefore, you need to also return the array’s size. That’s why there is a parameter returnSize passed in so that you can assign the array’s size to it.

To assign the array’s size, do this:

*returnSize = n;   // if the array's size is n.
1 Like

I still don’t know why your code is giving an error, Let me know if your code works.

Array is by default pass by reference, so why they are asking to malloc it in heap.
Actually its not required??

Actually you don’t have to, Here’s the working code:

/**
 * Note: The returned array must be malloced, assume caller calls free().
 */
int* sortArray(int* nums, int numsSize, int* returnSize){
    *returnSize=numsSize;
    
    int flag;
    for(int i=0;i<numsSize-1;i++)
    {
        flag=0;
        for(int j=0;j<numsSize-i-1;j++)
        {
            
            if(nums[j]>nums[j+1])
            {
                int temp;
                temp=nums[j];
                nums[j]=nums[j+1];
                nums[j+1]=temp;
                flag=1;
            }
        }
        if(flag==0)
            break;
    }
    return nums;

}


This gives TLE though, use quick sort or merge sort.

2 Likes

Thanks.
I was doing numsSize * sizeof(int)

You can do malloc too, But not needed for bubble sort.

This works fine:

/**
 * Note: The returned array must be malloced, assume caller calls free().
 */
int* sortArray(int* nums, int numsSize, int* returnSize){
    *returnSize=numsSize;
    int *p=(int *)malloc(numsSize * sizeof(int));
    int flag;
    for(int i=0;i<numsSize-1;i++)
    {
        flag=0;
        for(int j=0;j<numsSize-i-1;j++)
        {
            
            if(nums[j]>nums[j+1])
            {
                int temp;
                temp=nums[j];
                nums[j]=nums[j+1];
                nums[j+1]=temp;
                flag=1;
            }
        }
        if(flag==0)
            break;
    }
    for(int i=0;i<numsSize;i++)
       p[i]=nums[i];
 
    return p;

}
2 Likes

It’s really working thanks…

I will appreciate you for posting the question …
So that i can get a solution for the problem because of returnSize

int *p=(int *)malloc(numsSize * sizeof(int));

for(int i=0;i<numsSize;i++)
p[i]=nums[i];
atlast
return p;

This works , coz u didnt use malloc