Commonly asked Data Structures and Algorithms Problems by big tech and different solution approaches with code in Java and C

Powered by Blogger.

Wednesday, June 14, 2017

Migratory Birds Hackerrank solution Java


Problem : Find maximum times a number appeared in range 1 to 5

import java.io.*;
import java.util.*;
import java.text.*;
import java.math.*;
import java.util.regex.*;

public class Solution {

    static int migratoryBirds( int[] ar) {
        // Complete this function
        Arrays.sort(ar);
        return ar[5];
    }

    public static void main(String[] args) {
        Scanner in = new Scanner(System.in);
        int n = in.nextInt();
        int[] ar = new int[6];
        int[] arr=new int[6];
        Arrays.fill(ar,0);
        for(int ar_i = 0; ar_i < n; ar_i++){
            int temp=in.nextInt();
            ar[temp]++;
            arr[temp]++;
        }
        int max =migratoryBirds(ar);
        for(int i=0;i<6;i++)
            {
            if(arr[i]==max)
                {
                System.out.println(i);
            break;}
        }
        //System.out.println(result);
    }
}

0 Comments:

Post a Comment

Stats