Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- #include <iostream>
- using namespace std;
- int main()
- {
- int i, j, n, k, l, count0=0, count1=0, cou=0, cou2=0;
- cin >> n >> k;
- int niza[n], tempNiza[n];
- for(i=0; i<n; i++)
- {
- cin >> niza[i];
- tempNiza[i] = niza[i];
- }
- for(i=0; i<n; i++)
- {
- if(tempNiza[i] == 0 and i+k<=n)
- {
- for(j=0, l=i; j<k; j++, l++)
- {
- if(tempNiza[l]==0)tempNiza[l] = 1;
- else tempNiza[l] = 0;
- }
- count1++;
- }
- if(niza[i] == 1 and i+k<=n)
- {
- for(j=0, l=i; j<k; j++, l++)
- {
- if(niza[l]==1)niza[l] = 0;
- else niza[l] = 1;
- }
- count0++;
- }
- }
- for(i=n-1; i>n-k; i--)
- if(niza[i]!=niza[i-1]) { cou++; break; }
- for(i=n-1; i>n-k; i--)
- if(tempNiza[i]!=tempNiza[i-1]) { cou2++; break; }
- if(cou!=0 and cou2!=0) { cout << "-1"; return 0; }
- if(count1<count0 and count1!=0 and cou2==0)cout << count1;
- else if(count0!=0 and cou==0)cout << count0;
- else cout << max(count0, count1);
- return 0;
- }
Advertisement
Add Comment
Please, Sign In to add comment