I need to take a part of an Array, let's say:
32 64 66 69 72 78 81 87 94 95 1 2 4 8 16
realloc
a new memory from the end of the array, take the part from 32 to 95 using memcpy
to copy it after 16
, rearrange the array and return the value of k
(k
is the size of the first group- 32 to 95, which is 10).
So the output should be:
1 2 4 8 16 32 64 66 69 72 78 81 87 94 95
I've written this code, which doesn't work, Anyone help please :(
int arrangeArray(int** arr, int n)
{
// your code:
int i, k = 0, rest = 0;
for (i = 0; i < n-1;)
{
if (arr[i] < arr[++i])
k++;
}
rest = n - k;
*arr = (int*)realloc(*arr, (n + rest)*sizeof(int));
memcpy((*arr)+rest, *arr, n*sizeof(int));
k += rest;
memcpy(*arr, (*arr) + k, rest * sizeof(int));
*arr = (int*)realloc(*arr, n * sizeof(int));
return k;
}