Skip to content

Commit

Permalink
Add files via upload
Browse files Browse the repository at this point in the history
  • Loading branch information
Sandigupta authored Apr 3, 2024
1 parent 7b07867 commit 9c61e59
Showing 1 changed file with 80 additions and 0 deletions.
80 changes: 80 additions & 0 deletions STRING/Maximum_num_of_consicutive_one.cpp
Original file line number Diff line number Diff line change
@@ -0,0 +1,80 @@
// #include <bits/stdc++.h>
// using namespace std;
// int main()
// {
// string str;
// cin >> str;

// int k;
// cin >> k;

// // int maxZero = 0;
// int maxLenth = INT_MIN;
// int idx = -1;
// int currentZero = 0;
// for (int i = 0; i < str.length() - 1; i++)
// {
// currentZero = 0;
// for (int j = i; j < str.length()-1; j++)
// {
// if (str[j] == '0')
// {
// currentZero++;
// if (currentZero <= k)
// {
// idx = j;
// }
// }
// else
// {
// if (str[j] != '0')
// {
// idx = j;
// }
// else
// {
// break;
// }
// }
// if(j==str.length())
// break;
// }
// maxLenth = max((idx - i)+1, maxLenth);
// }
// cout << maxLenth << endl;
// }

#include <bits/stdc++.h>
using namespace std;

int main() {
string str;
cin >> str;

int k;
cin >> k;

int maxLenth = INT_MIN;
int idx = -1;
int currentZero = 0;

for (int i = 0; i < str.length(); i++) {
currentZero = 0;
for (int j = i; j < str.length(); j++) {
if (str[j] == '0') {
currentZero++;
if (currentZero > k) {
break; // Break if more than k zeros encountered
}
}
idx = j; // Update index regardless of character
maxLenth = max(maxLenth, idx - i + 1); // Update maxLenth inside loop
}
}

cout << maxLenth << endl;

return 0;
}
// time complexcity=O(n*n);
// Space complexcity= O(1);

0 comments on commit 9c61e59

Please sign in to comment.