-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathblankspace.cpp
More file actions
72 lines (58 loc) · 1.78 KB
/
blankspace.cpp
File metadata and controls
72 lines (58 loc) · 1.78 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
#include <iostream>
using namespace std;
class BlankSpace {
int* a; // Dynamic array
public:
BlankSpace(int n) {
cout << "constr" << endl;
a = new int[n]; // Allocate memory
}
~BlankSpace() {
cout << "destru" << endl;
delete[] a; // Free memory
}
void readArray(int n) {
cout << "read arr" << endl;
for (int i = 0; i < n; i++) {
while (!(cin >> a[i])) { // Check for valid input
cout << "Invalid input! Enter an integer: ";
cin.clear(); // Clear error flag
cin.ignore(1000, '\n'); // Ignore invalid input
}
}
}
int findMaxBlankSpace(int n) {
cout << "func" << endl;
int max_blank_space = 0, current_blank_space = 0;
for (int i = 0; i < n; i++) {
if (a[i] == 0) {
current_blank_space++;
} else {
if (current_blank_space > max_blank_space) {
max_blank_space = current_blank_space;
}
current_blank_space = 0;
}
}
// Final update in case the longest blank space is at the end
if (current_blank_space > max_blank_space) {
max_blank_space = current_blank_space;
}
return max_blank_space;
}
};
int main() {
int t;
cout << "t" << endl;
cin >> t; // Number of test cases
while (t--) {
int n;
cout << "n" << endl;
cin >> n; // Size of array
BlankSpace obj(n); // Create object
obj.readArray(n); // Read array input
cout << obj.findMaxBlankSpace(n) << endl; // Find and print max blank space
cin.ignore(1000, '\n'); // Clears leftover input after each test case
}
return 0;
}