Migratory Birds HackerRank Solution in C, C++, Java, Python

You have been asked to help study the population of birds migrating across the continent. Each type of bird you are interested in will be identified by an integer value. Each time a particular kind of bird is spotted, its id number will be added to your array of sightings. You would like to be able to find out which type of bird is most common given a list of sightings. Your task is to print the type number of that bird and if two or more types of birds are equally common, choose the type with the smallest ID number.

For example, assume your bird sightings are of types 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 .

Function Description

Complete the migratoryBirds function in the editor below. It should return the lowest type number of the most frequently sighted bird.

migratoryBirds has the following parameter(s):

  • arr: an array of integers representing types of birds sighted

Input Format

The first line contains an integer denoting n, the number of birds sighted and reported in the array arr.

The second line describes  as  space-separated integers representing the type numbers of each bird sighted.

Constraints

  • 5<=n<=2*10^5
  • It is guaranteed that each type is 1, 2, 3, 4, or 5.

Output Format

Print the type number of the most common bird; if two or more types of birds are equally common, choose the type with the smallest ID number.

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.

Migratory Birds HackerRank 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 HackerRank Solution in C++

#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 HackerRank 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 HackerRank 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 HackerRank Solution in C#

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 = new int[n];

for(int i = 0; i < n; i++){
types[i] = int.Parse(types_temp[i]);
}

List<int> counters = new List<int>() {0, 0, 0, 0, 0};

int max = 0, maxType = 0;

foreach(int type in types){
counters[type - 1]++;

if(counters[type - 1] > max){
max = counters[type - 1];
maxType = type - 1;
}
else if(counters[type - 1] == max){
if(maxType > type - 1){
maxType = type - 1;
}
}
}

Console.WriteLine((maxType + 1).ToString());
// your code goes here
}
}

 

Attempt – Migratory Birds HackerRank Challenge

Link – https://www.hackerrank.com/challenges/migratory-birds

Leave a Comment