-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathfind-if-array-can-be-sorted.py
More file actions
41 lines (37 loc) · 1.34 KB
/
find-if-array-can-be-sorted.py
File metadata and controls
41 lines (37 loc) · 1.34 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
'''
Intution was that since we are need to swap each adjacent element and sort we can use bubble sort and make a swap only if number of 1's are same in both swapiees. Finally check if the resultant array is same as originalarray.sort()
worked becz len(arr) MAX is 100.
'''
class Solution:
def canSortArray(self, nums: List[int]) -> bool:
seen = {}
def countOnes(a: int,b: int) -> bool:
countA, countB = 0, 0
if a in seen:
countA = seen[a]
else:
binA = bin(a)[2:]
for i in binA:
if i == '1':
countA += 1
seen[a] = countA
if b in seen:
countB = seen[b]
else:
binB = bin(b)[2:]
for j in binB:
if j == '1':
countB += 1
seen[b] = countB
return countA == countB
temp = nums.copy()
temp.sort()
n = len(nums)
for i in range(n):
for j in range(n-1-i):
if nums[j] > nums[j+1]:
if countOnes(nums[j],nums[j+1]):
nums[j],nums[j+1] = nums[j+1], nums[j]
else:
return False
return nums == temp