Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- Scanner sc = new Scanner(System.in);
- int n = sc.nextInt();
- int k = sc.nextInt();
- int arr[] = new int[n];
- for (int i = 0 ; i < n ; i++ ) {
- arr[i] = sc.nextInt();
- }
- int cnt1, i, j, odd;
- i=0; j=0; cnt1=0;
- odd=0; if(arr[i]%2==1)odd=1;
- while(j<n){
- if(odd<=k){
- cnt1+=j-i+1;
- j++;
- if(j<n && (arr[j]%2==1))odd++;
- }
- else {
- if(arr[i]%2==1)odd--;
- i++;
- }
- }
- int cnt2=0;
- i=0; j=0; cnt2=0;
- odd=0; if(arr[i]%2==1)odd=1;
- while(j<n){
- if(odd<=k-1){
- cnt2-=j-i+1; j++;
- if(j<n && (arr[j]%2==1))odd++;
- }
- else {
- if(arr[i]%2==1)odd--;
- i++;
- }
- }
- System.out.print(Math.abs(cnt1)- Math.abs(cnt2));
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement