Little Ashish's Huge Donation

Sort by

recency

|

29 Discussions

|

  • + 0 comments

    Knoxville Insurance Store (IRM Insurance Knoxville Tn) provides a range of services, including personal and commercial insurance. Their offerings cover auto, home, business, life, health, renters, and umbrella insurance policies. As an independent agency, they help clients by comparing policies from multiple providers to find the best coverage and rates tailored to individual or business needs.

  • + 0 comments

    The formula for calculation sum of squares of first natural number is:

    (n * (n+1) * (2*n + 1))/6

    So the thought process is start giving numbers to this formula from 1 and increment is by 1 till it's return values is less or equal to given "x" by testcases

    Note: This solution is a brute force approach as well as it pass all the test cases

    Solution:

    #include <math.h>
    #define ll unsigned long long
    
    ll sum_of_squares(ll n){
        return (n*(n+1)*(2*n+1))/6;
    }
    
    void successful_donations(ll x){
        int counter=1;
    
        while (sum_of_squares(counter) <= x){
            counter++;
        }
        printf("%d\n", counter-1);
    }
    
    int main(){
        int t;
        ll x;
        scanf("%d", &t);
        while (t--) {
            scanf("%lld", &x);
            successful_donations(x);
        }
    
        return 0;
    }
    
  • + 0 comments

    Given the sum of squares formula, the number of days to spend x candies is roughly the cube root of 3x. So I back off a few days from that estimate to be conservative and then iterate through until I find the right value.

    def solve(candies):
        day = math.floor(pow(3*candies,1/3))-3
        while day*(day+1)*(2*day+1) <= 6*candies:
            day += 1
        return day-1
    
  • + 0 comments

    c++ almost complete all test cases

    int solve(long x) {
        long total = 0;
        int i = 1;
        while(true){
            total += (i * i);
            if(total>x){
                break;
            }
            i++;
        }
        return --i;
    }
    
  • + 0 comments

    This can be also solved using transtion matrix and approach similar to fibonacci series etc. problems. Transition matrix:

    (0 1 0 2 1 0) - i^2

    (0 0 0 1 0 0) - i-1

    (0 0 0 1 1 0) - i

    (0 0 0 0 1 0) - const 1

    (0 1 0 2 1 1) - Sum(1 to i inclusive)

    Initial vector for i=1: (1, 0, 1, 1, 1)

    Then use efficient exponentitation to find relevant matrices and multiple. O(log n)