-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathdelete_dup_from_sorted_array.cpp
More file actions
49 lines (39 loc) · 1.14 KB
/
Copy pathdelete_dup_from_sorted_array.cpp
File metadata and controls
49 lines (39 loc) · 1.14 KB
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
//delete duplicates from sorted array
#include<bits/stdc++.h>
using namespace std;
int mod=1e9+7;
#define F(a,b,var) for(int var=a;var<b;var++)
#define FAST_INP ios_base::sync_with_stdio(false);cin.tie(NULL)
int validEnteries(vector<int> *A_ptr){
vector<int> &A = *A_ptr;
int j = 0;
for(int i=0; i< A.size() - 1; i++){
if(A[i] != A[i+1]){
A[j++] = A[i];
}
}
A[j++] = A[A.size()-1];
//for(int i=0; i<A.size();i++)cout<<A[i]<<" ";
return j; //returns last valid entry after which 0's have to be inserted
}
int main()
{
//code
FAST_INP;
int T;
cin>>T;
while(T--)
{
int m; cin>>m;
if(m == 0) return 0;
vector<int> A(m);
for(int i=0; i<m; i++)
cin>>A[i];
int ind = validEnteries(&A);
cout<<"\n"<<ind<<"";
for(int i=ind; i<m; i++)
A[i] = 0; //remaining enteries
for(int i=0; i<A.size(); i++)
cout<<A[i]<<" ";
}
}