-
Notifications
You must be signed in to change notification settings - Fork 0
/
ReplaceEven-Odd.cpp
93 lines (80 loc) · 1.7 KB
/
ReplaceEven-Odd.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
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
/*
Given an array arr[] of size N containing equal number of odd and even numbers.
Arrange the numbers in such a way that all the even numbers get the even index and odd numbers get the odd index.
Note: There are multiple possible solutions, Print any one of them. Also, 0-based indexing is considered.
Input:
arr[6] = {3, 6, 12, 1, 5, 8}
Output:
1
Explanation:
6 3 12 1 8 5 is a possible solution.
The output will always be 1 if your
rearrangement is correct.
*/
#include <bits/stdc++.h>
using namespace std;
class Solution
{
public:
void reArrange(int arr[], int N)
{
/*for(int i=0;i<N;i+=2)
{
if(arr[i+1]%2==0)
swap(arr[i],arr[i+1]);
}*/
int i = 0, j = 1;
while (i < N && j < N)
{
while (i < N && arr[i] % 2 == 0)
i += 2;
while (j < N && arr[j] % 2 == 1)
j += 2;
swap(arr[i], arr[j]);
i += 2;
j += 2;
}
}
};
int check(int arr[], int n)
{
int flag = 1;
int c = 0, d = 0;
for (int i = 0; i < n; i++)
{
if (i % 2 == 0)
{
if (arr[i] % 2)
{
flag = 0;
break;
}
else
c++;
}
else
{
if (arr[i] % 2 == 0)
{
flag = 0;
break;
}
else
d++;
}
}
if (c != d)
flag = 0;
return flag;
}
int main()
{
int N;
cin >> N;
int arr[N];
for (int i = 0; i < N; i++)
cin >> arr[i];
Solution ob;
ob.reArrange(arr, N);
cout << check(arr, N) << endl;
}