Code For Alls..!

get the code!!

Tuesday, 29 August 2017

Migratory Birds


A flock of  birds is flying across the continent. Each bird has a type, and the different types are designated by the ID numbers , and .
Given an array of  integers where each integer describes the type of a bird in the flock, find and 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.
Input Format
The first line contains an integer denoting  (the number of birds).
The second line contains  space-separated integers describing the respective type numbers of each bird in the flock.
Constraints
  • It is guaranteed that each type is , or .
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  bird
  • Type  birds
  • Type  bird
  • Type  birds
  • Type  bird
The type number that occurs at the highest frequency is type , so we print  as our answer.
C Program:
#include <math.h>
#include <stdio.h>
#include <string.h>
#include <stdlib.h>
#include <assert.h>
#include <limits.h>
#include <stdbool.h>
int migratoryBirds(int n, int* ar) {
   int a[5]={0},j;
    for(int i=0;i<n;i++)
        a[ar[i]]++;
    int m=0;
    for(int i=1;i<=5;i++)
        if(m<a[i])
        {
            m=a[i];
            j=i;
        }
    return j;
}
int main() {
    int n; 
    scanf("%i", &n);
    int *ar = malloc(sizeof(int) * n);
    for(int ar_i = 0; ar_i < n; ar_i++){
       scanf("%i",&ar[ar_i]);
    }
    int result = migratoryBirds(n, ar);
    printf("%d\n", result);
    return 0;
}


3 comments: