forked from TECHOUS/DSKaKhel
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathReplace_Even_With_first.cpp
More file actions
84 lines (65 loc) · 1.1 KB
/
Replace_Even_With_first.cpp
File metadata and controls
84 lines (65 loc) · 1.1 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
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
77
78
79
80
81
82
83
84
#include<iostream>
#include<limits>
#include<stack>
using namespace std;
/*
The question states
Input:
3 -> odd so array will not be bothered
6
36
61
121
38
56
36
3
6
-1
Output:
3
6
61
121
38
3
6
*/
int processArray(int *arr,int len){
stack <int> st;
int j = 0;
for(int i = 0 ; i < len ; i++){
if(arr[i] % 2 != 0){
st.push(arr[i]);
}
else if(arr[i] % 2 == 0){
st.push(arr[i]);
while(arr[i] % 2 == 0){
i++;
}
st.push(arr[i]);
}
}
while(!st.empty()){
arr[j++] = st.top();
st.pop();
}
len = j;
return len;
}
int main(){
int x;
int len = 1;
int *arr = new int [10000];
cin>>x;
arr[0] = x;
while(x != -1){
cin>>x;
arr[len++] = x;
}
int res = processArray(arr,len);
cout<<"The length of the modified array is " <<res<<endl;
for(int i = res-1 ; i > 0 ; i--){
cout<<arr[i]<<endl;
}
}