-
Notifications
You must be signed in to change notification settings - Fork 0
/
3-8-24Create Binary Array Sorting.cpp
73 lines (55 loc) · 1.24 KB
/
3-8-24Create 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
//{ 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 countOne = 0;
int countZero = 0;
for(int i=0; i<N; i++){
if(A[i] == 1) countOne++;
else countZero++;
}
int k = 0;
while(k < N){
if(countZero > 0) {
countZero--;
A[k++] = 0;
}else{
countOne--;
A[k++] = 1;
}
}
}
};
//{ 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