Advertisement
unknown_0711

Untitled

Nov 13th, 2022
24
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 0.93 KB | None | 0 0
  1. import java.util.*;
  2. import java.lang.*;
  3. import java.io.*;
  4.  
  5. public class Main
  6. {
  7. public static void main (String[] args) throws java.lang.Exception
  8. {
  9. Scanner sc = new Scanner(System.in);
  10. int n = sc.nextInt();
  11. int x = sc.nextInt();
  12. int[] arr = new int[n];
  13. int st = -1;
  14. int ed = -1;
  15. for(int i = 0 ; i < n ; i++){
  16. arr[i] = sc.nextInt();
  17. if(i-x >= 0 || i+x <= n-1) continue;
  18. if(st == -1) st = i;
  19. ed = i;
  20. }
  21. // st = 1 ed = 2
  22. // checking if non swap area is sorted or not
  23. st=Math.max(0,st);
  24. ed=Math.min(ed,n-1);
  25. for(int i = st ; i < ed -1 ; i++){
  26. if(arr[i] > arr[i+1]){
  27. System.out.println("NO"); // non swapping sorted or not
  28. return ;
  29. }
  30. }
  31. // non swap area either does not exist or it is sorted.
  32.  
  33. System.out.println("YES");
  34.  
  35.  
  36. }
  37. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement