fork download
  1. /* package whatever; // don't place package name! */
  2.  
  3. import java.util.*;
  4. import java.lang.*;
  5. import java.io.*;
  6.  
  7. /* Name of the class has to be "Main" only if the class is public. */
  8. class Ideone
  9. {
  10. public static int [] delMax(int [] Arr, int numDel)
  11. {
  12. int n=Arr.length;
  13. int i,j,max=Arr[0],cmax=1;
  14. for (i=1; i<n; i++)
  15. {
  16. if (Arr[i]>max)
  17. {
  18. cmax=1;
  19. max=Arr[i];
  20. }
  21. else if (Arr[i]==max)
  22. {
  23. cmax++;
  24. }
  25. }
  26. int nm = Math.abs(numDel);
  27. if (nm > cmax) nm=cmax;
  28. int [] r = new int[n-nm];
  29. if (numDel >= 0)
  30. {
  31. j=0;
  32. for (i=0; i<n; i++)
  33. {
  34. if (Arr[i] != max)
  35. {
  36. r[j++]=Arr[i];
  37. }
  38. else
  39. {
  40. if (numDel <= 0)
  41. {
  42. r[j++]=Arr[i];
  43. }
  44. numDel--;
  45. }
  46. }
  47. }
  48. else
  49. {
  50. j=n-nm-1;
  51. for (i=n-1; i>=0; i--)
  52. {
  53. if (Arr[i] != max)
  54. {
  55. r[j--]=Arr[i];
  56. }
  57. else
  58. {
  59. if (numDel >= 0)
  60. {
  61. r[j--]=Arr[i];
  62. }
  63. numDel++;
  64. }
  65. }
  66. }
  67. return r;
  68. }
  69.  
  70. public static void printArr(int [] Arr)
  71. {
  72. for (int i=0; i<Arr.length; i++)
  73. {
  74. System.out.print(Arr[i]+" ");
  75. }
  76. System.out.println();
  77. }
  78.  
  79. public static void main (String[] args) throws java.lang.Exception
  80. {
  81. int[] times = {341, 273, 278, 329, 445, 402, 388, 275, 243, 334, 412, 393,445,445, 299, 343,445, 317, 265};
  82. System.out.println("Исходный массив:");
  83. printArr(times);
  84. System.out.println("Максимумы удалены:");
  85. printArr(delMax(times,-2));
  86. }
  87. }
Success #stdin #stdout 0.05s 2184192KB
stdin
Standard input is empty
stdout
Исходный массив:
341 273 278 329 445 402 388 275 243 334 412 393 445 445 299 343 445 317 265 
Максимумы удалены:
341 273 278 329 445 402 388 275 243 334 412 393 445 299 343 317 265