# Thread: Help me to understand this leetcode problem

1. ## Help me to understand this leetcode problem

Okay , so a little context.
I am a fresher who is trying to look for a new job and I have decided to grind leetcode. I am trying to improve my C Basics and Advanced Knowledge as that is the primary language and sadly the only language I use while answering Coding Question.

Now for the problem , this problem Concatenation of Array

Given an integer array nums of length n, you want to create an array ans of length 2n where ans[i] == nums[i] and ans[i + n] == nums[i] for 0 <= i < n (0-indexed).

Specifically, ans is the concatenation of two nums arrays.

Return the array ans.

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

}```
So for the questions now.
1. What does the Returned Array must be malloced means?
2. Why is there a int * returnSize as a parameter? Doesn't ReturnSize means I need to return that value instead of putting it in the parameters?

2. 1. What does the Returned Array must be malloced means?
It means you do something like
int *answer = malloc( n * sizeof(*answer));
or
int *answer = malloc( n * sizeof(int));
I prefer the first style.

2. Why is there a int * returnSize as a parameter? Doesn't ReturnSize means I need to return that value instead of putting it in the parameters?
The returnSize tells the caller how big the array you allocated is.
It's somewhat a moot point in this case, as the caller can easily work out the effective size of the array from the specification (it's just 2*n).
But in other scenarios, it may be far less obvious, so it's good practice to return the value.
You just do
*returnSize = n;

n in both cases is numsSize*2

3. Your lucky day.

Code:
```/* main.cpp,  Copy_Twice,  Created by Great on 9/10/22. */
#include <stdio.h>
#include <stdlib.h>

#pragma mark - Interface
unsigned long array_length(const int *);

#pragma mark - Implementation
unsigned long array_length(const int *array) {
const int *a = array;
while (*a) {
++a;
}
return a - array;
}
int main(int argc, const char *argv[]) {

int nums[16] = {1, 2, 3, 0};
int *ans = 0;
int i = 0;
int n = 0;
int size = 0;

n = (signed int)array_length(nums);
size = 1 + n + n;

ans = (signed int *)malloc(sizeof(signed int) * size);

if (0 == ans) {
printf("Error: malloc()\n");

} else {

while ('\0' != nums[i]) {  /* Copy array            */
ans[i] = nums[i];
i = 1 + i;
}
i = 0;                     /* Reset i to 0          */

while ('\0' != nums[i]) {  /* Copy array again      */
ans[i + n] = nums[i];  /* {1, 2, 3} + {1, 2, 3} */
i = 1 + i;
}
ans[i + n] = 0;            /* End array with null   */

free(ans);                 /* malloc() -> free()    */
ans = 0;
}
return 0;
}```

4. I am a fresher who is trying to look for a new job
There is money in programming, but not for the programmer,
certainly not for the employee.

The worst part, programming is a soul-eating job,
in that sense, it is much worse than wasting your life..

"It's easier to fool people than to convince them that they have been fooled."
Mark Twain.

5. Originally Posted by kodax
Code:
`        while ('\0' != nums[i]) {  /* Copy array            */`
This will stop at the first 0 that it finds (element 3). Is that intended?

Also it's kind of unusual to compare an integer with a character literal '\0'. While it's equivalent to 0, usually you would use '\0' only when comparing characters (e.g., in a string).

6. Integer array can end with 0.

Code:
``` #ifndef DEF_H
#define DEF_H

#ifndef NIL
#define NIL 0
#endif

#endif```

7. Originally Posted by kodax
Integer array can end with 0.
Of course it can, if you want to use 0 as a sentinel value. But there's nothing in the original problem that said to have a sentinel-terminated array—zero could be considered a valid value in the array, after all, and it passed around array sizes too.

8. The question is written in a terrible way,
"Given an integer array nums of length n".

The problem is badly formulated.
We know that, 2 x n is length of the new array,
somehow there's is an argument, int *returnSize,
which means return the size that we already know.
This is the best example of a contradiction..

This is correct.
1. There is an array of integers.
2. Int n is the "number of integers" in the array.
3. Allocate a new array that contains two copy of the array.
4. Eg. the array is 1, 2, 3, the result should be 1, 2, 3, 1, 2, 3.
5. Use Free and Malloc.

These are the kind of people who not only run the education system,
they govern, applying their liberal laws without question,
what can you expect..

Popular pages Recent additions