Minimum Swaps required to group all 1s together | Practice | GeeksforGeeks

Given an array of 0’s and 1’s, we need to write a program to find the minimum number of swaps required to group all 1’s present in the array together.
Example 1:
â€‹Input : arr[ ] = {1, 0, 1, 0, 1}
Output : 1
Explanation:
Only 1 swap is required to group all 1's together. 
Swapping index 1 and 4 will give arr[] = {1, 1, 1, 0, 0}.


â€‹Example 2:
Input : arr[ ] = {1, 0, 1, 0, 1, 1} 
Output :  1
 
Your Task:
This is a function problem. The input is already taken care of by the driver code. You only need to complete the function minSwaps() that takes an array (arr), sizeOfArray (n) and return the minimum number of swaps required and if no 1's are present print -1. The driver code takes care of the printing.
Expected Time Complexity: O(N).
Expected Auxiliary Space: O(1).
 
Constraints:
1 ≤ N  ≤ 105
0 ≤ Arri ≤ 1
+1
sknwd88643 months ago
int minSwaps(int arr[], int n) {
    
    int k = 0;
    
    for(int i = 0; i < n; i++)
    {
        if(arr[i])
        k++;
    }
    
    if(k == 0)
    return -1;
    
    int maxi = INT_MIN;
    
    int curr = 0;
    
    for(int i = 0; i < n; i++)
    {
        if(i < k)
        {
            if(arr[i])
            {
                curr++;
            }
        }
        
        else
        {
            maxi = max(maxi, curr);
            
            if(arr[i])
            {
                curr++;
            }
            
            if(arr[i - k])
            {
                curr--;
            }
        }
    }
    
    maxi = max(maxi, curr);
    
    return k - maxi ;
}
0
shikharsrivastava20023 months ago
int minSwaps(int arr[], int n) {
    
    // Complete the function
    int ones=0;
    for(int i=0;i<n;i++)
    {
        if(arr[i])
        ones+=1;
    }
    if(!ones)
    return -1;
    int left=0;
    int right=0;
    
    int oc=0;
    int zeros=0;
    int minzeros=0;
    for(right=0;right<n;right++)
    {
        if(arr[right]==0)
        {
            zeros+=1;
        }
        oc+=1;
        if(oc==ones)
        {
            break;
        }
    
    }
    minzeros=zeros;
    
    while(right<n)
    {
        if(arr[left]==0)
        {
            zeros-=1;
        }
        left+=1;
        right+=1;
        if(arr[right]==0)
        {
            zeros+=1;
        }
        if(right<n)
        minzeros=min(minzeros,zeros);
    }
return minzeros;

}
+1
defendersatya53 months ago
not a easy question though.
0
chessnoobdj4 months ago
C++
int minSwaps(int arr[], int n) {
    int cnt_one = 0;
    for(int i=0; i<n; i++){
        if(arr[i])
            cnt_one += 1;
    }
    if(!cnt_one)
        return -1;
    int cnt = 0;
    for(int i=0; i<cnt_one; i++){
        if(arr[i])
            cnt += 1;
    }
    int ans = cnt_one-cnt, l=0, r=cnt_one;
    for(int i=0; i+cnt_one<n; i++){
        //cout << cnt << " " << l << " " << r << " " << i << "\n"; 
        if(arr[l++])
            cnt -= 1;
        if(arr[r++])
            cnt += 1;
        ans = min(ans, cnt_one-cnt);
        //cout << cnt << " " << ans << "\n";
    }
    return ans;
}
+1
imranwahid5 months ago
Easy C++ solution
0
tonyapstark
This comment was deleted.
0
VickySam19017 months ago
VickySam1901
Time Complexity: O(N).Auxiliary Space: O(1).Sliding Window Approach https://uploads.disquscdn.c...
-1
aditya anand10 months ago
aditya anand
Elegant Python3 Solutiondef minSwaps (arr, n) :      total_ones=arr.count(1)    temp_ones,max_ones=arr[:total_ones].count(1),0    max_ones=temp_ones    for i in range(total_ones,n):         if arr[i]==1: temp_ones+=1        if arr[i-total_ones]==1: temp_ones-=1         max_ones=max(max_ones,temp_ones)    return total_ones-max_ones if total_ones>0 else -1 
0
PK11 months ago
PK
int minSwaps(int a[], int n) {
    int tcount_one=0;    for(int i=0;i<n;i++) {="" tcount_one+="a[i];" }="" if(tcount_one="=0)" {="" return="" -1;="" }="" int="" count="0,max_count=0,i=0,j=0;" while(j<n)="" {="" count+="a[j];" if((j-i+1)="=" tcount_one)="" {="" max_count="max(max_count,count);" if(a[i]="=1)" {="" count--;="" }="" i++;="" }="" j++;="" }="" return="" tcount_one-max_count;="" }="">
0
prerna singh2 years ago
prerna singh
It gives incorrect output for this:
291 0 0 1 0 1 1 0 0 1 0 0 0 0 1 0 1 0 0 0 0 1 1 1 1 0 1 1 1
I do it in my ide it gives 6 which is correct. But while submitting code here, it gives 3 as output
We strongly recommend solving this problem on your own before viewing its editorial. Do you still
                    want to view the editorial?
 Login to access your submissions. 

Problem


Contest

Reset the IDE using the second button on the top right corner.
Avoid using static/global variables in your code as your code is tested against multiple test cases and these tend to retain their previous values.
Passing the Sample/Custom Test cases does not guarantee the correctness of code. On submission, your code is tested against multiple test cases consisting of all possible corner cases and stress constraints.
You can access the hints to get an idea about what is expected of you as well as the final solution code.
You can view the solutions submitted by other users from the submission tab.