-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathproblem3.cpp
More file actions
130 lines (125 loc) · 3.46 KB
/
problem3.cpp
File metadata and controls
130 lines (125 loc) · 3.46 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
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
#include <iostream>
#include <fstream>
using namespace std;
int minKBitFlips(int arr[], int n, int k) {
int* flipTracker = new int[n]();
int currentFlip = 0, flipCount = 0;
for (int i = 0; i < n; i++) {
if (i >= k) {
currentFlip ^= flipTracker[i - k];
}
if ((arr[i] + currentFlip) % 2 == 0) {
if (i + k > n) {
delete[] flipTracker;
return -1;
}
flipTracker[i] = 1;
currentFlip ^= 1;
flipCount++;
}
}
delete[] flipTracker;
return flipCount;
}
void runTestCaseFromFile(const char* filename, int choice) {
ifstream file(filename);
if (!file) {
cout << "Failed to open file: " << filename << endl;
return;
}
int numCases;
file >> numCases;
if (choice < 1 || choice > numCases) {
cout << "Invalid test case choice." << endl;
return;
}
int n, k;
int arr[100]; // Max size for test cases
for (int t = 1; t <= numCases; ++t) {
file >> n >> k;
for (int i = 0; i < n; ++i) {
file >> arr[i];
}
if (t == choice) {
cout << "Test Case " << choice << ":\n";
cout << "Input array: [";
for (int i = 0; i < n; ++i) {
cout << arr[i];
if (i != n - 1) cout << ", ";
}
cout << "], k = " << k << endl;
int result = minKBitFlips(arr, n, k);
cout << "Output: " << result << endl;
break;
}
}
file.close();
}
void runAllTestCases(const char* filename) {
std::ifstream infile(filename);
if (!infile) {
cout << "Failed to open file: " << filename << endl;
return;
}
int numCases;
infile >> numCases;
int n, k;
int arr[100];
for (int t = 1; t <= numCases; ++t) {
infile >> n >> k;
for (int i = 0; i < n; ++i) {
infile >> arr[i];
}
cout << "Test Case " << t << ":\n";
cout << "Input array: [";
for (int i = 0; i < n; ++i) {
cout << arr[i];
if (i != n - 1) cout << ", ";
}
cout << "], k = " << k << endl;
int result = minKBitFlips(arr, n, k);
cout << "Output: " << result << endl << endl;
}
infile.close();
}
void runManualTestCase() {
int n, k;
cout << "Enter the size of the array (n): ";
cin >> n;
cout << "Enter the segment size (k): ";
cin >> k;
int arr[100];
cout << "Enter the array elements (0 or 1) separated by spaces: ";
for (int i = 0; i < n; ++i) {
cin >> arr[i];
}
cout << "Input array: [";
for (int i = 0; i < n; ++i) {
cout << arr[i];
if (i != n - 1) cout << ", ";
}
cout << "], k = " << k << endl;
int result = minKBitFlips(arr, n, k);
cout << "Output: " << result << endl;
}
int main() {
cout << "Menu:\n";
cout << "1. Run Test Case 1\n";
cout << "2. Run Test Case 2\n";
cout << "3. Run Test Case 3\n";
cout << "4. Run All Test Cases\n";
cout << "5. Enter Test Case Manually\n";
cout << "Enter your choice: ";
int choice;
cin >> choice;
if (choice >= 1 && choice <= 3) {
runTestCaseFromFile("problem3.txt", choice);
} else if (choice == 4) {
runAllTestCases("problem3.txt");
} else if (choice == 5) {
runManualTestCase();
} else {
cout << "Invalid choice." << endl;
}
return 0;
}