Created
June 1, 2025 12:32
-
-
Save SuryaPratapK/d909f7301d8f6b9b0aed4380f54ccb2e to your computer and use it in GitHub Desktop.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
class Solution { | |
#define ll long long | |
ll combinations(ll n){ | |
if(n<0) | |
return 0; | |
return 1LL*(n+1)*(n+2)/2; | |
} | |
public: | |
long long distributeCandies(int n, int limit) { | |
ll all_combinations = combinations(n); | |
ll one_above_limit_combinations = 3LL*combinations(n-(limit+1)); | |
ll two_above_limit_combinations = 3LL*combinations(n-2*(limit+1)); | |
ll three_above_limit_combinations = combinations(n-3*(limit+1)); | |
ll invalid_combinations = one_above_limit_combinations - | |
two_above_limit_combinations + | |
three_above_limit_combinations; | |
ll valid_combinations = all_combinations - invalid_combinations; | |
return valid_combinations; | |
} | |
}; | |
/* | |
//JAVA | |
class Solution { | |
private long combinations(long n) { | |
if (n < 0) { | |
return 0; | |
} | |
return (n + 1) * (n + 2) / 2; | |
} | |
public long distributeCandies(int n, int limit) { | |
long allCombinations = combinations(n); | |
long oneAboveLimitCombinations = 3 * combinations(n - (limit + 1)); | |
long twoAboveLimitCombinations = 3 * combinations(n - 2 * (limit + 1)); | |
long threeAboveLimitCombinations = combinations(n - 3 * (limit + 1)); | |
long invalidCombinations = oneAboveLimitCombinations - twoAboveLimitCombinations + threeAboveLimitCombinations; | |
long validCombinations = allCombinations - invalidCombinations; | |
return validCombinations; | |
} | |
} | |
#Python | |
class Solution: | |
def combinations(self, n: int) -> int: | |
if n < 0: | |
return 0 | |
return (n + 1) * (n + 2) // 2 | |
def distributeCandies(self, n: int, limit: int) -> int: | |
all_combinations = self.combinations(n) | |
one_above_limit_combinations = 3 * self.combinations(n - (limit + 1)) | |
two_above_limit_combinations = 3 * self.combinations(n - 2 * (limit + 1)) | |
three_above_limit_combinations = self.combinations(n - 3 * (limit + 1)) | |
invalid_combinations = (one_above_limit_combinations - | |
two_above_limit_combinations + | |
three_above_limit_combinations) | |
valid_combinations = all_combinations - invalid_combinations | |
return valid_combinations | |
*/ |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment