HackerRank Migratory Birds Problem Solution

 


HackerRank Migratory Birds Problem 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; 
    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 c[5] = {0};
    for (int i=0; i<n; i++){
        c[types[i]-1]++;
    }
    int max = -1;
    int maxi = -1;
    for (int i=0; i<5; i++) {
        if(c[i]>max){
            max = c[i];
            maxi = i;
        }
    }
    printf("%d\n", maxi+1);
   
    // your code goes here
    return 0; 
}

HackerRank Migratory Birds Problem Solution  in 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()),
    types = readLine().split(' ');
    types = types.map(Number);
    // your code goes here

    var occ = [0,0,0,0,0],
        max_occ = 0,
        ans = 0
    for (var i = 0 ; i < types.length ; i++) occ[types[i]-1]++
    for (var i = 0 ; i < 5 ; i++) if (occ[i] > max_occ) max_occ = occ[i], ans = i
    
    //console.error(occ)
    console.log(ans + 1)
}

HackerRank Migratory Birds Problem Solution in Python

#!/bin/python3

import sys


n = int(input().strip())
types = list(map(int, input().strip().split(' ')))
# your code goes here
counter = [0 for ii in range(5)]
for t in types:
    counter[t-1] += 1

hf = 0
for nth, c in enumerate(counter[1:], start=1):
    if c > counter[hf]:
        hf = nth

print(hf+1)

 

0 Comments