Advertisement
Guest User

Untitled

a guest
Jan 24th, 2020
89
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 2.62 KB | None | 0 0
  1. import java.util.Arrays;
  2. import java.util.Random;
  3.  
  4. public class Mahendra {
  5. public static void main(String[] args) {
  6.  
  7. Random r = new Random();
  8. int rowsCount=Integer.parseInt(args[0]);
  9. int colsCount=Integer.parseInt(args[1]);
  10. int numberRange=Integer.parseInt(args[2]);
  11. int[][] elevations = new int[rowsCount][colsCount];
  12.  
  13. for (int row = 0; row < elevations.length; row++) {
  14. for (int col = 0; col < elevations[row].length; col++) {
  15. elevations[row][col] = r.nextInt(numberRange);
  16. }
  17. }
  18.  
  19. mostCommonVal(elevations);
  20. mostCommonVal2(elevations);
  21. }
  22.  
  23.  
  24.  
  25.  
  26.  
  27. public static void mostCommonVal2(int[][] elevations) {
  28. long t=System.currentTimeMillis();
  29. int[] ar=new int[elevations.length*elevations[0].length];
  30. for(int i=0;i<elevations.length;i++)
  31. for(int j=0;j<elevations[0].length;j++)
  32. ar[i*elevations[0].length+j]=elevations[i][j];
  33.  
  34. Arrays.sort(ar);
  35.  
  36.  
  37.  
  38. int[] arr=new int[ar.length];
  39. arr[0]=1;
  40. for(int i=1;i<ar.length;i++) {
  41. if(ar[i]==ar[i-1])
  42. arr[i]=arr[i]+1;
  43. else
  44. arr[i]=1;
  45. }
  46.  
  47. int max=Integer.MIN_VALUE;
  48. int maxIndex=-1;
  49.  
  50. for(int i=0;i<arr.length;i++){
  51. if(max<arr[i]){
  52. maxIndex=i;
  53. max=arr[i];
  54. }
  55. }
  56.  
  57. System.out.println(ar[maxIndex]);
  58. System.out.println("Time taken "+(System.currentTimeMillis()-t)+" ms");
  59.  
  60.  
  61.  
  62.  
  63.  
  64. }
  65.  
  66.  
  67. public static void mostCommonVal(int[][] elevations) {
  68. long t=System.currentTimeMillis();
  69.  
  70. int frequency = 0;
  71. int mostCommonValue = 0;
  72. int maxFrequency = 0;
  73.  
  74. for (int i = 0; i < elevations.length; i++) {
  75. for (int j = 0; j < elevations[i].length; j++) {
  76. frequency = 0;
  77. for (int row = i; row < elevations.length; row++) {
  78. for (int col = 0; col < elevations[j].length; col++) {
  79. if (elevations[i][j] == elevations[row][col]) {
  80. frequency++;
  81. }
  82. }
  83. if (maxFrequency < frequency) {
  84. mostCommonValue = elevations[i][j];
  85. maxFrequency = frequency;
  86. }
  87. }
  88. }
  89. }
  90.  
  91. System.out.println(mostCommonValue);
  92. System.out.println("Time taken "+(System.currentTimeMillis()-t)+" ms");
  93.  
  94. }
  95. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement