Algo for Migratory Birds problem of HackerRank

I made this algorithm after many tries to resolve the time requirements of the problem.  

At first I had thought of sorting the array passed first so that it would be helpful while calculating the frequency of the elements of that array, but later I thought of directly adding number of occurences of the elements in the "array" array. Which saved a lot of time, and also I was before going for a very tedious approach on calculation of the frequency.

Although my previous algo was working fine too but it was unable to pass two of the five tests which were there. 

Also I was working on an algorithm for determining the ranks the elements present in an array.

Well I am home and its kind of different now, but as always any port in a storm, so I gotta adjust. 

#include <bits/stdc++.h>


using namespace std;

string ltrim(const string &);
string rtrim(const string &);
vector<string> split(const string &);

/*
 * Complete the 'migratoryBirds' function below.
 *
 * The function is expected to return an INTEGER.
 * The function accepts INTEGER_ARRAY arr as parameter.
 */
 
int migratoryBirds(vector<int> arr) {

    // int check1=0;
//  for(int i=0;i<arr.size()+1;i++){
//      for(int j=0;j<arr.size();j++){
//       int temp;
//       if(arr[j]>arr[j+1]){
//           check1=1;
//           temp=arr[j];
//           arr[j]=arr[j+1];
//           arr[j+1]=temp;
          
//       }   
//      }
//      if(check1==0){
//          break;
//      }
//  }

 
//  int check;
 int max1=0,max=0;
for(int i=0;i<arr.size();i++){
    if(arr[i]>max1)max1=arr[i];
}
 vector<int> array(max1+1);
//  array={0};

for(int i=0;i<arr.size();i++){
    array[arr[i]]+=1;
    // if(arr[i]>max1)max1=arr[i];
}
for(int i=0;i<array.size();i++){
    cout<<array[i]<<"\t";
}cout<<endl;

//  for(int i=0;i<arr.size()-1;i++){
//     int freq=1;
//     check=0;
//     for(int j=i+1;j<arr.size();j++){
        
        
//         if((i!=0)&&(arr[i-1]==arr[i])){
//         break;
//         }else{
//          if(arr[i]==arr[j]){
//             freq=freq+1;
//             check=1;
            
//           }
//         }
//     }
// for(int i=0;i<arr.size();i++){
    
// }
// for(auto it=array.begin();it!=array.end();it++){
//     cout<<*it<<"\t";
//     if(*it>max)max=*it;
// }cout<<"\n";

for(int i=0;i<array.size();i++){
    if(array[i]>max)max=array[i];
}
//     if(freq>max)max=freq;
//    if(check==1)array[arr[i]]=freq;

 cout<<"The value of max is "<<max<<endl;

 for(int i=0;i<array.size();i++){
     if(array[i]==max){
         return i;
     }
 }
 return max;
}

int main()
{
    ofstream fout(getenv("OUTPUT_PATH"));

    string arr_count_temp;
    getline(cin, arr_count_temp);

    int arr_count = stoi(ltrim(rtrim(arr_count_temp)));

    string arr_temp_temp;
    getline(cin, arr_temp_temp);

    vector<string> arr_temp = split(rtrim(arr_temp_temp));

    vector<int> arr(arr_count);

    for (int i = 0; i < arr_count; i++) {
        int arr_item = stoi(arr_temp[i]);

        arr[i] = arr_item;
    }

    int result = migratoryBirds(arr);

    fout << result << "\n";

    fout.close();

    return 0;
}

string ltrim(const string &str) {
    string s(str);

    s.erase(
        s.begin(),
        find_if(s.begin(), s.end(), not1(ptr_fun<int, int>(isspace)))
    );

    return s;
}

string rtrim(const string &str) {
    string s(str);

    s.erase(
        find_if(s.rbegin(), s.rend(), not1(ptr_fun<int, int>(isspace))).base(),
        s.end()
    );

    return s;
}

vector<string> split(const string &str) {
    vector<string> tokens;

    string::size_type start = 0;
    string::size_type end = 0;

    while ((end = str.find(" ", start)) != string::npos) {
        tokens.push_back(str.substr(start, end - start));

        start = end + 1;
    }

    tokens.push_back(str.substr(start));

    return tokens;
}

Comments

Popular Posts