Sort by

recency

|

1342 Discussions

|

  • + 0 comments

    Here is my c++ solution, you can watch the explanation here : https://youtu.be/SbHCdf_c7ss

    int viralAdvertising(int n) {
        int ans = 0, shared = 5;
        for(int i = 1; i <= n; i++){
            ans += shared / 2;
            shared = (shared/2)*3;
        }
        return ans;
    }  
    
  • + 0 comments
    function viralAdvertising(n) {
        let shared = 5; 
        let ans = 0; 
        for(let i = 1; i <= n; i++) {
            let liked = Math.floor(shared / 2);
            ans += liked; 
            shared = liked * 3; 
        }
        return ans; 
    }
    
  • + 0 comments

    Python3 Simple Brute force approach.

    def viralAdvertising(n):
        Acc = 0
        Shares = 5  # Initial shares on the first day
    
        for _ in range(n):
            Likes = Shares // 2  # Calculate likes as half of the shares
            Acc += Likes  # Accumulate the total likes
            Shares = Likes * 3  # Calculate new shares based on the likes
    
        return Acc
    
  • + 0 comments

    Solution with recursion

    def viralAdvertising(n): def rec(n, r): if n == 1: return r return r + rec(n-1, r*3//2)

    return rec(n, 2)
    
  • + 1 comment

    I spent a lot of time trying to figure out an o(1) solution.

    It looks like a form of exponential or logarithmic growth that can be calculated without iterating through an array. Anyone have any ideas?