Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Create SolutionbyPooja2.cpp #1125

Merged
merged 1 commit into from Aug 2, 2020
Merged
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
@@ -0,0 +1,49 @@
//An Array is given with 0s and 1s and you need to sort the array.


#include<iostream>
using namespace std;

void sort(int *arr, int n)
{
int i, j;
i = 0;
j = n - 1;

while(i <= j)
{

if(arr[i] == 1 && arr[j] == 0)
{
arr[i] = 0;
arr[j] = 1;
}

if(arr[i] != 1)
i++;
if(arr[j] != 0)
j--;

}
}

int main()
{
int size;

cout<<"Enter size ";
cin>>size;


int *arr = new int[size];
cout<<"Enter the array element(only 0 and 1) ";
for(int i = 0; i < size; i++)
cin >> arr[i];

sort(arr, size);

for(int i = 0; i < size; i++)
cout << arr[i] << " ";

}