-
-
Notifications
You must be signed in to change notification settings - Fork 0
/
segregate-0-and-1.cpp
61 lines (54 loc) · 1.08 KB
/
segregate-0-and-1.cpp
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
/*
Description: A program to segregate the array of 0s and 1s
Approach: Counting the number of 0s present in the array and
saving it in variable z. For the first z indexes, putting 0 and
for the remaining indexes putting 1.
Time complexity: O(n)
*/
#include <iostream>
#include <vector>
using namespace std;
void segregate0and1(vector<int> &v, int n)
{
int z = 0;
for (int i = 0; i < n; i++)
{
//counting the number of 0s and storing it the variable z
if (v[i] == 0)
{
z++;
}
}
//for z indexes, putting 0 in the vector
//for remaining indexes putting 1 in the vector
for (int j = 0; j < n; j++)
{
if (j < z)
{
v[j] = 0;
}
else
{
v[j] = 1;
}
}
}
int main()
{
int n;
cout << "Enter number of array elements\n";
cin >> n;
vector<int> v(n);
cout << "Enter the array elements (only 0 and 1)\n";
for (int i = 0; i < n; i++)
{
cin >> v[i];
}
//calling the function
segregate0and1(v, n);
cout << "After segregating, the array is: \n";
for (int j = 0; j < n; j++)
{
cout << v[j] << " ";
}
}