Advertisement
unknown_0711

Untitled

Oct 18th, 2022
35
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 1.14 KB | None | 0 0
  1.  
  2. Scanner sc = new Scanner(System.in);
  3. int n = sc.nextInt();
  4. int k = sc.nextInt();
  5. int arr[] = new int[n];
  6. for (int i = 0 ; i < n ; i++ ) {
  7. arr[i] = sc.nextInt();
  8. }
  9. int cnt1, i, j, odd;
  10. i=0; j=0; cnt1=0;
  11. odd=0; if(arr[i]%2==1)odd=1;
  12.  
  13. while(j<n){
  14.  
  15. if(odd<=k){
  16. cnt1+=j-i+1;
  17. j++;
  18. if(j<n && (arr[j]%2==1))odd++;
  19. }
  20. else {
  21. if(arr[i]%2==1)odd--;
  22. i++;
  23. }
  24. }
  25.  
  26. int cnt2=0;
  27. i=0; j=0; cnt2=0;
  28. odd=0; if(arr[i]%2==1)odd=1;
  29. while(j<n){
  30. if(odd<=k-1){
  31. cnt2-=j-i+1; j++;
  32. if(j<n && (arr[j]%2==1))odd++;
  33. }
  34. else {
  35. if(arr[i]%2==1)odd--;
  36. i++;
  37. }
  38. }
  39. System.out.print(Math.abs(cnt1)- Math.abs(cnt2));
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement