Skip to content

Commit

Permalink
Create 1968-array-with-elements-not-equal-to-average-of-neighbors.js
Browse files Browse the repository at this point in the history
Solved array-with-elements-not-equal-to-average-of-neighbors in JS.
  • Loading branch information
aadil42 authored Jun 20, 2023
1 parent 6e1801d commit de59fad
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 de59fad

Please sign in to comment.