Skip to content
thecscience
THECSICENCE

Learn everything about computer science

  • Home
  • Human values
  • NCERT Solutions
  • HackerRank solutions
    • HackerRank Algorithms problems solutions
    • HackerRank C solutions
    • HackerRank C++ solutions
    • HackerRank Java problems solutions
    • HackerRank Python problems solutions
thecscience
THECSICENCE

Learn everything about computer science

HackerRank Migratory Birds Problem Solution

Yashwant Parihar, April 12, 2023April 12, 2023

In this post, We are going to solve HackerRank Migratory Birds Problem. 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 migratory birds function in the editor below.

migratory birds have 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 10 power 5
  • 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 types 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 are type 3.

HackerRank Migratory Birds Problem Solution
HackerRank Migratory Birds Problem Solution

Migratory Birds C Solution

#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; 
    scanf("%d",&n);
    int *types = malloc(sizeof(int) * n);
    for(int types_i = 0; types_i < n; types_i++){
       scanf("%d",&types[types_i]);
    }
    
    int counts[5];
    for(int i=0; i<5; i++){
        counts[i] =0;
    }
    
    for(int i=0; i<n; i++){
        counts[types[i]-1]++;
    }
    
    int maxpos = 0;
    for(int i=1; i<5; i++){
        if(counts[i] > counts[maxpos]){
            maxpos = i;
        }
    }
    printf("%d\n",maxpos+1);
    return 0;
}

Migratory Birds C++ Solution

#include <cmath>
#include <cstdio>
#include <vector>
#include <iostream>
#include <algorithm>
using namespace std;


int main(){
    int num_of_birds, best_bird = 1, max_bird = 0;
    cin >> num_of_birds;
    vector<int> types(5, 0);
    for(int i = 0, next_bird; i < num_of_birds; ++i){
        cin >> next_bird;
        ++types[next_bird-1];
    }
    
    for(int i = 0; i < 5; ++i){
        if(types[i] > max_bird){
            max_bird = types[i];
            best_bird = i + 1;
        }
    }
    cout << best_bird << endl;
    return 0;
}

Migratory Birds C Sharp Solution

using System;
using System.Collections.Generic;
using System.IO;
using System.Linq;
class Solution {

    static void Main(String[] args) {
        int n = Convert.ToInt32(Console.ReadLine());
        string[] types_temp = Console.ReadLine().Split(' ');
        int[] types = Array.ConvertAll(types_temp,Int32.Parse);
        // your code goes here
        var arr = new int[5];
        for (var i = 0; i < n; i++){
            arr[types[i]-1] = arr[types[i]-1] + 1;
        }
        var result = 4;
        for(var i = 3; i >= 0; i--){
            if(arr[i] >= arr[result]){
                result = i;
            }
        }
        Console.WriteLine(result + 1);
    }
}

Migratory Birds Java Solution

import java.io.*;
import java.util.*;
import java.text.*;
import java.math.*;
import java.util.regex.*;
import java.util.function.Function;
import java.util.stream.Collectors;
import java.util.stream.IntStream;

public class Solution {

    public static void main(String[] args) {
        Scanner in = new Scanner(System.in);
        int n = in.nextInt();
        int[] types = new int[n];
        for(int types_i=0; types_i < n; types_i++){
            types[types_i] = in.nextInt();
        }
        // your code goes here
        
        Map<Integer, Long> typesToCountMap = IntStream.of(types).
                boxed().
                collect(Collectors.groupingBy(Function.identity(), Collectors.counting()));


        Long maxCount = typesToCountMap.values().stream().
                max(Comparator.naturalOrder()).
                get();

        List<Integer> typeWithMaxCount = typesToCountMap.entrySet().stream().
                filter(item -> Objects.equals(item.getValue(), maxCount)).
                map(Map.Entry::getKey).
                collect(Collectors.toList());
        System.out.println(typeWithMaxCount.get(0));
    }
}

Migratory Birds JavaScript Solution

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());
    types = readLine().split(' ');
    types = types.map(Number);
    // your code goes here
    
    var obj = {
        1:0,
        2:0,
        3:0,
        4:0,
        5:0
    }
    
    function count(num){
        obj[num]++;
    }
    
    types = types.map(count);
    
    var max = 0;
    var type = 1;
    
    for(var prop in obj){
        if(obj[prop]>max){
            max=obj[prop];
            type = prop;
        }else if(obj[prop]===max && prop<type){
            type=prop;
        }
    }
    
    console.log(type);
    
    

}

Migratory Birds Python Solution

#!/bin/python3

import sys


n = int(input().strip())
types = list(map(int, input().strip().split(' ')))
# your code goes here
counts = [0,0,0,0,0,0]
for i in types:
    counts[i]+=1;
print(counts.index(max(counts)))

Other Solutions

  • HackerRank Day of the Programmer Solution
  • HackerRank Bill Division Problem Solution
c C# C++ HackerRank Solutions java javascript python CcppCSharphackerrank solutionjavajavascriptpython

Post navigation

Previous post
Next post

Leave a Reply

You must be logged in to post a comment.

  • HackerRank Dynamic Array Problem Solution
  • HackerRank 2D Array – DS Problem Solution
  • Hackerrank Array – DS Problem Solution
  • Von Neumann and Harvard Machine Architecture
  • Development of Computers
©2025 THECSICENCE | WordPress Theme by SuperbThemes