-
Notifications
You must be signed in to change notification settings - Fork 19
/
dsacpp
70 lines (56 loc) · 1.26 KB
/
dsacpp
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
Check if any pair of consecutive 1s can be separated by at most M 0s by circular rotation of a Binary String
// C++ program for the above approach
#include <bits/stdc++.h>
using namespace std;
// Function to check if any pair of
// consecutive 1s can be separated by at
// most M 0s by circular rotation of string S
void rotateString(int n, int m, string s)
{
// Stores the indices of all 1s
vector<int> v;
// Store the number of pairs
// separated by at least M 0s
int cnt = 0;
// Traverse the string
for (int i = 0; i < n; i++) {
if (s[i] == '1') {
// Store the current index
v.push_back(i);
}
}
// Traverse the array containing indices
for (int i = 1; i < (int)v.size(); i++) {
// If the number of 0s > M,
// then increment cnt by 1
if ((v[i] - v[i - 1] - 1) > m) {
// Increment cnt
cnt++;
}
}
// Check if at least M '0's lie between
// the first and last occurrence of '1'
if (v.size() >= 2
&& (n - (v.back() - v[0]) - 1) > m) {
// Increment cnt
cnt++;
}
// If the value of cnt <= 1, then
// rotation of string is possible
if (cnt <= 1) {
cout << "Yes";
}
// Otherwise
else {
cout << "No";
}
}
// Driver Code
int main()
{
string S = "101001";
int M = 1;
int N = S.size();
rotateString(N, M, S);
return 0;
}