Skip to content

Commit

Permalink
Merge pull request neetcode-gh#1343 from julienChemillier/patch-30
Browse files Browse the repository at this point in the history
Add 1968 in c language
  • Loading branch information
Ahmad-A0 authored Nov 2, 2022
2 parents c79ed76 + 19046ef commit 59f1fd2
Showing 1 changed file with 26 additions and 0 deletions.
26 changes: 26 additions & 0 deletions c/1968-Array-Not-Average-Neighbors.c
Original file line number Diff line number Diff line change
@@ -0,0 +1,26 @@
/*
You want to rearrange the elements in the array such that every element
in the rearranged array is not equal to the average of its neighbors
Space: O(n)
Time: O(nlog(n)) (quicksort)
*/

int cmp(const void* a, const void* b) {
return *(int*)a - *(int*)b;
}
int* rearrangeArray(int* nums, int numsSize, int* returnSize){
int* ans = malloc(sizeof(int)*numsSize);
*returnSize = numsSize;
qsort(nums, numsSize, sizeof(int), cmp);
int i, j=0;
for (i=1; i<numsSize; i+=2) { // Put numbers in odd indexes
ans[i] = nums[j];
j++;
}
for (i=0; i<numsSize; i+=2) { // Put numbers in even indexes
ans[i] = nums[j];
j++;
}
return ans;
}

0 comments on commit 59f1fd2

Please sign in to comment.