HackerRank HackFest 2020 SOLUTIONS

Game of Maximization Solution

Game of Maximization Solution JAVA:

int oddPosition = 0;
    int evenPosition = 0;
    
    for(int i=0;i<arr.size(); i++)
    {
        if(i%2 == 0)
        {
            evenPosition += arr.get(i);
        }
        else
        {
            oddPosition += arr.get(i);
        }               
    }
       
    while(evenPosition!=oddPosition)
    {       
        if(evenPosition>oddPosition)
        {
            evenPosition--;                
        }
        else if(evenPosition<oddPosition)
        {
            oddPosition--;                
        } 
    }     

    return evenPosition*2; 

Join here for more: https://t.me/DevsIntelCrunch

Strictly Increasing Solution cpp

#include <bits/stdc++.h>

using namespace std;

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

/*
 * Complete the 'whoIsTheWinner' function below.
 *
 * The function is expected to return a STRING.
 * The function accepts INTEGER_ARRAY arr as parameter.
 */

string whoIsTheWinner(vector<int> arr) {
    // WRITE DOWN YOUR CODE HERE
     unordered_map<int, int>map1;
    for(int i=0;i<arr.size();i++)
    {
        if(map1.count(arr[i])==0)
            map1[arr[i]]=1;
        else
            map1[arr[i]]++;
    }
    int count=0,count1=0;
    unordered_map<int, int>::iterator it;
    for(it=map1.begin();it != map1.end();it++)
    {
        // cout<<it->second<<endl;
        if(it->second%2==1)
            count1++;
        if(it->second>1)
        count++;
    }
    if(count1%2==0)
    {
        if(count==0)
            return "First";
        else
          return "Second";  
    }

        return "First";


}

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

    string t_temp;
    getline(cin, t_temp);

    int t = stoi(ltrim(rtrim(t_temp)));

    for (int t_itr = 0; t_itr < t; t_itr++) {
        string n_temp;
        getline(cin, n_temp);

        int n = stoi(ltrim(rtrim(n_temp)));

        string arr_temp_temp;
        getline(cin, arr_temp_temp);

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

        vector<int> arr(n);

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

            arr[i] = arr_item;
        }

        string result = whoIsTheWinner(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;
}

Leave a Reply

Your email address will not be published. Required fields are marked *

%d bloggers like this: