Skip to content

Commit

Permalink
Merge pull request #2606 from aadil42/patch-58
Browse files Browse the repository at this point in the history
Create 1968-array-with-elements-not-equal-to-average-of-neighbors.js
  • Loading branch information
aakhtar3 authored Sep 9, 2023
2 parents 736719c + de59fad commit e68d5a5
Showing 1 changed file with 25 additions and 0 deletions.
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]];
}

0 comments on commit e68d5a5

Please sign in to comment.