HackerRank Migratory Birds Solution

Hello Programmers, In this post, you will learn how to solve HackerRank Migratory Birds Solution. This problem is a part of the HackerRank Algorithms Series.

One more thing to add, don’t straight away look for the solutions, first try to solve the problems by yourself. If you find any difficulty after trying several times, then look for the solutions. We are going to solve the HackerRank Algorithms problems using C, CPP, JAVA, PYTHON, JavaScript & SCALA Programming Languages.

You can practice and submit all HackerRank problem solutions in one place. Find a solution for other domains and Sub-domain. I.e. Hacker Rank solution for HackerRank C ProgrammingHackerRank C++ ProgrammingHackerRank Java Programming, HackerRank Python ProgrammingHackerRank Linux ShellHackerRank SQL Programming, and HackerRank 10 days of Javascript.

HackerRank Migratory Birds Solution
HackerRank Migratory Birds Solution

As you already know that this site does not contain only the Hacker Rank solutions here, you can also find the solution for other problems. I.e. Web Technology, Data StructuresRDBMS ProgramsJava Programs Solutions,  Fiverr Skills Test answersGoogle Course AnswersLinkedin Assessment, and Coursera Quiz Answers.

HackerRank Migratory Birds

Task

Given an array of bird sightings where every element represents a bird type id, determine the id of the most frequently sighted type. If more than 1 type has been spotted that maximum amount, return the smallest of their ids.

Example

arr = [1, 1, 2, 2, 3]
There are two each of types 1 and 2, and one sighting of type 3. Pick the lower of the two types seen twice: type 1.

Function Description

Complete the migratoryBirds function in the editor below.

migratoryBirds has the following parameter(s):

  • int arr[n]: the types of birds sighted

Returns

  • int: the lowest type id of the most frequently sighted birds

Input Format

The first line contains an integer, n, the size of arr.
The second line describes arr as n space-separated integers, each a type number of the bird sighted.

Constraints

  • 5 <= n <= 2 x 105
  • It is guaranteed that each type is 1, 2, 3, 4, or 5.

Sample Input 0

6
1 4 4 4 5 3

Sample Output 0

4

Explanation 0

The different types of birds occur in the following frequencies:

  • Type 1: 1 bird
  • Type 2: 0 birds
  • Type 3: 1 bird
  • Type 4: 3 birds
  • Type 5: 1 bird

The type number that occurs at the highest frequency is type 4, so we print 4 as our answer.

Sample Input 1

11
1 2 3 4 5 4 3 2 1 3 4

Sample Output 1

3

Explanation 1

The different types of birds occur in the following frequencies:

  • Type 1: 2
  • Type 2: 2
  • Type 3: 3
  • Type 4: 3
  • Type 5: 1

Two types have a frequency of 3, and the lower of those is type 3.

HackerRank Migratory Birds Solution

Migratory Birds Solution in C

#include <math.h>
#include <stdio.h>
#include <string.h>
#include <stdlib.h>
#include <assert.h>
#include <limits.h>
#include <stdbool.h>

int main(){
    int n; 
    int i;
    int j;
    int k;
    int max;
    int sth;
    max = 0;
    j = 1;
    scanf("%d",&n);
    sth = 0;
    int *types = malloc(sizeof(int) * n);
    for(int types_i = 0; types_i < n; types_i++){
       scanf("%d",&types[types_i]);
    }
    // your code goes here
    i = 0;
    while (j <= 5 )
        {
        k = 0;
        i = 0;
        while (i < n)
            {
            if (types[i] == j)
                k++;
            i++;
        }
        if (k > max)
            {
            sth = j;
            max = k;
        }
        j++;
    }
    printf("%d", sth);
    return 0;
}

Migratory Birds Solution in Cpp

#include <bits/stdc++.h>

using namespace std;

const int maxN = 1e5+10;
int N,A[10];

int main()
{
    cin >> N;
    for (int i=1,x; i <= N; i++) cin >> x, A[x]++;
    int ans = 1;
    for (int i=2; i <= 5; i++)
        if (A[i] > A[ans]) ans = i;
    cout << ans;
}

Migratory Birds Solution in Java

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

public class Solution {

