-
Notifications
You must be signed in to change notification settings - Fork 61
Expand file tree
/
Copy path3011-find-if-array-can-be-sorted.js
More file actions
57 lines (52 loc) · 1.33 KB
/
3011-find-if-array-can-be-sorted.js
File metadata and controls
57 lines (52 loc) · 1.33 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
/**
* 3011. Find if Array Can Be Sorted
* https://leetcode.com/problems/find-if-array-can-be-sorted/
* Difficulty: Medium
*
* You are given a 0-indexed array of positive integers nums.
*
* In one operation, you can swap any two adjacent elements if they have the same number of
* set bits. You are allowed to do this operation any number of times (including zero).
*
* Return true if you can sort the array in ascending order, else return false.
*/
/**
* @param {number[]} nums
* @return {boolean}
*/
var canSortArray = function(nums) {
const sorted = [...nums].sort((a, b) => a - b);
const bitGroups = [];
let currentBits = countSetBits(nums[0]);
let group = [nums[0]];
for (let i = 1; i < nums.length; i++) {
const bits = countSetBits(nums[i]);
if (bits === currentBits) {
group.push(nums[i]);
} else {
bitGroups.push(group);
group = [nums[i]];
currentBits = bits;
}
}
bitGroups.push(group);
let index = 0;
for (const group of bitGroups) {
const groupSorted = [...group].sort((a, b) => a - b);
for (const num of groupSorted) {
if (num !== sorted[index]) {
return false;
}
index++;
}
}
return true;
function countSetBits(num) {
let count = 0;
while (num) {
count += num & 1;
num >>= 1;
}
return count;
}
};