Segregate 0's and 1's in an array || Sort an array of 0's and 1's

preview_player
Показать описание
Segregate 0's and 1's in an array || Sort an array of 0's and 1's
#programming #codes #kkcodes

Other Related Videos

Follow KK Codes on
Рекомендации по теме
Комментарии
Автор

Nice explanation..
if anyone needs code here it is ..
program is easy.
we have iterate over the array, if we find 0 we need to assign the value in first item index and increment the index else we have to decrement the last item in the array and copy the values of original array into the temp array.

//Program
class segarr{
public static void main(String args[]){
int arr[] = {0, 1, 0, 1, 1, 0, 1};
int n = arr.length;
int temp[] = new int[n];
System.out.println("Original Array is :");
for(int i=0;i<n;i++){
System.out.print(arr[i]+ " ");
}
int firstitem=0, lastitem=n-1;
for(int i=0;i<n;i++){
if(arr[i]==0){
temp[firstitem]=arr[i];
firstitem++;
}
else{
temp[lastitem]=arr[i];
lastitem--;
}
}

System.out.println("Segerated array is :");
for(int i=0;i<n;i++){

System.out.println(temp[i]+ " ");
}
}
}

RakeshSingh-hbrj
Автор

I really appreciate your approach to solve the 0s and 1s array sorting problem

chakreshsahu
Автор

Just iterate through array and count number of 0 and 1..
In temp array, add 0' s and 1's those many times...simple

AutomationTestingStudio
Автор

execute on computer it will be helpful ..carry on you are teaching good

IMRAN-bwtb
Автор

we could also do this problem by two-pointers and swapping zero and 1 whenever needed.

pranavmishra
Автор

Please write this on a computer and compile it!

srinidhiarekal
Автор

please mam upload more videos on this series why you stop uploading now, please upload

SansaniKhabarlive
Автор

bilkul bhi smj ni aaya or confuse kr diya

snehabajpai