    public static void main(String[] args) {
        Scanner in = new Scanner(System.in);
        int n = in.nextInt();
        int[] types = new int[n];
        int[] count = new int[6];
        int max=0;
        for(int types_i=0; types_i < n; types_i++){
            types[types_i] = in.nextInt();
            count[types[types_i]]++;
            if(count[types[types_i]]>max)
                max=count[types[types_i]];
        }
        for(int i=0;i<count.length;i++)
        {
            if(max==count[i])
            {
                System.out.println(i);
                break;
            }
                
        }
        
    }
}

Migratory Birds Solution in Python

#!/bin/python

import sys


n = int(raw_input().strip())
l =list( map(int, raw_input().strip().split(' ')))
l.sort()
ans=l[0]
count=1
max=1
for i in range(1,n):
    if l[i]==l[i-1]:
        count+=1
    else:
        count=1
    if count>max:
        max=count
        ans=l[i]
print ans

Migratory Birds Solution using JavaScript

process.stdin.resume();
process.stdin.setEncoding('ascii');

var input_stdin = "";
var input_stdin_array = "";
var input_currentline = 0;

process.stdin.on('data', function (data) {
    input_stdin += data;
});

process.stdin.on('end', function () {
    input_stdin_array = input_stdin.split("\n");
    main();    
});

function readLine() {
    return input_stdin_array[input_currentline++];
}

/////////////// ignore above this line ////////////////////

function main() {
    var n = parseInt(readLine());
    a = readLine().split(' ');
    a = a.map(Number);
    var max = 0;
    b = new Array(6);
    b.fill(0);
    for(i=0;i<n;++i){
        b[a[i]]++;
       
    }
    var ind = 0;
    for(i=1;i<=5;++i){
        if(b[i]>max){
            max = b[i];
            ind = i;
        }
    }
    // your code goes here
    console.log(ind);
}

Migratory Birds Solution in Scala

object Solution {

    def main(args: Array[String]) {
        val sc = new java.util.Scanner (System.in);
        var n = sc.nextInt();
        //var types = new Array[Int](n);
        var countArr = new Array[Int](5)
        for(i <- 0 to n-1) {
           var curr = sc.nextInt()-1 
           countArr(curr) = countArr(curr) + 1
        }
        var max = 0
        for(i <- 1 to 4){
            if(countArr(i) > countArr(max)){
                max = i
            }
        }
        println(max+1)
    }
}

Migratory Birds Solution in Pascal

will update sooon

Disclaimer: This problem (Migratory Birds) is generated by HackerRank but the solution is provided by Chase2learn. This tutorial is only for Educational and Learning purposes.

FAQ:

1. How do you solve the first question in HackerRank?

If you want to solve the first question of Hackerrank then you have to decide which programing language you want to practice i.e C programming, Cpp Programing, or Java programming then you have to start with the first program HELLO WORLD.

2. How do I find my HackerRank ID?

You will receive an email from HackerRank to confirm your access to the ID. Once you have confirmed your email, the entry will show up as verified on the settings page. You will also have an option to “Make primary”. Click on that option. Read more

3. Does HackerRank detect cheating?

yes, HackerRank uses a powerful tool to detect plagiarism in the candidates’ submitted code. The Test report of a candidate highlights any plagiarized portions in the submitted code and helps evaluators to verify the integrity of answers provided in the Test.

4. Does HackerRank use camera?

No for coding practice Hackerrank does not use camera but for companies’ interviews code submission time Hackerrank uses the camera.

5. Should I put HackerRank certificate on resume?

These certificates are useless, and you should not put them on your resume. The experience you gained from getting them is not useless. Use it to build a portfolio, and link to it on your resume. 

6. Can I retake HackerRank test?

The company which sent you the HackerRank Test invite owns your Test submissions and results. It’s their discretion to permit a reattempt for a particular Test. If you wish to retake the test, we recommend that you contact the concerned recruiter who invited you to the Test and request a re-invite. 

7. What is HackerRank?

HackerRank is a tech company that focuses on competitive programming challenges for both consumers and businesses. Developers compete by writing programs according to provided specifications. Wikipedi


Finally, we are now, in the end, I just want to conclude some important message for you

Note:- I compile all programs, if there is any case program is not working and showing an error please let me know in the comment section. If you are using adblocker, please disable adblocker because some functions of the site may not work correctly.

Please share our posts on social media platforms and also suggest to your friends to Join Our Groups. Don’t forget to subscribe. 

Sharing Is Caring

Leave a Comment