Sort by

recency

|

297 Discussions

|

  • + 5 comments

    I don't understand all the complicated solutions here. There is someone talking about overflow being well defined in C++ for unsigned types, and there happens to be a common 32bit unsigned integer type usually available in unsigned int. So my simple solution which passes all tests seems best.

    #include <iostream>
    
    
    int main() {
        static_assert(sizeof(unsigned int) == 4);
        unsigned int N, S, P, Q;
        
        std::cin >> N >> S >> P >> Q;
        
        auto count =0;
        auto previous = S;
        while (N--) {
            auto brandnew = previous * P + Q;
            if (brandnew != previous) ++count;
            previous = brandnew;
        }
           
        std::cout << count;
        return 0;
    }
    
  • + 0 comments

    Unfair! The same code is passing with C++ 11 but not with C++ 14 (time outing some cases).

    The hackerrrank gotta fix their compilers!

  • + 2 comments

    tortise and hare doesn't make any sense... When the data is not stored up in any structure, the time is enough for just purely comparison and increment count accordingly.

        int a = s%mod;
        int compare = s%mod;
        int count = 1;
        for (int i = 1; i < n; i++)
        {
            a = ((a%mod)*(p%mod)+q%mod)%mod;
            if (a != compare)
            {
                count++;
                compare = a;
            }
            else continue;
        }
        cout<<count;
    
  • + 0 comments

    I solved it and explained in a GitHub repository, enjoy :) https://github.com/IvanPinna/BitArray-Hackerrank

  • + 1 comment

    As we are constantly doing % 2^31, and given that unsigned overflow behavior is well defined in c++, is there any point in using long long (64bits) types?