Tips to obtain the remedy:
- Count the variety of 0’s making use of ‘for’ loophole and also shop it in a variable e.g zeroCount.
- Currently we understand that continuing to be things will certainly be 1’s.
- Run ‘for’ loophole till zeroCount, and also change the range things with 0.
- Run another ‘for’ loophole to fill up the continuing to be things with 1.
feature arrangeNumber( arr) {
allow zeroCount =0;
for( allow i= 0; i < < arr.length; i++) {
if( arr[i]== 0) {
zeroCount += 1;
}
} for( allow i= 0; i < < zeroCount; i++) {
arr[i] = 0;
}
for( allow i= zeroCount; i < < arr.length; i++) {
arr[i] = 1;
}
return arr;
}
const nums = [1,0,0,1,0,1,1];
const finalres= arrangeNumber( nums);
console.log(' result', finalres);// result: [0,0,0,1,1,1,1]
Time intricacy and also Area intricacy:
Time intricacy:- O( n)// as we do not have embedded for loopholes
Area intricacy: O( 1)// as we are just saving worths in variable and also not saving anything in range and so on
Resource:- Set apart 0’s and also 1’s in JavaScript