forked from riyaaa04/cpp20
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy path62.cpp
More file actions
67 lines (54 loc) · 1.47 KB
/
62.cpp
File metadata and controls
67 lines (54 loc) · 1.47 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
#include <iostream>
using namespace std;
// Function to segregate even and odd numbers in an array
void segregateEvenOdd(int arr[], int size)
{
int left = 0; // Index of the leftmost element
int right = size - 1; // Index of the rightmost element
while (left < right)
{
// Move left index to the right while arr[left] is even
while (arr[left] % 2 == 0 && left < right)
{
left++;
}
// Move right index to the left while arr[right] is odd
while (arr[right] % 2 != 0 && left < right)
{
right--;
}
if (left < right)
{
// Swap arr[left] and arr[right]
int temp = arr[left];
arr[left] = arr[right];
arr[right] = temp;
left++;
right--;
}
}
}
int main()
{
int size1;
cout << "Enter a size of array : ";
cin >> size1;
int arr[size1];
cout << "Enter "<< size1 << " numbers into the array : \n";
for(int i = 0; i < size1; i++){
cin >> arr[i];
}
int size = sizeof(arr) / sizeof(arr[0]);
cout << "Original Array: ";
for (int i = 0; i < size; i++) {
cout << arr[i] << " ";
}
cout << endl;
segregateEvenOdd(arr, size);
cout << "Array after segregating even and odd numbers: ";
for (int i = 0; i < size; i++) {
cout << arr[i] << " ";
}
cout << endl;
return 0;
}