Message on Whatsapp 8879355057 for DSA(OA + Interview) + Fullstack Dev Training + 1-1 Personalized Mentoring to get 10+LPA Job
0 like 0 dislike
746 views
in Online Assessments by Expert (34,270 points) | 746 views

2 Answers

0 like 0 dislike
solution

def countMovesToLeft(nums, target):
    count = 0
    last = 0
    for i in range(len(nums)):
        if nums[i] == target:
            count += i - last
            last += 1
    return count
        
return min(countMovesToLeft(nums, 1), countMovesToLeft(nums, 0))
by Expert (34,270 points)
0 like 0 dislike
Sort 1's and 0's in array to the end. Either end is fine.
Find the minimum number of adjacent swaps required to sort.

Input:
[1,1,1,0,0,0]

output:
0
Input:
[0,0,0,1,1,1]

output:
0
Input:
[0,0,0,0,1,0,1,0,0]

output:
5
Input:
[0,0,1,0,1,0]

output:
3
by Expert (34,270 points)