Advertisement
unknown_0711

Untitled

Nov 14th, 2022
15
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 2.03 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. // //your code here
  10. // Scanner sc = new Scanner(System.in);
  11. // int n= sc.nextInt();
  12. // int arr[]=new int[n];
  13. // for(int i=0;i<n;i++)
  14. // arr[i]=sc.nextInt();
  15. // int key= arr[n-1];
  16. // for(int i=n-2;i>=0;i--){
  17. // if(arr[i]>key){
  18. // arr[i+1]=arr[i];
  19. // }
  20. // else {
  21. // arr[i+1]=key;
  22. // break;
  23. // }
  24. // for(int j=0;j<n;j++)System.out.print(arr[j]+" ");
  25. // System.out.println();
  26. // }
  27. // if(arr[0]>key)
  28. // arr[0]=key;
  29. // for(int j=0;j<n;j++)System.out.print(arr[j]+" ");
  30. // System.out.println();
  31.  
  32.  
  33. Scanner sc = new Scanner(System.in);
  34. int n= sc.nextInt();
  35. int arr[]=new int[n];
  36. for(int i=0;i<n;i++)
  37. arr[i]=sc.nextInt();
  38.  
  39. int key=arr[n-1]; // 3
  40. for(int i=n-2; i>=0; i--){
  41. if(arr[i] > key)
  42. arr[i+1] = arr[i];
  43. else{
  44. arr[i+1] = key;
  45. break;
  46. }
  47. for(int j=0; j<n; j++)
  48. System.out.print(arr[j] + " ");
  49. System.out.println();
  50. }
  51. if(arr[0] > key)
  52. arr[0] = key;
  53. for(int j=0; j<n; j++)
  54. System.out.print(arr[j] + " ");
  55. System.out.println();
  56. }
  57. }
  58.  
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement