• + 1 comment

    Your solution is recursive but it's not a tail recursion (it's important when using C/C++ with GCC that optimizes tail calls). So it will stack the function calls because of the

    (received / 2) + ...
    

    On top of that you're computing "(received / 2)" twice per call. It would be better to iterate instead of using non tail recursive call, like the following :

    int countLikesOnDay(int day) {
    	int received = 5;
    	int cumulative = 0;
    	for(int i = 0; i < day; i++) {
    		received /= 2;
    		cumulative += received;
    		received *= 3;
    	}
    	
    	return cumulative;
    }
    
    // ...
    // somewhere in main()
    countLikesOnDay(day);
    

    Or transform it to a tail recursive call like the following :

    int countLikesOnDayAux(int day, int received, int cumulative) {
    	if (day == 0)
    		return cumulative;
    	else
    		return countLikesOnDayAux(day-1, (received / 2) * 3, cumulative + (received / 2));
    }
    
    int countLikesOnDay(int day) {
    	return countLikesOnDayAux(day, 5, 0);
    }
    
    // ...
    // somewhere in main()
    countLikesOnDay(day);
    

    (I don't know how to avoid computing "(received / 2)" twice in the tail recursive call).

    That's just an advice, just remember that

    (received / 2) + 
            countLikesOnDay(--day, (received / 2) * 3);
    

    using a lot of space (to remember each previous call) and time (to compute the whole stack at the end of the recursion), while iteration and tail recursion (when optimized) are using just the number of parameters in space, and small calculations per call, not a big one at the end.

    But for sure, non tail recusive calls, will always be the easiest way of reading an algorithm, that's why your solution is the most readable solution, but not the most elegant, I don't agree with that.