Skip to content

Commit de59fad

Browse files
authored
Create 1968-array-with-elements-not-equal-to-average-of-neighbors.js
Solved array-with-elements-not-equal-to-average-of-neighbors in JS.
1 parent 6e1801d commit de59fad

File tree

1 file changed

+25
-0
lines changed

1 file changed

+25
-0
lines changed
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,25 @@
1+
/**
2+
* Two Pointers
3+
* https://leetcode.com/problems/array-with-elements-not-equal-to-average-of-neighbors/
4+
*
5+
* Time O(n*log(n)) | Space O(n)
6+
* @param {number[]} nums
7+
* @return {number[]}
8+
*/
9+
var rearrangeArray = function(nums) {
10+
nums.sort((a,b) => a-b);
11+
12+
let midPointer = Math.ceil(nums.length / 2);
13+
let beginingPointer = 1;
14+
15+
while(midPointer < nums.length) {
16+
swap(midPointer, beginingPointer, nums);
17+
midPointer++;
18+
beginingPointer += 2
19+
}
20+
return nums;
21+
};
22+
23+
var swap = function(i,j,nums) {
24+
[nums[i], nums[j]] = [nums[j], nums[i]];
25+
}

0 commit comments

Comments
 (0)