Movatterモバイル変換


[0]ホーム

URL:


Skip to content

Navigation Menu

Sign in
Appearance settings

Search code, repositories, users, issues, pull requests...

Provide feedback

We read every piece of feedback, and take your input very seriously.

Saved searches

Use saved searches to filter your results more quickly

Sign up
Appearance settings

Commit403e926

Browse files
Add 1968 in c language
1 parent98b6666 commit403e926

File tree

1 file changed

+26
-0
lines changed

1 file changed

+26
-0
lines changed

‎c/1968-array-not-average-neighbors.c

Lines changed: 26 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,26 @@
1+
/*
2+
You want to rearrange the elements in the array such that every element
3+
in the rearranged array is not equal to the average of its neighbors
4+
5+
Space: O(n)
6+
Time: O(nlog(n)) (quicksort)
7+
*/
8+
9+
intcmp(constvoid*a,constvoid*b) {
10+
return*(int*)a-*(int*)b;
11+
}
12+
int*rearrangeArray(int*nums,intnumsSize,int*returnSize){
13+
int*ans=malloc(sizeof(int)*numsSize);
14+
*returnSize=numsSize;
15+
qsort(nums,numsSize,sizeof(int),cmp);
16+
inti,j=0;
17+
for (i=1;i<numsSize;i+=2) {// Put numbers in odd indexes
18+
ans[i]=nums[j];
19+
j++;
20+
}
21+
for (i=0;i<numsSize;i+=2) {// Put numbers in even indexes
22+
ans[i]=nums[j];
23+
j++;
24+
}
25+
returnans;
26+
}

0 commit comments

Comments
 (0)

[8]ページ先頭

©2009-2025 Movatter.jp