-
Notifications
You must be signed in to change notification settings - Fork 2.3k
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #2606 from aadil42/patch-58
Create 1968-array-with-elements-not-equal-to-average-of-neighbors.js
- Loading branch information
Showing
1 changed file
with
25 additions
and
0 deletions.
There are no files selected for viewing
25 changes: 25 additions & 0 deletions
25
javascript/1968-array-with-elements-not-equal-to-average-of-neighbors.js
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,25 @@ | ||
/** | ||
* Two Pointers | ||
* https://leetcode.com/problems/array-with-elements-not-equal-to-average-of-neighbors/ | ||
* | ||
* Time O(n*log(n)) | Space O(n) | ||
* @param {number[]} nums | ||
* @return {number[]} | ||
*/ | ||
var rearrangeArray = function(nums) { | ||
nums.sort((a,b) => a-b); | ||
|
||
let midPointer = Math.ceil(nums.length / 2); | ||
let beginingPointer = 1; | ||
|
||
while(midPointer < nums.length) { | ||
swap(midPointer, beginingPointer, nums); | ||
midPointer++; | ||
beginingPointer += 2 | ||
} | ||
return nums; | ||
}; | ||
|
||
var swap = function(i,j,nums) { | ||
[nums[i], nums[j]] = [nums[j], nums[i]]; | ||
} |