Segregate 0s and 1s in an array

 Problem statement:- Program to Segregate 0s and 1s in an array.

Example:-

              Input : Given 

                                size=5

                                 arr[]=[1,0,1,0,0] 

             Output:

                        arr[]=[0,0,0,1,1]            

Data requirement:-

   Input Data:- size, arr

  Output Data:-arr

  Additional Data:- i, c

Program in C

Here is the source code of the C Program to Segregate 0s and 1s in an array.

Code:

#include<stdio.h>
main()
{
    printf("Enter the size of the array:");
    int size;
    scanf("%d",&size);
    int arr[size];
    int i;
    printf("Enter the Element of the array(only 0s and 1s):\n");
    for(i=0;i<size;i++)
    {
        scanf("%d",&arr[i]);
    }

    int c=0;

    for(i=0;i<size;i++)
    {
        if(arr[i]==0)
        {
            c++;
        }
    }
    for(i=0;i<c;i++){
        arr[i]=0;
    }
     for(i=c;i<size;i++){
        arr[i]=1;
    }

    printf("After segregate 0s and 1s in an Array, Array is:");
    for(i=0;i<size;i++)
    {
        printf("%d ",arr[i]);
    }
}

Input/Output:
Enter the size of the array:5
Enter the Element of the array(only 0s and 1s):
1 0 1 0 0
After segregate 0s and 1s in an Array, Array is:0 0 0 1 1

Program in C++

Here is the source code of the C++ Program to Segregate 0s and 1s in an array.

Code:

#include<iostream>
using namespace std;
main()
{
    cout<<"Enter the size of the array:";
    int size;
    cin>>size;
    int arr[size];
    int i,temp=0,j;
    cout<<"Enter the Element of the array(only 0s and 1s):\n";
    for(i=0;i<size;i++)
    {
        cin>>arr[i];
    }
   int c=0;

    for(i=0;i<size;i++)
    {
        if(arr[i]==0)
        {
            c++;
        }
    }
    for(i=0;i<c;i++){
        arr[i]=0;
    }
     for(i=c;i<size;i++){
        arr[i]=1;
    }

    cout<<"After segregate 0s and 1s in an Array, Array is:";
    for(i=0;i<size;i++)
    {
        cout<<arr[i]<<" ";
    }
}


Input/Output:
Enter the size of the array:6
Enter the Element of the array(only 0s and 1s):
1 0 1 1 0 1
After segregate 0s and 1s in an Array, Array is:0 0 1 1 1 1

Program in Java

Here is the source code of the Java Program to Segregate 0s and 1s in an array.

Code:

import java.util.Scanner;
public class Segregate0sAnd1s {

public static void main(String[] args) {
Scanner sc=new Scanner(System.in);
  System.out.println("Enter the size of the array:");
     int size,i;
     size=sc.nextInt();
     int arr[ ]=new int[size];
  System.out.println("Enter the Element of the array(only 0s and 1s):");
      for(i=0;i<size;i++)
     {
         arr[i]=sc.nextInt();
     }
      int c=0;

      for(i=0;i<size;i++)
      {
          if(arr[i]==0)
          {
              c++;
          }
      }
      for(i=0;i<c;i++){
          arr[i]=0;
      }
       for(i=c;i<size;i++){
          arr[i]=1;
      }
      System.out.println("After segregate 0s and 1s in an Array, Array is:");
      for(i=0;i<size;i++)
      {
      System.out.print(arr[i]+" ");
      }
          sc.close();
}}

Input/Output:
Enter the size of the array:
7
Enter the Element of the array(only 0s and 1s):
0 1 0 0 1 1 0
After segregate 0s and 1s in an Array, Array is:
0 0 0 0 1 1 1 

Program in Python

Here is the source code of the Python Program to Segregate 0s and 1s in an array.

Code:

arr=[]
size = int(input("Enter the size of the array: "))
print("Enter the Element of the array(only 0s and 1s):")
for i in range(0,size):
    num = int(input())
    arr.append(num)

c=0
for i in range(0,size):
    if arr[i]==0:
        c+=1

for i in range(0,c):
        arr[i]=0

for i in range(c,size):
        arr[i]=1

print("After segregate 0s and 1s in an Array, Array is:")
print(arr)

Input/Output:
Enter the size of the array: 3
Enter the Element of the array(only 0s and 1s):
0
1
0
After segregate 0s and 1s in an Array, Array is:
[0, 0, 1]


Post a Comment

0 Comments