Program to Segregate 0s on Left Side & 1s on right side of the Array

Method-1 

a=list(map(int,input().split()))
i,j=0,len(a)-1
while i<j:
    while a[i]==0 and i<j:
        i+=1
    while a[j]==1 and i<j:
        j-=1
    if i<j:
        a[i]=0
        a[j]=1
        i+=1
        j-=1
print(a)


Method-2

a=list(map(int,input().split()))
c=0
for i in range(len(a)):
    if a[i]==0:
        c+=1
for i in range(c):
    a[i]=0
for i in range(c,len(a)):
    a[i]=1

print(a)
        



  Input: 1 0 1 0 1 1 1 0 0 1 0 Output: 0, 0, 0, 0, 0, 1, 1, 1, 1, 1, 1

No comments:

Post a Comment