Skip to content

Instantly share code, notes, and snippets.

@Ch-sriram
Created June 3, 2020 09:11
Show Gist options
  • Select an option

  • Save Ch-sriram/0352873393a6ac69f7df077679b2bfb3 to your computer and use it in GitHub Desktop.

Select an option

Save Ch-sriram/0352873393a6ac69f7df077679b2bfb3 to your computer and use it in GitHub Desktop.
Migratory Birds using Counting Sort [O(N)].
// Problem link: https://www.hackerrank.com/challenges/migratory-birds/problem
#include <iostream>
#include <vector>
#include <algorithm>
#include <climits>
using namespace std;
#define ull uint64_t
void count_sort_rec(const vector<int> &ar, vector<int> &count, ull idx = 0) {
if(idx == ar.size()) return;
count[ar[idx]]++;
count_sort_rec(ar, count, idx+1);
}
void count_check_rec(const vector<int> &count, ull &max_idx, int max_ele = INT_MIN, ull idx = 1) {
if(idx == count.size()) return;
if(max_ele < count[idx]) {
max_ele = count[idx];
max_idx = idx;
}
count_check_rec(count, max_idx, max_ele, idx+1);
}
void migratory_birds(const vector<int> &ar) {
vector<int> count(6,0);
count_sort_rec(ar, count);
ull max_idx = 0;
count_check_rec(count, max_idx);
cout << max_idx;
}
int main() {
int n; cin >> n;
vector<int> arr(n);
for(int i = 0; i < n; ++i)
cin >> arr[i];
migratory_birds(arr);
return 0;
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment