-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathLeetcode_3467.cpp
44 lines (35 loc) · 1.07 KB
/
Leetcode_3467.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
#include <iostream>
#include <vector>
using namespace std;
class Solution {
public:
// Function to transform the given array
vector<int> transformArray(vector<int>& nums) {
int j = 0; // Index to track position for replacing elements
// Iterate through the array
for (int i = 0; i < nums.size(); i++) {
// If the element is even, replace it with 0
if (nums[i] % 2 == 0) {
nums[j++] = 0;
}
}
// Fill the remaining positions with 1
while (j < nums.size()) {
nums[j++] = 1;
}
return nums;
}
};
int main() {
Solution obj; // Create an instance of Solution class
// Initialize input array
vector<int> nums = {1, 2, 3, 4, 5};
// Call the transformArray function
vector<int> result = obj.transformArray(nums);
// Print the transformed array
for (int i = 0; i < result.size(); i++) {
cout << result[i] << " ";
}
cout << endl;
return 0;
}