forked from Haresh1204/Algorithms
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Binary_Array_sorting.c
56 lines (49 loc) · 1.17 KB
/
Binary_Array_sorting.c
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
// { Driver Code Starts
//Initial template for C++
#include <bits/stdc++.h>
using namespace std;
// } Driver Code Ends
//User function template for C++
// binArray is an array that consists only 0s and 1s
// return sorted binary array
class Solution{
public:
vector<int> SortBinaryArray(vector<int> binArray)
{
// Your code goes here
int oneInd = -1;
for(int ind=0; ind<binArray.size(); ind++)
if(binArray[ind]==1){
oneInd = ind;
break;
}
if(oneInd==-1)
return binArray;
for(int ind=oneInd+1; ind<binArray.size(); ind++)
if(binArray[ind]==0){
swap(binArray[ind], binArray[oneInd]);
oneInd++;
}
return binArray;
}
};
// { Driver Code Starts.
int main() {
int t;
cin>>t;
while(t--)
{
int n;
cin>>n;
vector<int> binArray(n);
for(int i = 0; i < n; i++)
cin>>binArray[i];
Solution ob;
vector<int> result = ob.SortBinaryArray(binArray);
for(int i=0; i<n; i++)
cout<<result[i]<<" ";
cout<<endl;
}
return 0;
}
// } Driver Code Ends