lundi 22 juillet 2019

Using std::vector in hackerank birthday cake candles question

I had solved this problem using array but not able to do it with vectors.

#include <iostream>
#include <algorithm>
#include <vector>
using namespace std;

int main() 
{
    int age, count = 0, i;
    vector<int>length;
    cin >> age;

    for (i = 0; i <= age; i++)
    {
        length.push_back(i);
    }
    sort(length.begin(), length.end());

    int max = length.back();
    for (i = 0; i <= length.size(); i++)
    {
        if (length[i] == max)
            count++;
    }
    cout << count;
    return 0;
}

I expect the output to be 2 in the first test case but the actual output is 1.

Here's the problem - https://www.hackerrank.com/challenges/birthday-cake-candles/problem

Aucun commentaire:

Enregistrer un commentaire