-
Notifications
You must be signed in to change notification settings - Fork 1
/
Binary Array Sorting.cpp
76 lines (61 loc) · 1.12 KB
/
Binary Array Sorting.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
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
//{ Driver Code Starts
// A Sample C++ program for beginners with Competitive Programming
#include <bits/stdc++.h>
using namespace std;
// } Driver Code Ends
class Solution{
public:
// A[]: input array
// N: input array
//Function to sort the binary array.
void binSort(int A[], int N)
{
//Your code here
/**************
* No need to print the array
* ************/
int zeros = 0;
int ones = 0;
for(int i =0; i < N; i++)
{
if(A[i] == 1)
ones++;
else
zeros++;
}
int i=0;
while(zeros != 0)
{
A[i] = 0;
i++;
zeros--;
}
while(ones != 0)
{
A[i]=1;
i++;
ones--;
}
}
};
//{ Driver Code Starts.
int main() {
int T;
cin>>T;
// Input the number of testcases
while(T--)
{
int N;
cin>>N; //Input size of array N
int A[N];
for(int i = 0; i < N; i++)
cin>>A[i];
Solution obj;
obj.binSort(A,N);
for(int x:A)
cout<<x<<" ";
cout<<endl;
}
return 0;
}
// } Driver Code Ends