We read every piece of feedback, and take your input very seriously.
To see all available qualifiers, see our documentation.
There was an error while loading. Please reload this page.
1 parent 2eab6ea commit dfe654eCopy full SHA for dfe654e
3291-find-if-array-can-be-sorted/find-if-array-can-be-sorted.cpp
@@ -0,0 +1,24 @@
1
+class Solution {
2
+public:
3
+ bool canSortArray(vector<int>& nums) {
4
+ vector<pair<int,int>>v;
5
+ v.push_back({nums[0],nums[0]});
6
+ for(int i = 1 ; i < nums.size() ;i++)
7
+ {
8
+ int cur = __builtin_popcount(nums[i]);
9
+ int prev = __builtin_popcount(nums[i-1]);
10
+ if(cur != prev)
11
12
+ v.push_back({nums[i],nums[i]});
13
+ }
14
+ v.back().first = min(v.back().first,nums[i]);
15
+ v.back().second = max(v.back().second,nums[i]);
16
17
+ for(int i = 1 ; i < v.size() ; i++)
18
19
+ if(v[i-1].second > v[i].first)
20
+ return false;
21
22
+ return true;
23
24
+};
0 commit comments