• + 0 comments

    Time Complexity : O(1)

    Visit my code in github : https://github.com/praveen-de-silva/HackerRank_ProblemSolving/blob/main/HaloweenSale.cpp

    int howManyGames(int p, int d, int m, int s) { // invalid condition if (s < p) { return 0; }

    int n1 = 1 + (p - m) / d; // max number of games down to 'm'
    int s1 = n1 * (2*p - (n1 - 1) * d) / 2;      // total cost down to 'm'
    int res;
    
    // case 01 : 's' is in between 'p' and 's1'
    if (s < s1) {
        double det = sqrt((2*p + d) * (2*p + d) - 8*s*d);
        res = (2*p + d - det) / (2 * d);
    } 
    
    // case 02 : 's' is greater than 's1'
    else{
        res = n1 + (s - s1) / m;
    }
    
    return res;
    

    }