Skip to main content

Command Palette

Search for a command to run...

0 and 1 in java

Published
1 min readView as Markdown
// User function Template for Java

class Solution {
    void segregate0and1(int[] arr) {

     int left=0;
     int right=arr.length-1;

     while(left<right)
     {
         if(arr[left]==0)
         {
             left++;
         }
         else if(arr[right]==1)
         {
             right--;
         }
         else
         {
             swap(arr,left,right);
             left++;
             right--;
         }
     }
    }

     void swap(int []arr,int left,int right)
     {
         int temp= arr[left];
         arr[left]=arr[right];
         arr[right]=temp;
     }



}

More from this blog

Amit singh's blog

235 posts