Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- import java.util.Arrays;
- import java.util.Random;
- public class Mahendra {
- public static void main(String[] args) {
- Random r = new Random();
- int rowsCount=Integer.parseInt(args[0]);
- int colsCount=Integer.parseInt(args[1]);
- int numberRange=Integer.parseInt(args[2]);
- int[][] elevations = new int[rowsCount][colsCount];
- for (int row = 0; row < elevations.length; row++) {
- for (int col = 0; col < elevations[row].length; col++) {
- elevations[row][col] = r.nextInt(numberRange);
- }
- }
- mostCommonVal(elevations);
- mostCommonVal2(elevations);
- }
- public static void mostCommonVal2(int[][] elevations) {
- long t=System.currentTimeMillis();
- int[] ar=new int[elevations.length*elevations[0].length];
- for(int i=0;i<elevations.length;i++)
- for(int j=0;j<elevations[0].length;j++)
- ar[i*elevations[0].length+j]=elevations[i][j];
- Arrays.sort(ar);
- int[] arr=new int[ar.length];
- arr[0]=1;
- for(int i=1;i<ar.length;i++) {
- if(ar[i]==ar[i-1])
- arr[i]=arr[i]+1;
- else
- arr[i]=1;
- }
- int max=Integer.MIN_VALUE;
- int maxIndex=-1;
- for(int i=0;i<arr.length;i++){
- if(max<arr[i]){
- maxIndex=i;
- max=arr[i];
- }
- }
- System.out.println(ar[maxIndex]);
- System.out.println("Time taken "+(System.currentTimeMillis()-t)+" ms");
- }
- public static void mostCommonVal(int[][] elevations) {
- long t=System.currentTimeMillis();
- int frequency = 0;
- int mostCommonValue = 0;
- int maxFrequency = 0;
- for (int i = 0; i < elevations.length; i++) {
- for (int j = 0; j < elevations[i].length; j++) {
- frequency = 0;
- for (int row = i; row < elevations.length; row++) {
- for (int col = 0; col < elevations[j].length; col++) {
- if (elevations[i][j] == elevations[row][col]) {
- frequency++;
- }
- }
- if (maxFrequency < frequency) {
- mostCommonValue = elevations[i][j];
- maxFrequency = frequency;
- }
- }
- }
- }
- System.out.println(mostCommonValue);
- System.out.println("Time taken "+(System.currentTimeMillis()-t)+" ms");
- }
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement