Some days ago, I learned the concept of LCM (least common multiple). I've played with it for several times and I want to make a big number with it.
But I also don't want to use many numbers, so I'll choose three positive integers (they don't have to be distinct) which are not greater than n. Can you help me to find the maximum possible least common multiple of these three integers?
Input
The first line contains an integer n (1 ≤ n ≤ 106) — the n mentioned in the statement.
Output
Print a single integer — the maximum possible LCM of three not necessarily distinct positive integers that are not greater than n.
Examples
Input
9
Output
504
Input
7
Output
210
Note
The least common multiple of some positive integers is the least positive integer which is multiple for each of them.
The result may become very large, 32-bit integer won't be enough. So using 64-bit integers is recommended.
For the last example, we can chose numbers 7, 6, 5 and the LCM of them is 7·6·5 = 210. It is the maximum value we can get.
Assistant
Other models, same or similar prompt
Responses were produced in different, uncontrolled contexts (system prompts, settings, prior turns). This is not a benchmark.
We need to find maximum LCM of three numbers a, b, c ∈ [1, n] (positive integers ≤ n). They can be equal? "they don't have to be distinct". So we can pick duplicates, but usually duplicates wouldn't help maximize LCM because LCM(a,a,b)=LCM(a,b). So effectively we pick three numbers possibly with duplicates but best likely three distinct.
We need to compute max over all triples (a,b,c) with 1 ≤ a,b,c ≤ n of lcm(a,b,c). Since n up to 10^6, brute force is O(n^3) impossible. We need to reason about structure.
Observation: LCM of three numbers ≤ product; but to maximize LCM, we likely pick numbers close to n, and also have large pairwise lcm. Possibly pick three largest numbers that are pairwise coprime? LCM then product. So for n=9, max LCM is 504 = 987? Actually 987=504, yes. For n=7, max is 765=210. So these examples suggest picking the three largest numbers yields product? Check: For n=9, 9,8,7 product 504, LCM? Since 9,8,7 are pairwise coprime? 9 and 8 coprime, 9 and 7 coprime, 8 and 7 coprime. Yes, product = LCM. For n=7, 7,6,5 product 210, pairwise: 7 and 6 coprime? gcd(7,6)=1, 7 and 5=1, 6 and 5=1, so LCM product.
But is it always the case that the three largest numbers are pairwise coprime? Not necessarily. For n=8, the three largest are 8,7,6. Check: gcd(8,7)=1; gcd(8,6)=2; gcd(7,6)=1. So LCM(8,7,6) = LCM(LCM(8,6),7) = LCM(24,7)=168. Product = 336. So LCM is 168 < 336. Could there be a triple with larger LCM? Possibly 8,7,5? LCM=875=280? Actually 8,7,5 are pairwise coprime, LCM=280. 280 > 168. Or 8,7,9? but 9 > n=8 not allowed. So for n=8, best may be 8,7,5? But 5 is less than 6. 8,7,5=280. What about 8,7,3? 168. 8,7,4? 56? no. So 280 likely max. Check 8,5,7 also. But maybe 8,7,9? no. What about 8,8,7? LCM(8,7)=56. So no. So for n=8, maximum LCM is 280? Let's test other combos: 8,5,7 gave 280. 8,7,6 gave 168. 8,7,3 gave 168. 8,7,9 not allowed. 7,6,5=210. 8,6,5=120? Actually LCM(8,6,5)=LCM(24,5)=120. 8,7,4=56. 8,7,2=56? So 280 seems best. But what about 8,7,9? not allowed. 8,8,7=56. So 280.
But maybe 8,7,8? same. So indeed, maximum might be product of three numbers that are pairwise coprime and as large as possible, not necessarily the three largest. So we need to search over triples (a,b,c) ≤ n maximizing lcm.
Since n ≤ 1e6, we could try enumerating all triples of numbers near n? Possibly the optimal triple consists of numbers close to n. Because LCM tends to be large when numbers are large. So we could consider candidate sets from high numbers.
But we need a systematic way. Let's denote we want max LCM. Observations:
LCM(a,b,c) = LCM(LCM(a,b),c). So it's at least max(a,b,c).
For given maximum value m = max(a,b,c), the best would be to choose two other numbers that are as large as possible and also share as few common factors with m and each other. Typically, we want numbers that are pairwise coprime to maximize product.
But there may be cases where the optimal triple includes numbers not all near n? unlikely because product of three large numbers is huge. However, consider n=100, maybe triple (100, 99, 97) could be good if 100 and 99 and 97 are pairwise? 100 and 99 gcd1; 100 and 97 gcd1; 99 and 97 gcd1; product=960300. But maybe (100,99,98)? 100 and 98 share factor 2? LCM(100,99,98) = let's compute: 100=2^25^2; 99=3^211; 98=27^2. LCM = 2^23^25^27^211 = 2^23^2254911 = 49254911 = (49=36; 3625=900; 90049=44100; 4410011=485100). So product 10099*98 = 970200. LCM=485100 < product. So (100,99,97) gives product 960300 and LCM=960300 (since coprime) which is >485100. So indeed coprime triple is better even if one number slightly smaller.
But could there be triple (100, 99, 101)? not allowed.
So we need to consider selection of three numbers ≤ n maximizing lcm.
Since n up to 1e6, we could attempt to brute-force over possible combinations of numbers near top? But how many? Possibly we could consider candidates among the top few hundred numbers? However, we need to guarantee correctness. Let's think about worst-case: n=1e6, optimum might involve numbers not necessarily in top 100? Probably still near top, because product grows quickly. But let's test if there is any scenario where the optimum includes a number much smaller than n. Suppose n is large, say 10^6. Could it be beneficial to pick two numbers near n and a third number much smaller because that small number shares no common factors, but the third near n is also large but shares factor? But if we pick three near top, even if they share factors, the LCM might still be high. However, maybe the best is to pick three numbers that are all coprime and as large as possible, which likely are near n but might skip some numbers to avoid common factors.
We can formulate: For each possible candidate for the maximum value a (largest of triple), we can try to find the best pair (b,c) ≤ n, b,c ≤ a (since a is max) that maximize LCM(a,b,c). But we could also have triple where two numbers equal a? That might be suboptimal because duplicates don't increase LCM. So we consider distinct numbers.
But perhaps there's known result: For three numbers ≤ n, maximum LCM is either:
n*(n-1)*(n-2) if they are pairwise coprime.
n*(n-1)(n-3) if n and n-1 are coprime (they always are) but n and n-2 share a factor when n even? Actually when n even, n and n-2 are both even, so share factor 2. So then maybe n(n-1)*(n-3) is better if n-3 is coprime with both? But need to check.
Or maybe consider (n-1)(n-2)(n-3) if n is odd and n and n-2 share factor? Wait, if n odd, then n and n-2 are both odd, but could share factor? For example n=9 odd: 9 and 7 coprime? Actually 9 and 7 are coprime, but 9 and 8 are coprime, 9 and 6 share factor 3? But we used 9,8,7. For n=9, n and n-2=7 coprime, so n*(n-1)(n-2) works. For n=7, 7,6,5 works. For n=8 even: n(n-1)(n-2)=876=336 product but LCM=168, not optimal; n(n-1)(n-3)=875=280 product and LCM=280 (since coprime? 8,7,5 coprime? 8 and 5 gcd1, 7 and 5 gcd1, 8 and 7 gcd1; yes). So that works. What about n=10? Let's test: 10,9,8: gcd(10,8)=2 -> product 720, LCM(10,9,8)=LCM(40,9)=360? Actually compute: 10=25, 9=3^2, 8=2^3, LCM=2^33^25=360. So product 720, LCM 360. 10,9,7: 10,9,7 are coprime? 10 and 7 gcd1, 9 and7 gcd1, 10 and9 gcd1 -> product 630, LCM 630. So better: 630 > 360. Also 10,9,7 = 1097 =630. Also check 10,9,5? 10 and5 share factor, LCM smaller. So 10,9,7 =630. Check 10,8,7? 10,8,7: 10 and8 share2, LCM(10,8,7)=LCM(40,7)=280? Actually 10,8,7: LCM(10,8)=40, LCM(40,7)=280, product=560, so less. 9,8,7: 987=504 but are they pairwise coprime? 9 and8 yes, 8 and7 yes, 9 and7 yes, so LCM=504. So 504 < 630. So best so far 630.
What about 10,9,11? not allowed. So candidate (10,9,7) yields 630. Also consider (10,9,7) uses numbers not including 8. So pattern: for even n, maybe best is n*(n-1)(n-3) if n divisible by 2 but not by 3? Let's check n=10: n-3=7, product=109*7=630. That works. But is (10,9,7) always coprime? 10 and9 yes, 9 and7 yes, 10 and7 yes if n mod7? Usually 10 and7 are coprime. So it's safe.
But what about n=12? Let's test: n=12. Compute candidates:
12,11,10: Check pairwise: 12 and 10 share 2 => LCM will be less than product. Compute LCM: 12=2^23, 11 prime, 10=25 => LCM=2^235*11=660. Product=1320.
12,10,9? But max=12? But we could also consider triple not necessarily containing 12? Maybe 11,10,9? 11,10,9 are pairwise? 10 and9 gcd1, 11 and10 gcd1, 11 and9 gcd1 => LCM=11109=990. That's larger than 924. And 990 > 924. Also check 11,10,9 all ≤12 and not all include 12. So max might be 990. Also check 12,11,9 we already did 396. 12,10,9: LCM(12,10,9)=? 12=2^23,10=25,9=3^2 => LCM=2^23^25=180? Wait include 11? no. Actually LCM=2^23^25=180. So small. 11,10,9 gives 990. So best might be 990.
Now consider 12,11,10? 660, less. So triple (11,10,9) yields 990. That is (n-1)(n-2)(n-3)? For n=12, (n-1)(n-2)(n-3)=11109=990 exactly. So that is the product of three consecutive numbers just below n. For n=10, (n-1)(n-2)(n-3)=987=504, but we got 630 > 504. So for n=10, (n-1)(n-2)(n-3) is 987=504, but (10,9,7)=630 is larger. So optimum not always (n-1)(n-2)(n-3). For n=12, optimum is 990 which is (11,10,9). Could there be (12,11,7)=924 <990. (12,11,5?) no. (12,10,7)=? 12107? but not coprime? 12 and10 share2, LCM(12,10,7)=420? Actually product=840, LCM=420? Let's compute: 12 and10 LCM=60, with7 gives 420. So lower. So 990 seems max.
But is there (11,10,8)=? 11,10,8: 10 and8 share2 => LCM=440. (11,9,8)= LCM(11,9,8)=? 9 and8 coprime? 9 and8 gcd1, 11 coprime => 1198=792. So less.
What about (12,11,10,9?) only three. So 990.
Now check n=13: possibilities.
(13,12,11): Are they pairwise coprime? 13 prime, 12=2^23, 11 prime => all coprime? 13 and12 yes,13 and11 yes,12 and11 yes => LCM=1312*11=1716. But product 1716. So that's candidate.
(12,11,10): we already considered 12,11,10 = 111210=1320? Actually product = 121110=1320, and are they pairwise? 12 and10 share2, LCM=660? Wait compute: LCM(12,11,10)= LCM(12,10)=60 then with 11 gives 660. So not product. So 660. So not.
(12,11,9): LCM(12,11,9)=? 12 and9 share3 => LCM=2^23^211=396. So less.
(12,11,8): LCM=264. etc.
(11,10,9): 11109=990 < 1430.
Thus best so far: (13,11,10)=1430. Also (13,12,11)=1716? But check if they are pairwise coprime: 13 and 12 yes, 13 and 11 yes, 12 and 11 yes. So indeed they are pairwise coprime, so LCM = product = 131211 = 1716. That's larger than 1430. So candidate (13,12,11) gives 1716. So best is 1716.
Now is (13,12,11) the three largest numbers? Yes. So for n=13, optimum is n*(n-1)(n-2)=1312*11=1716.
But for n=12, the three largest are 12,11,10 but they are not pairwise coprime (12 and10 share 2), so product 1320 but LCM 660; that's lower than (11,10,9)=990; and lower than (12,11,9?) 396; (12,11,7) 924; (12,11,5?) no; (12,10,9?) 180; So optimum was 990, which is (n-1)(n-2)(n-3) for n=12? Actually (11,10,9)= (n-1)(n-2)(n-3) = 11109 = 990. So pattern: For n where n is even and n is not divisible by 3? Wait n=12 even, also n divisible by 3? 12 divisible by 3. For n=10 even, not divisible by 3, optimum was n*(n-1)(n-3)=1097=630, which is not (n-1)(n-2)(n-3)=98*7=504. So there is variation.
Let's test more n to see pattern. Let's compute for some n manually or reason.
We want maximum LCM among triples a≤b≤c≤n (maybe not sorted). The LCM is at most product. To achieve product, numbers must be pairwise coprime. So we aim to find three numbers ≤ n that are pairwise coprime and maximize product. Since product of three numbers near n is large, we can try to pick three largest numbers that are pairwise coprime. If the three largest numbers are pairwise coprime, then product is maximum among all triples? Not necessarily: maybe there is another triple with numbers slightly smaller but still pairwise coprime that gives larger product? For example, if the three largest are 12,11,10 (product 1320) but they are not all pairwise coprime (10 and12 share factor). However, LCM(12,11,10)=660, less than product. So the LCM is not product, so maybe a triple with slightly lower maximum but all pairwise coprime could have product slightly less than 1320 but LCM equals product, which could be larger than 660. For instance, (11,10,9) product 990, which is >660. But (12,11,9) product 1188 but not pairwise coprime? 12 and9 share 3 => LCM=396. So not. (12,11,7) product 924, LCM=924 >990? Actually 924<990. So (11,10,9) gives 990 > 924. (12,11,5) product 660, LCM maybe? 12 and5 coprime, 11 and5 coprime => product=660 LCM=660? Actually 12115=660, but check 12 and5 gcd1, 11 and5 gcd1, 12 and11 gcd1 => LCM=660. But 660 < 990. So 990 is better.
But (12,11,7) gave 924 <990. So (11,10,9) gave 990, which is less than product of three largest (1320) but LCM is product for that triple, while LCM for three largest is only 660, so the pairwise coprime triple with product 990 beats it.
Now could there be a triple with product even larger than 990 but still pairwise coprime? For n=12, possible larger products among triples: (12,11,7)=924, (12,10,9)=1080? But 12,10,9 are not pairwise coprime, product 1080 but LCM 180? Actually LCM=180 (since 12=2^23,10=25,9=3^2 => LCM=2^23^25=180). So not. (12,11,8)=1056, but not pairwise. (12,11,9)=1188, not pairwise. (12,10,7)=840, (12,9,8)=? product 864, LCM? 12,9,8: 12 and8 share4? Actually 12=2^23,9=3^2,8=2^3 => LCM=2^33^2=72, small. (11,10,8)=880, LCM=440. (11,10,9)=990. (11,9,8)=792, (10,9,8)=720? Actually product=720, LCM=LCM(10,9,8)=360. So 990 seems highest among pairwise coprime triples.
But what about (12,11,5)=660, (12,11,1)=132? no.
Thus optimum appears to be max over either:
product of three largest numbers if they are pairwise coprime.
else product of n, n-1, n-3 if those are pairwise coprime? (like for n=10)
else product of n-1, n-2, n-3? (like for n=12)
or maybe other combinations like n, n-2, n-3? etc.
But we need to be systematic. Since n is up to 10^6, we can simply consider a limited set of candidate triples around the top values, and compute LCM for each, then take maximum. The number of candidate triples can be small if we argue that the optimal triple must consist of numbers within a small range from n. Because if we pick a number significantly smaller than n, we lose multiplicative factor that can't be compensated by making others larger (since max is at most n). For instance, if we don't pick n, the largest number we can pick is at most n-1. The product of three numbers each at most n is ≤ n^3. If we pick n, n-1, n-2 product ≈ n^3. If we skip n, maximum product is (n-1)(n-2)(n-3) ≈ n^3 - 6n^2... The difference is about 3n^2. For large n, n^3 dominates, so triple containing n is likely best if it can achieve near product. But if triple with n fails to be pairwise coprime, LCM might be much less than product, while triple without n might be pairwise coprime and have product close to n^3 but slightly less, but LCM equals product, so could be larger than the LCM of the triple with n. So we need to compare a few candidates.
How many candidates? We can consider triples (a,b,c) where each is among the top, say, 100 numbers? But we can prove that the optimal triple is among a small set like the top few numbers. Actually known problem: "Maximum LCM" from Codeforces problem? I recall a problem: "Given n, find maximum LCM of three numbers ≤ n". It might be Codeforces Round #235 or something? I recall a solution: The answer is one of the following: n*(n-1)(n-2) if n is odd; else if n is even, try (n-1)(n-2)(n-3) and n(n-1)(n-3) and maybe n(n-1)(n-2) but usually n even yields gcd(n, n-2) >=2 so product not optimal. However, we saw for n=12, (n-1)(n-2)(n-3)=990, while n(n-1)(n-3)=12119=1188? Wait 1211*9=1188, but is that pairwise coprime? 12 and9 share 3, so LCM is not product, LCM(12,11,9)=? 12 and9 LCM=36, with11=396. So LCM=396, not 1188. So that candidate is not good unless numbers are coprime. So we need to compute LCM, not product. But if we compute LCM of n, n-1, n-3, we need to check if they are pairwise coprime? Actually n and n-3 may share a factor. For n=10, 10 and7 coprime, so LCM=product. For n=12, 12 and9 share 3, so LCM < product. So we need to evaluate LCM.
Thus simple product checks are insufficient; we must compute LCM for candidates. But maybe we can argue that the optimal triple will be one of:
(n, n-1, n-2)
(n, n-1, n-3)
(n, n-2, n-3)
(n-1, n-2, n-3)
and maybe also (n, n-1, n-4) etc? But let's test counterexample: n=18? Let's try to see if any triple beyond these could be optimal. Since numbers are large, the LCM is at most product. If we deviate more, product drops significantly. But we need to be careful for cases where n is small. For n=1e6, the product difference between (n, n-1, n-2) and (n, n-1, n-100) is roughly n*(n-1)*100, which is huge. So candidate with a number far from n would have significantly lower product, and even if that number is coprime with others, LCM may still be less than the LCM of a closer triple if that closer triple's LCM is not too reduced from product.
But could there be a scenario where triple (n, n-1, n-2) has LCM very small due to heavy sharing of factors? The worst-case LCM for triple of large numbers might be as low as about half product? Actually consider numbers like n, n-2 when n even: both even, so they share factor 2, so LCM = (n)(n-2)/2 if they have gcd 2 and third number is coprime? Actually LCM(a,b,c) = LCM(LCM(a,b),c). If a and b share gcd d>1, then LCM(a,b)=ab/d. That's at least ab/ (min(a,b)?) Actually d can be larger if they share more factors. The maximum reduction factor is when a and b have large gcd. But the gcd of two consecutive even numbers? If n is even, n and n-2 are both even and differ by 2. Their gcd could be 2 if n/2 and (n-2)/2 are coprime; could be higher if n divisible by something? For example, n=12, 12 and10 gcd=2. So LCM = 1210/2=60. So product 120 reduced to 60, factor 0.5. Then with third number n-1 (odd, coprime to both maybe?) But n-1 is odd, might share factors with n or n-2? Possibly. For n=12, n-1=11, coprime, so LCM overall = LCM(60,11)=660 = 121011/2? Actually product=1320, half=660. So LCM = product/2.
For n=18: n=18, n-1=17, n-2=16. LCM(18,16)=? 18=23^2, 16=2^4, LCM=2^43^2=144, which is (1816)/ (gcd=2) =144; product 288. Then with 17: LCM=14417=2448. Product 181716=4896, LCM=2448 = product/2.
For n even, n and n-2 always both even, so gcd at least 2. So LCM(n, n-1, n-2) = product / (gcd(n,n-2) / something? Actually more precisely: LCM = product / (gcd(n,n-2)*gcd(LCM(n,n-2), n-1?) Actually easier: LCM(a,b,c) = product / (gcd(a,b)*gcd(a,c)*gcd(b,c) / something?) Not exactly; there's formula using prime exponents. But we can compute directly.
Now, if we take (n, n-1, n-3), for n even, n and n-3: n even, n-3 odd. Their gcd could be >1 if 3 divides n? For n=10, gcd(10,7)=1, good. For n=12, gcd(12,9)=3, bad. For n=14, 14 and 11 gcd? 14 and 11 gcd1. So it depends on n mod 3. For n multiple of 3, n and n-3 share factor 3. So LCM may be reduced.
Thus perhaps the best candidate is among the few combos involving top numbers, and we can compute LCM for each and take max.
But need to be sure that no other triple yields higher LCM. Could there be a triple like (n, n-2, n-4)? For n even, maybe n and n-2 share factor 2, n and n-4 share factor? For n=12: (12,10,8) product=960, LCM? 12,10,8: 12=2^23,10=25,8=2^3 => LCM=2^335=120. So low.
What about (n, n-1, n-5)? For n=12: 12,11,7 product=924, LCM=924 (since coprime?), 12 and7 gcd1, 11 and7 gcd1, 12 and11 gcd1 => LCM=924. That's less than 990.
But (11,10,9)=990 >924. So better.
What about (n, n-3, n-4)? 12,9,8 product=864, LCM? 12 and9 share3 => LCM(12,9)=36, with8 => 72? Actually 36 and8 LCM=72. So low.
So far the best candidates are among the triples formed by numbers in {n, n-1, n-2, n-3}. But for n=12, the best is (n-1,n-2,n-3) which is within that set. For n=10, best is (n, n-1, n-3). For n=9, best is (n, n-1, n-2). For n=8, best is (n, n-1, n-3)=8,7,5=280, which is within {8,7,6,5? Actually includes 6 as n-2 but not used. So still within the set {n, n-1, n-2, n-3}.
Now consider n=6. Let's compute to test. n=6. Options:
(6,5,5)? duplicate: LCM=30.
So max LCM=60? Both (6,5,4) gave 60 and (5,4,3) gave 60. So answer 60.
Our candidates: (n,n-1,n-2)=6,5,4 gives LCM=60. (n,n-1,n-3)=6,5,3 gives LCM=30. (n,n-2,n-3)=6,4,3 gives LCM=12. (n-1,n-2,n-3)=5,4,3 gives LCM=60. So max=60. OK.
Now n=2: numbers 1,2. We need three numbers ≤2, they can repeat. So best? Use 2,2,1? LCM=2. Or 2,2,2? LCM=2. Or 2,1,1? LCM=2. So answer 2. Candidates: (2,1,0?) not allowed. But from top set: n=2, n-1=1, n-2=0 invalid. So we need to handle small n separately. Since n≥1, but for n<3 we need to consider possible duplicates. Our algorithm can just brute force for small n (like n ≤ 50) if needed, but we can also reason. However, the problem constraints n up to 1e6, but we can special-case small n.
Given the pattern, it's plausible that the optimal triple is always among the triples formed by the top few integers (maybe up to 5 numbers). Actually, I recall a known Codeforces problem: "Maximum LCM" with solution: For n <= 2, answer n; else consider triples (n, n-1, n-2), (n, n-1, n-3), (n-1, n-2, n-3) and output the maximum LCM among them. I think that's exactly the solution.
Let me check with n=12: (n, n-1, n-2)=LCM(12,11,10)=660; (n, n-1, n-3)=LCM(12,11,9)=396; (n-1, n-2, n-3)=LCM(11,10,9)=990; max=990. Works.
n=5: Let's test: n=5. Options: (5,4,3) = LCM? 5,4,3 coprime => 60. (5,4,2)=20, (5,3,2)=30, (4,3,2)=12, (5,5,4)=20? etc. So max 60. Candidates: (5,4,3)=60; (5,4,2)=? but 2 is n-3? n-3=2, so (n,n-1,n-3) = (5,4,2) LCM=20; (n-1,n-2,n-3) = (4,3,2)=12; (n,n-1,n-2) = (5,4,3)=60. So max=60. Good.
n=4: n=4. Options: numbers 1..4. Best triple? Possibly (4,3,2) LCM=12; (4,3,1)=12; (4,2,3) same. (4,4,3)=12. So max 12. Candidates: (4,3,2)=12; (4,3,1) but 1 is not n-3? n-3=1 so (n,n-1,n-3) = (4,3,1) LCM=12; (n-1,n-2,n-3) = (3,2,1)=6; (n,n-1,n-2) = (4,3,2)=12. So max=12. Works.
n=3: numbers 1..3. Best triple? Possibly (3,2,1) LCM=6; (3,3,2)=6. So answer 6. Candidates: (3,2,1)=6; (3,2,0?) invalid; (3,2,? Actually n-3=0 invalid. So we need to consider only valid triples. But we can just compute max among all valid combos: (3,2,1) gives 6. Our candidate set might miss (3,2,1) if we restrict to (n,n-1,n-2) only? (3,2,1) is exactly (n,n-1,n-2) because n=3 => (3,2,1). So okay. (n-1,n-2,n-3) invalid. So only that candidate works.
n=2: (2,1,1) LCM=2. Candidate (2,1,?) n-2=0 invalid. So we may need to handle n=2 specially. Our candidate set: (n,n-1,n-2) invalid because n-2=0; (n,n-1,n-3) invalid; (n-1,n-2,n-3) invalid. So we need to consider other triples: like (n,n,n) =2? Or (n,n,n-1)? But we can just brute for small n. However, maybe we can also consider (n,n,n) as candidate. But the known solution often says: if n<3, answer n (since we can take n, n, n? For n=2, answer 2; for n=1, answer 1). But check: n=1, only number 1, so LCM(1,1,1)=1. So answer 1.
Thus, for n >= 3, we can consider the three candidates.
But is it always enough? Let's test some more random values: n=20. Let's compute with our candidate method and perhaps manually check some other triples to ensure no better.
Compute candidates:
(20,19,18): LCM? 20=2^25, 19 prime, 18=23^2. LCM(20,18)=180? Actually 20 and 18: gcd=2, LCM=2018/2=180. Then with 19 (coprime) => 18019=3420. Product 201918=6840. LCM=3420.
(20,19,17): 20,19,17 are all pairwise coprime? 20 and 17 yes, 19 and 17 yes, 20 and 19 yes. So LCM=201917=6460. Product 6460.
(19,18,17): Check pairwise: 19 prime with 18, 19 with 17, 18 and 17. So all pairwise coprime except 18 and? 18 and 17 coprime, yes. So LCM=191817=5814. Product 5814.
So max among these is 6460 from (20,19,17). That's (n, n-1, n-3) because n-3=17. Also (n, n-1, n-2) gave 3420, (n-1,n-2,n-3) gave 5814. So candidate max=6460.
Now check if any other triple could give more? Possibly (20, 18, 17)? 20,18,17: 20 and18 share2 => LCM(20,18)=180, with17=3060. Less. (20,19,15)? 20 and15 share5 => LCM maybe 201915/gcd? Actually compute: 20 and15 LCM=60, with19 =>1140? Actually 6019=1140. Less. (19,18,16)? 19,18,16: 18 and16 share2 => LCM=LCM(18,16)=144? Actually 18=23^2, 16=2^4 => LCM=2^43^2=144, with19 =>2736. Less. (20, 19, 14)? 20 and14 share2 => LCM(20,14)=140, with19=2660. Less. (20,17,16)? 20 and16 share4 => LCM=80, with17=1360. Less. (20,19,13)? 20,19,13 all coprime? 20 and13 gcd1, 19 and13 gcd1 => LCM=2019*13=4940. Less than 6460. So 6460 seems max.
Thus candidate set works.
Now test n=21:
(21,20,19): are they pairwise coprime? 21=37, 20=2^25, 19 prime. gcd(21,20)=1, gcd(21,19)=1, gcd(20,19)=1 => coprime. So LCM=212019=7980.
(21,20,17): 21,20,17 all coprime? 21 and17 gcd1? 21=37,17 prime => yes. So LCM=2120*17=7140.
(20,19,18): 20,19,18: 20 and18 share2 => LCM(20,18)=180, with19=3420. Product=6840. LCM=3420.
So max among these is 7980 from (21,20,19). That's (n,n-1,n-2). So candidate set yields max.
Now test n=22 (even):
(22,21,20): 22=211,21=37,20=2^25. gcd(22,20)=2 => LCM(22,20)=220? Actually 2220/2=220. Then with21 (coprime) => 22021=4620. Product=2221*20=9240. So LCM=4620.
(22,21,19): Check pairwise: 22 and21 gcd1,22 and19 gcd1? 22 and19 gcd1,21 and19 gcd1 => all coprime. So LCM=222119=8778.
(21,20,19): as above, 21,20,19 all coprime => LCM=7980.
(22,21,18): 22 and18 share2,21 and18 share3 => LCM could be lower: compute LCM(22,21)=462? Actually 22 and21 LCM=462, then with18: 462 and18 gcd? 462=23711,18=23^2 => LCM = 23^2711=1386? Actually 462=23711, 18=23^2 => LCM=23^2711=1386. So 1386. Less.
(22,21,17): LCM=222117=7854? Check: 22*21=462, *17=7854. Are they all pairwise coprime? 22 and17 gcd1,21 and17 gcd1,22 and21 gcd1 => yes. So LCM=7854. That's less than 8778.
(22,20,17): 22 and20 share2 => LCM=2220/2=220? Actually 220, with17=3740. Less.
So max seems 8778 from (22,21,19) = (n, n-1, n-3) since n-3=19. Check (n-1,n-2,n-3) = (21,20,19)=7980. So max = 8778.
(24,23,19): 24 and19 gcd1, 23 and19 gcd1, 24 and23 gcd1 => all coprime? 24=2^33, 19 prime, 23 prime, yes. So LCM = 242319 = 10488. That's (n, n-1, n-5)? Actually n-5=19. That's not in our set of three (n, n-1, n-3) would be (24,23,21) which gave 3864. So (24,23,19) gives 10488, which is larger than 6072? Yes 10488 > 6072. Let's compute (24,23,19) product=242319=10488, LCM=10488 (since pairwise coprime? Check 24 and19 coprime, 23 and19 coprime, 24 and23 coprime, yes). So that's candidate not among the three we listed: we had only (n, n-1, n-2), (n, n-1, n-3), (n-1, n-2, n-3). But here we see (n, n-1, n-5) yields a higher LCM. But wait, is n-5=19 allowed? Yes. So does this beat other combos? Check (23,22,21)=? 232221=10626? Actually 2322=506, 21=10626. But are they pairwise coprime? 22 and21 share? gcd(22,21)=1, 23 and22=1, 23 and21=1. So they are all coprime. So LCM=10626. That's even larger! So (23,22,21) = (n-1,n-2,n-3) yields 10626? Wait n=24, n-1=23, n-2=22, n-3=21. LCM of 23,22,21: Are they pairwise coprime? 22 and21: gcd=1; 23 and22:1; 23 and21:1. Yes all coprime, so product=2322*21=10626, LCM=10626. That's > 10488. So (n-1,n-2,n-3) yields 10626. So our candidate (n-1,n-2,n-3) is already 10626. But we must ensure it's valid: all numbers ≤ n. Yes. So max would be 10626. But check if any other triple gives more than 10626? (24,23,22) gave 6072, (24,23,19) gave 10488, (23,22,21)=10626, (24,22,21) maybe? 24,22,21: 24 and22 share2, 24 and21 share3 => LCM likely lower. So 10626 seems max. So our candidate set still yields 10626, because (n-1,n-2,n-3) is included. Good.
But earlier we considered (24,23,19) which is not in the set but is less than (n-1,n-2,n-3). So it's fine.
But is there a case where the optimal triple is something like (n, n-1, n-5) or (n, n-3, n-5) etc, and it beats all three candidate sets? Let's try to find a counterexample.
We need to consider n where (n-1,n-2,n-3) might not be pairwise coprime. For example, if n-1, n-2, n-3 share factors? But they are three consecutive numbers, so at most two of them even? Actually among three consecutive numbers, one is multiple of 3? Possibly one multiple of 2, one multiple of 3, but they could all share some factor? The three numbers (k, k+1, k+2) are such that one is divisible by 2, one by 3, but they can share common factors pairwise? For example (8,9,10): 8 and10 share 2, but 8 and9 coprime, 9 and10 coprime. So they are not all pairwise coprime: 8 and10 are not coprime. So (n-1,n-2,n-3) may not be pairwise coprime, which would reduce its LCM from product. In that case, maybe another triple like (n, n-1, n-3) or (n, n-1, n-4) etc could yield higher LCM.
Consider n such that (n-1,n-2,n-3) have a pair with gcd>1. This happens when the two even numbers among them are both even? Actually among three consecutive numbers, exactly one is divisible by 3? Not exactly: among any three consecutive integers, one is divisible by 3. So if that one also is even, it might share factor 2 with another even? Actually there can be at most one even number that is multiple of 2? Wait three consecutive numbers can have either two evens and one odd, or two odds and one even? Let's list: pattern: even, odd, even (two evens) or odd, even, odd (one even). So for odd n, the triple (n-1,n-2,n-3) corresponds to? For n even: n even, then n-1 odd, n-2 even, n-3 odd. So the triple (n-1,n-2,n-3) has one even (n-2) and two odds. So the only possible gcd>1 among them could be if n-2 shares a factor with one of the odds? That can happen, e.g., if n-2 is multiple of 3 and n-1 or n-3 also multiple of 3? Since among three consecutive numbers, exactly one is multiple of 3. So if n-2 is the multiple of 3 (the middle), then n-1 and n-3 are not multiples of 3. So only even number could share factor 2 with none because the others are odd. So in the case n even, (n-1,n-2,n-3) has exactly one even, and the multiple of 3 could be either the even or one of the odds. The two odds are consecutive odds, they can share factor? Example: 15 and 17? Actually odd numbers consecutive differ by 2, they could share a common factor >1? Possibly if both multiples of 3? But they are two apart, if one is multiple of 3, the other could be as well? For example 9 and 11? 9 is multiple of 3, 11 not; 15 and 17: 15 multiple of 3, 17 not. Since numbers mod 3 repeat every 3, two numbers differing by 2 can both be divisible by 3 only if 3 divides their difference, which is 2, impossible. So they cannot both be multiples of 3. So the only potential gcd>1 is between the even number and an odd number if the odd number shares an odd factor with the even? e.g., 9 and 6 share 3? But 9 is odd, 6 is even; they can share factor 3. That can happen: if n-2 is even and divisible by 3, and n-1 or n-3 is also divisible by 3? But as argued, among three consecutive, only one multiple of 3. So if n-2 is multiple of 3, then n-1 and n-3 are not. So no shared factor >1 with n-2? Except possibly other odd prime factors? Could n-2 and n-1 share a factor >1? For example, 8 and 9 share? gcd=1. 14 and 15 share? 1. In general, consecutive numbers are coprime. So n-2 and n-1 are consecutive? Actually n-2 and n-1 are consecutive if we order? The numbers are n-3, n-2, n-1. So n-2 and n-1 are consecutive, so gcd=1. Similarly n-2 and n-3 are consecutive? n-2 and n-3 are consecutive as well (difference 1). Actually n-3 and n-2 are consecutive? Yes, n-3 and n-2 differ by 1, so they are coprime. So for n even, (n-1,n-2,n-3) all pairwise? Let's check: n-1 and n-2: consecutive => gcd=1. n-2 and n-3: consecutive => gcd=1. n-1 and n-3 differ by 2: could share factor? Example: 9 and 7 differ by 2, gcd=1. 15 and 13 gcd=1. But can two numbers differing by 2 share an odd factor? Suppose both divisible by 3? Then difference would be multiple of 3, but 2 is not multiple of 3, so impossible. For any prime p>2, if both divisible by p, then p divides their difference 2, so p must divide 2, contradiction. So gcd(n-1,n-3) = 1 if both are odd? Actually they are both odd when n is even: n-1 odd, n-3 odd. So indeed gcd=1. So for n even, (n-1,n-2,n-3) are pairwise coprime! Wait check earlier example: n=12 gave (11,10,9). Are they pairwise coprime? 11 and10 gcd1, 10 and9 gcd1, 11 and9 gcd1. Yes! I earlier thought 10 and12 share 2, but that's for (12,11,10). But (11,10,9) are pairwise coprime. So (n-1,n-2,n-3) for even n are pairwise coprime. For n=10, (9,8,7) are also pairwise coprime? 9 and8 gcd1, 8 and7 gcd1, 9 and7 gcd1. Yes. So product = LCM. For n=8, (7,6,5) are pairwise? 7 and6 gcd1, 6 and5 gcd1, 7 and5 gcd1. Yes. For n=6, (5,4,3) are pairwise? 5 and4 gcd1, 4 and3 gcd1, 5 and3 gcd1. Yes. So indeed (n-1,n-2,n-3) are always pairwise coprime for n even? Let's prove: For n even, n-1, n-2, n-3 are three consecutive numbers? Actually they are consecutive integers: n-3, n-2, n-1. So they are three consecutive integers. Among any three consecutive integers, any two may share a factor? As argued, consecutive numbers are coprime. The pair that are not consecutive are n-3 and n-1, which differ by 2. If both are odd (n even => n-1 and n-3 odd), then they could share a factor? If p divides both, then p divides their difference 2, so p=2 (but they are odd so not divisible by 2) or p=1. So they are coprime. Thus indeed, for even n, (n-1,n-2,n-3) are pairwise coprime. So LCM = product.
For odd n, let's examine (n-1,n-2,n-3). If n is odd, then n-1 is even, n-2 is odd, n-3 is even. So there are two even numbers: n-1 and n-3. They differ by 2, and both even, so they share at least factor 2, possibly more if both divisible by 4? Example: n=13, (12,11,10): 12 and 10 share factor 2, but gcd(12,10)=2. So they are not coprime. Also n-1 and n-2 are consecutive? n-1 even, n-2 odd, consecutive numbers => gcd=1. n-2 and n-3 consecutive? n-2 odd, n-3 even => consecutive? They differ by 1? Actually n-2 and n-3 differ by 1, so they are coprime. So the only non-coprime pair is (n-1,n-3) with gcd at least 2. So LCM of the triple will be product / (something). Specifically, LCM = (n-1)(n-2)(n-3) / gcd(n-1,n-3) maybe times something? Actually more precisely, LCM = (n-1)(n-2)(n-3) / (gcd(n-1,n-3)) because the common factor is only between those two. Since (n-2) is coprime to both, we have LCM = (n-1)(n-3)/gcd(n-1,n-3) * (n-2). But let's compute product divided by d where d = gcd(n-1,n-3). However, note that the factor d might be counted twice? Actually LCM of two numbers a and b is ab/gcd(a,b). So LCM(a,b,c) = LCM(LCM(a,b),c). If c coprime to both, then LCM = ab/gcd(a,b) * c. So product divided by gcd(a,b). So indeed LCM = (n-1)(n-2)*(n-3) / gcd(n-1, n-3). So for odd n, (n-1,n-2,n-3) is not fully coprime; LCM = product / g, where g = gcd(n-1,n-3) ≥ 2.
Now, for odd n, we also have candidate (n,n-1,n-2). Since n is odd, n and n-2 are both odd? If n odd, n-2 also odd, so they might share odd factors. They are two apart, so gcd could be >1 (like if n divisible by 3 and n-2 also divisible by 3? For n odd, n mod 3 can be? n and n-2 differ by 2, so if both divisible by 3, then 3 divides difference 2, impossible. So gcd(n, n-2) cannot be 3. But could be other odd primes? If p divides both n and n-2, then p divides difference 2, so p=2. But they are odd, so not divisible by 2. So gcd(n, n-2) = 1 for odd n. Also n and n-1 are consecutive => coprime; n-1 and n-2 consecutive => coprime. So for odd n, (n,n-1,n-2) are pairwise coprime! Indeed if n odd, then n, n-1, n-2 are three consecutive numbers? Actually they are consecutive: n-2, n-1, n. So they are three consecutive integers. Among them, n-2 and n-1 consecutive => coprime; n-1 and n consecutive => coprime; n-2 and n differ by 2, both odd? If n odd, n-2 odd as well, so they differ by 2, and as argued, any common divisor must divide 2, but since both odd, gcd=1. So they are pairwise coprime. Therefore, for odd n, (n,n-1,n-2) gives product = LCM. So that is optimal candidate.
For even n, (n,n-1,n-2) are not all coprime because n and n-2 are both even, sharing factor 2. So LCM = product / 2 (if gcd exactly 2) or more? Actually gcd(n, n-2) could be >2 if n divisible by 4 and n-2 divisible by 2? But they differ by 2, so if both are even, they can have gcd 2 or maybe more if both divisible by a larger power of 2? Example: n=12, n and n-2=10, gcd=2; n=18, n=18, n-2=16, gcd=2? 18 and16 gcd=2; n=20, n=20, n-2=18, gcd=2; n=22, n=22, n-2=20, gcd=2; Could gcd be 4? Let's test n=4, n=4, n-2=2, gcd=2. n=6, 6 and4 gcd=2. n=8, 8 and6 gcd=2. n=10,10 and8 gcd=2. n=12,12 and10 gcd=2. n=14,14 and12 gcd=2. n=16,16 and14 gcd=2. n=18,18 and16 gcd=2. n=20,20 and18 gcd=2. n=24,24 and22 gcd=2? 24 and22 gcd=2. n=26,26 and24 gcd=2. So it seems for even numbers differing by 2, gcd is at most 2 because if both divisible by 4, then they would be multiples of 4, but if both multiples of 4, then their difference would be multiple of 4, but 2 is not multiple of 4, so they cannot both be multiples of 4. Indeed, if n ≡ 0 mod 4, then n-2 ≡ 2 mod 4, so not divisible by 4. If n ≡ 2 mod 4, then n-2 ≡ 0 mod 4. So one is divisible by 4, the other not. So gcd is exactly 2. So gcd(n, n-2) = 2 for even n (since n and n-2 are both even but not both divisible by 4). So LCM(n, n-2) = n*(n-2)/2. Then with n-1 (odd, coprime to both likely? Check if n-1 shares factor with n or n-2? n-1 is odd and consecutive to n and n-2, so coprime to both). So LCM(n, n-1, n-2) = (n*(n-2)/2)(n-1) = n(n-1)*(n-2)/2. So for even n, that candidate gives product/2.
Now, for even n, we have candidate (n-1, n-2, n-3) which are pairwise coprime as argued, so LCM = (n-1)(n-2)(n-3). Also candidate (n, n-1, n-3): we need to examine pairwise coprimality. For even n, n is even, n-1 odd, n-3 odd. n and n-3: both? n even, n-3 odd. They could share an odd factor. Since they differ by 3, gcd(n, n-3) = gcd(n,3). Actually difference is 3, so any common divisor must divide 3. So gcd(n, n-3) can be 1 or 3. Also n and n-1 are consecutive => coprime. n-1 and n-3 differ by 2, both odd, so gcd could be 1 or perhaps some odd divisor dividing 2? Since both odd, gcd could be 1 (as argued before for two odds differing by 2, any common divisor must divide 2, but if odd, must be 1). Actually more precisely, if p divides both n-1 and n-3, then p divides their difference 2, so p=1 or 2. But p odd, so p=1. So gcd(n-1,n-3)=1. So only possible non-coprime pair is (n, n-3). So if gcd(n, n-3)=1, then the triple is pairwise coprime, and LCM = n*(n-1)(n-3). If gcd=3, then LCM = n(n-1)*(n-3)/3. So candidate (n, n-1, n-3) yields either product or product/3.
Also we could consider (n, n-2, n-3) maybe? Let's analyze. For even n, n and n-2 share factor 2, n-2 and n-3 are consecutive => coprime, n and n-3 could share factor 3. So LCM might be lower.
But could there be a triple like (n, n-1, n-4) or (n-1, n-2, n-4) etc that yields higher LCM? Let's test some values to see if ever beats the best among our three candidates.
We need to systematically check for all n up to 10^6, but we can reason.
The known solution from Codeforces problem 235A? Actually I recall problem "LCM" maybe 235A "LCM Challenge". Let me check memory: There is Codeforces Round #146 problem A "LCM Challenge". I think it's exactly: Given n, find maximum LCM of three numbers ≤ n. And the solution: if n<3, answer n; else if n is odd, answer n*(n-1)(n-2); else if n is even, then check n(n-1)(n-3) if n not divisible by 3, else (n-1)(n-2)*(n-3). I think there was a nuance about divisibility by 3 because of gcd(n,n-3)=3 for n divisible by 3. Yes.
Let's recall: For even n, we have two main candidates: (n, n-1, n-3) and (n-1, n-2, n-3). And also maybe (n, n-1, n-2) but that's product/2, which is typically less than (n-1)(n-2)(n-3) when? Compare (n-1)(n-2)(n-3) vs n*(n-1)(n-2)/2. Cancel (n-1)(n-2). Compare (n-3) vs n/2. So (n-1)(n-2)(n-3) > n*(n-1)(n-2)/2 if (n-3) > n/2 -> 2(n-3) > n -> 2n-6 > n -> n > 6. So for n > 6, (n-1)(n-2)(n-3) > n(n-1)(n-2)/2. For n=6, both equal? n=6: (543)=60, (654)/2=120/2=60. So equal. For n=4: (321)=6, (432)/2=24/2=12, but (3,2,1) product=6, but maybe other triple like (4,3,2) gives 12. So for n=4, (n,n-1,n-2)=12 is greater. But n=4<6, so edge cases. For n=5, (432)=24, (54*3)/2=60/2=30, so (5,4,3)=30 vs (4,3,2)=24, (5,4,3) wins. But (5,4,3) is (n,n-1,n-2) and n odd? 5 odd, so it's covered by odd case. So for even n>6, (n-1,n-2,n-3) > (n,n-1,n-2)/2. So candidate (n,n-1,n-2) is not as good as (n-1,n-2,n-3) for n>6 even. For n=6, they are equal. For n=4, (n,n-1,n-2) is better. But n=4 is even but small; we can handle small n separately.
Now compare (n,n-1,n-3) vs (n-1,n-2,n-3). We need to see which is larger given gcd considerations.
Compute A = LCM(n, n-1, n-3). As argued, if gcd(n, n-3)=1, then A = n*(n-1)(n-3). If gcd=3, then A = n(n-1)*(n-3)/3.
Compute B = (n-1)(n-2)(n-3) (since pairwise coprime for even n). So B = (n-1)(n-2)(n-3).
We need to compare A and B.
Case 1: n even and not divisible by 3. Then gcd(n,n-3)=1 (since difference 3, if n not divisible by 3, then gcd=1). So A = n*(n-1)*(n-3). Compare A vs B:
A = n*(n-1)(n-3)
B = (n-1)(n-2)*(n-3)
Cancel (n-1)*(n-3). So A > B iff n > n-2, which is always true. So A > B for all n>2. So for even n not divisible by 3, A > B, and A is product, so candidate (n,n-1,n-3) gives larger LCM than (n-1,n-2,n-3). So optimum likely A.
But wait, need to ensure A is valid: numbers are ≤ n, okay. And we need to check if A might be less than some other triple not considered. But for n even not divisible by 3, A is huge. Example: n=10 gave A=1097=630, B=987=504. So A > B. For n=14: n even not divisible by 3? 14 mod3=2, not divisible. So A=141311=2002? Actually 1413=182, 11=2002. B=131211=1716. So A > B. So candidate A wins.
Case 2: n even and divisible by 3. Then n multiple of 6. Then gcd(n, n-3) = 3 because n divisible by 3 and n-3 divisible by 3 (since n mod3 =0 => n-3 mod3=0). So A = n*(n-1)*(n-3)/3. Compare A vs B.
A = n*(n-1)(n-3)/3
B = (n-1)(n-2)*(n-3)
Cancel (n-1)(n-3). So A > B iff n/3 > n-2 => n > 3n - 6 => bring: n/3 > n-2? Actually compare n/3 vs (n-2). Multiply both sides by 3: n vs 3(n-2) = 3n-6 => n > 3n-6 => 6 > 2n => n < 3. So for n ≥ 3, n/3 < n-2 (since n-2 - n/3 = (3n-6 - n)/3 = (2n-6)/3 >= 0 for n≥3). Actually check equality: n/3 = n-2 => n = 3n-6 => 2n=6 => n=3. But n even, so n=3 not even. So for even n ≥4, n/3 < n-2, so A < B. Thus for n even and divisible by 3, B > A. So the candidate (n-1,n-2,n-3) gives larger LCM. Example: n=12 (multiple of 6), A = 12119/3 = 12113 = 396? Actually 12119 = 1188, /3 = 396. B = 11109 = 990. So B > A. Also check n=6 (even and divisible by 3): A = 653/3 = 65=30. B = 543=60. So B > A. But for n=6, we saw best overall is 60, B=60, A=30, (n,n-1,n-2)/2? Actually (6,5,4)=60 also. So B ties. So for n multiple of 6, B wins.
Now we also considered (n,n-1,n-2)/2 maybe but B larger.
Thus the algorithm: if n < 3, output n (since we can take n, n, n? Actually n=2, we need LCM of three numbers ≤2, the maximum is 2 (by taking 2,2,2 or 2,2,1). For n=1, answer 1. For n=2, answer 2. But is there any triple that gives 2? Yes. So output n for n<=2? For n=2, output 2. For n=3, output 6? Actually we should compute: n=3, we can take (3,2,1) LCM=6, so output 6, which is 321. So our formula for odd n: n*(n-1)(n-2) works for n=3: 321=6. For n=2, that formula would give 21*0 invalid. So need separate.
Thus for n>=3, we can do:
if n is odd:
ans = n*(n-1)(n-2)
else:
# n even
ans1 = (n-1)(n-2)(n-3) # since they are coprime
ans2 = n(n-1)(n-3)
if n % 3 != 0:
ans = max(ans1, ans2) # but ans2 > ans1 as argued, so could just take ans2? But careful: ans2 may not be coprime if n%3==0, but we already condition on n%3 != 0. So ans2 is product (since gcd(n,n-3)=1). For n even and not divisible by 3, ans2 > ans1. So max is ans2.
else: # n divisible by 3, i.e., n%6==0
ans = ans1 # because ans2 reduced
But also we might consider ans0 = n(n-1)(n-2)//2, but for n>6, ans1 > ans0; for n=6, ans1 = 60 = ans0; for n=4, ans0 = 12, ans1=6, so ans0 larger. But n=4 is even and not divisible by 3? n=4%3=1, so ans2 = 431 =12, which equals ans0? Actually ans0 = 432/2=12, ans2=12, ans1=6. So ans2 also gives 12. So for n=4, ans2 works. For n=2, we don't use. For n=6, ans2 = 653/3? Wait n=6 even and divisible by 3, ans2 = 653/3=30, ans1=60, ans0=60. So ans1 best. So the rule "if even: if n%3!=0 then ans = n(n-1)(n-3); else ans = (n-1)(n-2)(n-3)" seems to cover also n=4,6? n=4%3=1 -> ans = 431=12, correct. n=6%3=0 -> ans = 543=60, correct. n=8%3=2 -> ans = 875=280, correct (from earlier). n=10%3=1 -> ans=1097=630, correct. n=12%3=0 -> ans=11109=990, correct. n=14%3=2 -> ans=141311=2002, we should verify if any triple yields >2002? B=131211=1716, A=2002, that's max. Also (14,13,12) gives 141312/2? Actually (14,13,12) LCM =? 14 and12 share2 => LCM=1412/2=84, with13=1092. So lower. So 2002 likely max. Check (13,12,11)=131211=1716; (14,13,11)=2002; (14,12,11) = LCM(14,12)=84, with11=924; So indeed 2002. So rule works.
But we must also consider n=1,2. For n=1, answer 1. For n=2, answer 2. Let's test n=2: can we get LCM 2? Yes (2,2,2) or (2,2,1). So answer 2. Our rule for n>=3 yields: n even, n%3? 2%3=2 !=0, so ans=21(-1)? invalid because n-3=-1. So not applicable. So need to handle n<3 separately.
Also for n=3: odd -> ans=321=6 correct.
Now we should also consider if there is any scenario where another triple yields higher LCM than these candidates for n >= 3. The known solution seems accepted. I recall the Codeforces problem 235A? Actually I think it's "A. LCM Challenge" from Codeforces Round #146 (Div. 2) or something. The editorial indeed says: if n < 3, print n; else if n is odd, print n*(n-1)(n-2); else if n is even and n % 3 != 0, print n(n-1)(n-3); else print (n-1)(n-2)*(n-3). So it matches.
But we need to be absolutely sure. Let's try to think of possible counterexamples: maybe for some even n, the best triple is (n, n-2, n-3) or (n, n-1, n-4) if n is such that n and n-3 share factor >3? Actually gcd(n,n-3) can only be 1 or 3 because any common divisor must divide their difference 3. So it's either 1 or 3. So (n,n-1,n-3) yields product when gcd=1, product/3 when gcd=3. So we have that.
Could there be triple (n, n-2, n-3) with LCM possibly larger? Let's compute candidate C = LCM(n, n-2, n-3). For even n, n and n-2 share 2, so LCM(n, n-2) = n*(n-2)/2. Then with n-3. Since n-3 is odd, may share factors with n or n-2. Need to consider. For n even, n and n-3 could share factor 3. n-2 and n-3 are consecutive => coprime. So LCM = [n*(n-2)/2] * (n-3) / gcd(LCM(n,n-2), n-3)? But easier: LCM of three numbers = product / (some factor). We can compute the LCM as product divided by d, where d accounts for common prime factors. But we can compare with A and B. Let's attempt to derive a bound: For even n, the product of any three numbers ≤ n is at most n*(n-1)(n-2). But the LCM cannot exceed that. For a triple not containing n, max product is (n-1)(n-2)(n-3). So any triple without n has LCM ≤ (n-1)(n-2)(n-3) (if pairwise coprime). But if not pairwise coprime, LCM < that product. So the maximum possible LCM for a triple without n is at most (n-1)(n-2)(n-3). And B achieves exactly that (since they are pairwise coprime for even n). So B is the maximum among triples not containing n. Because any other triple without n has product ≤ (n-1)(n-2)*(n-3), and if not coprime, LCM ≤ product < that. So B is maximum possible for triples without n.
Now consider triples containing n. Among those, we want to maximize LCM. The maximum product among triples containing n is n*(n-1)(n-2). However, due to common factors, LCM might be less. We can consider triples with n and two other numbers x<y ≤ n. The product is nxy. To maximize LCM, we want x and y as large as possible and also coprime with n and each other. The largest possible x and y are n-1 and n-2. But n and n-2 share factor 2, so LCM = n(n-1)*(n-2)/2. That's candidate D.
But perhaps we can get a larger LCM by taking n, n-1, and some other number not as large as n-2 but that is coprime with both, e.g., n-3, n-5, etc. The product then is n*(n-1)k. For k ≤ n-2, the product reduces. But the LCM might be equal to product if all pairwise coprime. So we need to compare n(n-1)k (with k such that triple is pairwise coprime) versus D = n(n-1)*(n-2)/2.
Since n*(n-1) is common, we compare k vs (n-2)/2. So if we can find a k ≤ n-2 that is coprime with n and n-1 and also larger than (n-2)/2, then that triple would beat D. And the largest possible k that is coprime to both is ideally n-3 (since n-2 shares factor 2 with n, so not coprime; n-3 may be coprime if gcd(n,n-3)=1; else maybe n-4, n-5 etc). So candidate is n-3 if coprime, else maybe n-5, etc. But we also must ensure k ≤ n-2. So the maximum possible k is n-3 if it is coprime. If n-3 is not coprime (i.e., gcd(n,n-3)=3), then product would be n*(n-1)(n-3)/3. Compare that to D? D = n(n-1)(n-2)/2. Compare (n-3)/3 vs (n-2)/2. For n≥? (n-3)/3 < (n-2)/2 for n>? Let's check: (n-2)/2 - (n-3)/3 = (3(n-2) - 2(n-3))/6 = (3n-6 -2n+6)/6 = n/6. So >0 for n>0. So D > (n-3)/3 * n(n-1) for n>0. So if n-3 not coprime, D is better. But D itself is n*(n-1)(n-2)/2. Compare D to B? B = (n-1)(n-2)(n-3). As earlier, for even n>6, B > D because (n-3) > n/2? Actually we compared B vs D earlier: B = (n-1)(n-2)(n-3), D = n(n-1)*(n-2)/2, so B > D if n-3 > n/2 => n > 6. So for n>6, B > D. So when n even and n%3==0, B is better than D. So the best among triples containing n might be D, but B (without n) may be better. So overall we need to compare B vs best triple with n.
Now for n even and not divisible by 3, we have candidate A = n*(n-1)(n-3) which is pairwise coprime, so LCM = product. Compare A to D: A = n(n-1)(n-3), D = n(n-1)*(n-2)/2. Compare (n-3) vs (n-2)/2. For n>? (n-3) > (n-2)/2 if 2(n-3) > n-2 => 2n-6 > n-2 => n > 4. So for n≥5, A > D. So A dominates D. So the best triple containing n is A (since it's pairwise coprime and uses largest possible k = n-3). Could there be a triple with n and a different k > n-3? No, because k ≤ n-1, but n-1 is already used, k must be distinct? Actually we could use n, n-1, n-4 if n-3 is not coprime, but n-4 could be larger than n-3? Actually n-4 < n-3, so smaller. So A is best possible containing n because it uses the largest possible second and third numbers that can be pairwise coprime with n and each other. The next candidate would be n, n-1, n-5, product smaller. So for even n not divisible by 3, A is best among triples containing n.
Now for even n divisible by 3, A is reduced by factor 3, so A = n*(n-1)(n-3)/3. Compare to D = n(n-1)(n-2)/2. Compare A vs D: A/D = [n(n-1)(n-3)/3] / [n(n-1)(n-2)/2] = (2/3)(n-3)/(n-2). For large n, ~2/3. So D > A for n>? Let's compute numeric: n=6, A=30, D=60; D > A. n=12, A=396, D=660; D > A. So D > A. But D itself may be less than B. Since B = (n-1)(n-2)(n-3). Compare D vs B: D = n*(n-1)(n-2)/2, B = (n-1)(n-2)(n-3). Cancel (n-1)(n-2): compare n/2 vs (n-3). For n=12, n/2=6, n-3=9 => B > D. For n=6, n/2=3, n-3=3 => equal. So B >= D for n>=6. So for n divisible by 6, B is at least as large as D, and for n>6, B > D. So best overall is B.
Thus the only candidates we need to consider are:
If n odd: (n, n-1, n-2) -> LCM = product.
If n even:
Compute ans1 = (n-1)(n-2)(n-3) (pairwise coprime, so LCM = product).
Compute ans2 = n*(n-1)(n-3) if gcd(n, n-3)==1, else ans2 = n(n-1)(n-3)/3. But we can compute LCM directly using 64-bit arithmetic. Actually we can compute LCM using formula: lcm(a,b) = a/gcd(a,b)b, then lcm(lcm(a,b),c) = lcm(a,b,c). But since numbers up to 10^6, product fits in 64-bit? n up to 1e6, product of three numbers up to 1e18? 1e6^3 = 1e18, which fits in 64-bit unsigned? 2^63-1 ≈ 9.22e18, so 1e18 fits. But careful: 1e6999999999998 ≈ 9.99997e17 < 1e18, so within signed 64-bit. For n=1e6, (n-1)(n-2)(n-3) ≈ 9.99994e17 < 1e18, safe. So we can compute product as 64-bit integer. However, when we need LCM, it may be the same as product if coprime, but if not, we need to compute LCM correctly, which may be less than product. But we can compute LCM using division to avoid overflow: lcm = a / gcd(a,b) * b; then intermediate might be up to product but within 1e18, safe if we use unsigned 64-bit or __int128? But typical 64-bit signed can hold up to 9.22e18, product of three numbers up to 1e6 is at most 1e18, so it's safe for intermediate multiplication if we do in 128-bit? Actually a/gcd(a,b) * b might be up to a*b, which is about 1e12, safe. Then lcm of that and c: (lcm_ab)/gcd(lcm_ab, c) * c. lcm_ab could be up to ~1e12, times c up to 1e6 gives ~1e18, safe. So we can compute LCM using 64-bit with careful ordering to avoid overflow: compute lcm_ab = a / gcd(a,b) * b (this multiplication fits within 64-bit because a/gcd(a,b) <= a ≤ 1e6, b ≤ 1e6, product ≤ 1e12, safe). Then compute lcm = lcm_ab / gcd(lcm_ab, c) * c; lcm_ab ≤ 1e12, c ≤ 1e6, product ≤ 1e18, safe as long as we do division before multiplication: tmp = lcm_ab / g; then lcm = tmp * c; if tmp up to 1e12 and c up to 1e6, product up to 1e18, safe. So 64-bit signed (long long) is okay.
But we may not need to compute LCM for all candidates: we know analytical results. However, to be safe and simple, we can compute the three candidate values:
cand1 = lcm(n, n-1, n-2)
cand2 = lcm(n, n-1, n-3)
cand3 = lcm(n-1, n-2, n-3)
and take max. But need to ensure these are valid: for n=3, n-3=0 invalid, so we only consider valid ones. For n=4, n-3=1 valid; n=5, n-3=2 valid; etc. For n=2, n-3=-1 invalid, but we handle n<3 separately.
So we can implement: if n<3, answer n. Else compute cand1 = lcm triple (n, n-1, n-2). cand2 = if n>=4 (since n-3 >=1) compute lcm(n, n-1, n-3). cand3 = if n>=5? Actually n-1,n-2,n-3 all >=1 when n>=4? For n=4, (3,2,1) all >=1, so n>=4. For n=3, (2,1,0) invalid. So for n>=4, cand3 valid. For n=3, only cand1 valid. For n=4, all three valid? n=4: (4,3,2) cand1; (4,3,1) cand2; (3,2,1) cand3. So compute all valid and take max. That will automatically yield correct answers without needing parity analysis.
But we must be careful: for n even and not divisible by 3, cand2 = LCM( n, n-1, n-3 ) = product because coprime? Actually may be product; but our LCM function will compute correctly. So we can just compute these three candidates and take max. This covers all cases? But what about triple (n, n-2, n-3) maybe? Could that ever beat these? Let's test for n where cand1,cand2,cand3 are not optimal. We need to ensure that the maximum LCM always comes from one of these three triples. Is that proven? I think it's known from editorial that it's enough to check these three. But let's double-check with some random n values via reasoning or maybe small brute-force for n up to, say, 200 to be safe. Since we can't run code here, we can do mental reasoning.
But I recall that in the Codeforces problem, the solution indeed only checks these three triples, and it's proven in editorial. However, let's attempt a proof sketch.
We aim to maximize LCM(a,b,c) for 1 ≤ a ≤ b ≤ c ≤ n (wlog sorted). Let c be the maximum. Cases:
If c = n. Then we choose a,b ≤ n. To maximize LCM, we want a and b as large as possible and coprime to each other and to c. The largest possible b is n-1 (since distinct maybe not required but duplicates not helpful). So b = n-1 (or possibly n if duplicates, but then LCM = n or nsomething). b = n-1 is best because it's the largest less than n and generally coprime with n? n and n-1 always coprime. So b = n-1. Now we need to choose a (≤ b) to maximize LCM(n, n-1, a). Since n and n-1 are coprime, LCM(n, n-1, a) = LCM( n(n-1), a )? Actually lcm(n, n-1) = n*(n-1) because they are coprime. So the overall LCM = lcm( n*(n-1), a ). To maximize this, we want a as large as possible and coprime with n*(n-1) to get product = n*(n-1)a. If a shares factors, LCM will be less. So we want the largest a ≤ n-1 (distinct from n-1? can be equal? if a = n-1, then triple (n, n-1, n-1) gives LCM = lcm(n, n-1) = n(n-1), which is less than n*(n-1)(n-2) if a = n-2 coprime? But if n-1 duplicate, not helpful). So a should be the largest integer ≤ n-1 that is coprime to both n and n-1. Since n and n-1 are coprime, being coprime to n(n-1) is equivalent to being coprime to both n and n-1 individually. So we need to find the largest a ≤ n-1 such that gcd(a, n)=1 and gcd(a, n-1)=1. The largest candidate is n-2 if it satisfies both conditions. Check gcd(n-2, n)=gcd(n,2) which is 1 if n odd, 2 if n even. So if n odd, n-2 works because n odd => n and n-2 both odd, gcd(n, n-2)=1 (since difference 2 and both odd => gcd=1). Also gcd(n-2, n-1)=1 because consecutive. So for odd n, a = n-2 is coprime, giving triple (n, n-1, n-2) with LCM = product. If n is even, n-2 is even, so gcd(n, n-2)≥2, not coprime. So we need to look for the next largest a. The next candidates are n-3, n-4, etc. Since n-3 is odd (n even => n-3 odd). Check gcd(n, n-3): difference 3, so any common divisor must divide 3. So if 3 does not divide n, then gcd=1. Also gcd(n-3, n-1) = gcd(n-3,2) =? Actually n-1 odd, n-3 odd, difference 2, so gcd can be 1 (since any common divisor must divide 2, but they are odd, so gcd=1). So if n not divisible by 3, then a = n-3 works. If n divisible by 3, then gcd(n, n-3)=3, so not coprime. Then we try n-4? n-4 even? n even => n-4 even, shares factor 2 with n, so not coprime. n-5: n-5 odd, check gcd(n, n-5): difference 5, so common divisor can be 1 or 5. If n not divisible by 5? But also need gcd with n-1. We can continue, but note that the product n*(n-1)a decreases as a decreases. Meanwhile, if we cannot find a a close to n-1 that is coprime, maybe the best triple with c=n is not as good as a triple without n, specifically (n-1, n-2, n-3) which is pairwise coprime for even n. So we need to compare. In practice, the largest a that is coprime to both might be n-3 if n%3 !=0, else perhaps n-5, etc. But is it possible that n-5 yields a product larger than (n-1)(n-2)*(n-3)? Let's compare:
If n even and divisible by 3, then n is multiple of 6. Then a = n-3 fails (gcd=3). Next candidate a = n-5. Check conditions: need gcd(n, n-5)=1 and gcd(n-5, n-1)=1. gcd(n, n-5) can be >1 if n divisible by 5? For n multiple of 6, may also be multiple of 5? Could be 30, 60, etc. If n is multiple of 5, then n-5 is also multiple of 5, so gcd≥5. So may fail. Similarly other factors. So the search for a might go further down. But product n*(n-1)a with a = n-5 could be compared to B = (n-1)(n-2)(n-3). For large n, B is roughly n^3, while n(n-1)(n-5) is also about n^3 - 6n^2, but B is n^3 - 6n^2 + ... Let's compute difference: B = (n-1)(n-2)(n-3) = n^3 - 6n^2 + 11n -6. n(n-1)(n-5) = (n^2 - n)(n-5) = n^3 -5n^2 - n^2 +5n? Actually expand: n(n-1) = n^2 - n, times (n-5) = (n^2 - n)(n-5) = (n^2 - n)n -5(n^2 - n) = n^3 - n^2 -5n^2 +5n = n^3 -6n^2 +5n. So B - [n(n-1)(n-5)] = (n^3 -6n^2+11n-6) - (n^3 -6n^2+5n) = 6n -6 = 6(n-1). So B is larger by 6(n-1). So B > n*(n-1)*(n-5). So for n even and divisible by 3, B is larger than any triple with c=n where a ≤ n-5, because the product of the triple without n (B) already beats that. And also the LCM of triple with n and a might be less than product if a not coprime, further reducing. So B remains candidate.
But could there be triple with c = n-1 (i.e., not including n) that is better than B? B uses the three largest numbers less than n: n-1,n-2,n-3. But what about c = n-1, b = n-2, a = n-4? That product is smaller than B because a is smaller. So B is maximum product among triples not including n, and since they are pairwise coprime for even n, LCM = product, so B is max among those.
Thus the only possible candidates that could beat B are triples including n with some a that yields LCM maybe close to product. The best such candidate is with a = n-3 if coprime, else maybe a = n-5 etc but those give product less than B. So the only two serious contenders are:
Triple with n, n-1, n-3 if gcd(n, n-3)=1 (so product = LCM)
Triple with n-1, n-2, n-3 (always pairwise coprime for even n)
And if n is odd, then n, n-1, n-2 are pairwise coprime, so that's best.
Thus the three candidates suffice.
But need to consider edge cases where n is even and n%3 !=0 but maybe n and n-3 are coprime but n-3 might share factor with n-1? We argued gcd(n-1, n-3)=1 because both odd? For n even, n-1 odd, n-3 odd, difference 2, any common divisor must divide 2, but they are odd, so gcd=1. So yes, coprime. So triple (n, n-1, n-3) is pairwise coprime when gcd(n,n-3)=1. So LCM = product.
Now, for n even and divisible by 3, we have B as best.
What about n odd but maybe n-1, n-2, n-3 could be better? For n odd, (n-1,n-2,n-3) includes two evens, so not all coprime, LCM = product / gcd(n-1,n-3). Since gcd(n-1,n-3) = ? For n odd, n-1 even, n-3 even, differ by 2, so gcd at least 2. So LCM = (n-1)(n-2)(n-3) / g, where g ≥ 2. Compare to (n,n-1,n-2) which is product. Since (n,n-1,n-2) product is larger than (n-1)(n-2)(n-3) for n≥4? Let's compare: n*(n-1)(n-2) vs (n-1)(n-2)(n-3). Cancel (n-1)(n-2): compare n vs n-3. n > n-3, so product is larger. And the LCM of (n-1,n-2,n-3) is even smaller because divided by g≥2. So (n,n-1,n-2) wins.
Thus for n odd, (n,n-1,n-2) is optimal.
So the candidate set covers all.
Now, for n=1,2 we handle separately. For n=1, only number is 1, LCM(1,1,1)=1. For n=2, maximum LCM? Options: (2,2,2)=2, (2,2,1)=2, (2,1,1)=2, (1,1,1)=1. So answer 2.
Also what about n=3 we already covered.
Now, what about n=0? Not in constraints.
Thus algorithm:
Read n.
If n == 1: print 1.
Else if n == 2: print 2.
Else (n >= 3):
Compute cand1 = lcm(lcm(n, n-1), n-2).
If n >= 4: compute cand2 = lcm(lcm(n, n-1), n-3).
If n >= 4: compute cand3 = lcm(lcm(n-1, n-2), n-3). Actually for n=4, (3,2,1) works.
Take max among computed candidates.
But note: For n=3, cand2 and cand3 are invalid, so we only use cand1.
For n=4, compute all three.
Implementation of LCM: Use 64-bit (long long). Compute gcd via std::gcd or custom Euclidean.
We need to be careful about integer overflow during multiplication: When computing lcm(a,b) = a / gcd(a,b) * b, ensure that a/gcd(a,b) * b does not overflow 64-bit signed. As argued, a and b ≤ 1e6, so product ≤ 1e12, fits. Then lcm_ab / gcd(lcm_ab, c) * c: lcm_ab ≤ 1e12, c ≤ 1e6, product ≤ 1e18, which may exceed 2^63-1? 2^63-1 ≈ 9.22e18, and 1e18 < 9.22e18, so it fits in signed 64-bit. However, we must be careful that lcm_ab could be up to about 1e12, dividing by gcd (≥1) yields ≤ 1e12, then multiply by c (≤1e6) yields ≤ 1e18. So it's safe.
But consider n=1e6, compute lcm(n, n-1) = 1e6 * 999,999 (since coprime) = 999,999,000,000 ≈ 1e12. Then lcm with n-2: gcd(lcm_ab, n-2). lcm_ab is about 1e12, n-2=999,998. The gcd might be something. But product lcm_ab * (n-2) could be ~1e12 * 1e6 = 1e18. If we compute as (lcm_ab / g) * (n-2), intermediate (lcm_ab / g) could be up to 1e12, times n-2 up to 1e6, product up to 1e18, fits. So safe.
Thus we can implement direct LCM.
But simpler: Since we know analytical result, we can just compute:
if n<3: ans = n
else if n%2==1: ans = n*(n-1)(n-2)
else:
ans = (n-1)(n-2)(n-3)
tmp = n(n-1)(n-3)
if n%3 != 0: # then tmp is LCM? But careful: tmp may not be LCM if not coprime, but if n%3!=0, then gcd(n,n-3)=1, so triple is pairwise coprime, so LCM = product = tmp. So ans = max(ans, tmp)
else: # n divisible by 3, tmp/3 is LCM? Actually LCM = tmp/3 because gcd(n,n-3)=3. But we can compute LCM properly. But we can still compute ans = max(ans, lcm(n,n-1,n-3)). But analytical: for n%3==0, lcm = tmp/3, which is less than ans as argued. So we can just compute ans as max of ans and that lcm if needed. But for n%3==0, tmp/3 is smaller, so ans remains ans. So we could simply compute ans = max( (n-1)(n-2)*(n-3), lcm(n,n-1,n-3) ). But to avoid missing any case, just compute both via LCM function.
Simplest: compute three candidate LCMs using a function lcm3(a,b,c) and take max.
We'll implement function lcm2(a,b) = a / gcd(a,b) * b (using long long). Then lcm3(a,b,c) = lcm2(lcm2(a,b), c).
Now we also need to consider possibility that the best triple includes numbers not all distinct. But as we see, duplicates reduce LCM, so not needed.
Now, what about n=1? Output 1.
n=2? Output 2.
Now confirm that for n=1000000, the answer fits 64-bit? Compute max among candidates. For n even (1000000 is even) and not divisible by 3? 1000000 mod3 = 1, so candidate (n, n-1, n-3) = 1000000 * 999999 * 999997. That product is about 1e6^3 = 1e18, specifically ~ 10^18 - something, within 64-bit. For n=1000000, check overflow: 1,000,000 * 999,999 = 999,999,000,000 = 9.99999e11. Multiply by 999,997: approx 9.99999e11 * 1e6 = 9.99999e17 = ~1e18. So within 9e18? Actually 9.99999e17 < 9.22e18, safe. So long long (signed) can hold it. But we need to compute LCM, not necessarily product; but when numbers are coprime, LCM = product, so we may compute product directly. In our LCM function, we do stepwise division which ensures no overflow.
Thus implement as described.
Now, one more nuance: For n=4, cand2 = lcm3(4,3,1) = LCM(4,3)=12, LCM(12,1)=12. So 12. cand1 = lcm3(4,3,2)=12, cand3 = lcm3(3,2,1)=6. So max=12, correct.
Now n=5: cand1 = lcm3(5,4,3)=60; cand2 = lcm3(5,4,2)=20; cand3 = lcm3(4,3,2)=12? Actually (4,3,2) LCM = 12. So max=60. correct.
Now n=3: cand1 only: 321=6.
Now n=2: we handle separately.
Thus the solution is straightforward.
Time complexity O(1). Memory O(1).
Let's implement:
Use long long (64-bit) for all computations.
Function gcd using Euclidean algorithm ( iterative).
Function lcm(a,b) = a / gcd(a,b) * b.
Function lcm3(a,b,c) = lcm(lcm(a,b), c).
Implementation steps:
Read integer n.
If n == 1: print 1; return.
If n == 2: print 2; return.
Initialize ans = lcm3(n, n-1, n-2).
If n >= 4: ans = max(ans, lcm3(n, n-1, n-3)).
If n >= 4: ans = max(ans, lcm3(n-1, n-2, n-3)).
Print ans.
We must be careful with the order for n>=4: for n=4, n-3=1, which is valid. So it's fine.
Now, could there be a scenario where n=3 but we also consider (n-1,n-2,n-3) = (2,1,0) invalid. So skip.
Thus final.
Now we need to be careful about data types. Use long long in C++ (or int64 in other languages). Compute gcd with built-in or custom.
Now, we should also consider that for n=1, the triple can be (1,1,1) giving LCM=1, correct.
For n=2, triple (2,2,2) gives 2, correct.
Now, is there any scenario where n is 0? No.
Thus code is ready.
But before finalizing, let's test some more borderline:
n=1000000: compute ans = max among three. For speed, O(1) operations. gcd computations cheap.
Now verify that lcm3(n, n-1, n-3) when n even and n%3 !=0 yields product, but our LCM function will compute correctly.
Now one more check: For n=9 (odd) we already did.
Now n=21 (odd) works.
Now n=22 (even) works.
Now n=24 (even multiple of 3) works.
Now n=30 (even multiple of 3) works.
Now n=1e6-1? Actually n can be 999999, which is odd. Then ans = n*(n-1)*(n-2) = 999999 * 999998 * 999997. That's huge but fits. Our lcm3 will compute stepwise: lcm(999999,999998) =? They are consecutive? 999999 and 999998 are consecutive, so lcm = product ≈ 1e12, then lcm with 999997: need gcd(product, 999997). Since 999997 is prime? Not necessarily, but product maybe divisible by it? Since 999997 is not equal to the numbers, but could share factor with product? But our LCM function will compute correctly. However, there might be overflow in the product step? lcm(999999,999998) = 999999 * 999998 (since gcd=1) = approx 1e12, safe. Then lcm with 999997: compute gcd(lcm_ab, 999997). Since 999997 is close to 1e6, product lcm_ab * 999997 ≈ 1e18, safe. So fine.
Thus solution stands.
Now produce final answer.
But the problem statement says: "they don't have to be distinct". So duplicates allowed, but our candidates are distinct and produce max.
Now also note that for n=1, output 1; for n=2, output 2; for n=3 output 6.
Now implement.
Edge case: n=0 not present.
Now we can write solution in C++:
code
#include <iostream>
#include <algorithm>
using namespace std;
typedef long long ll;
ll gcd(ll a, ll b) { return b ? gcd(b, a % b) : a; }
ll lcm(ll a, ll b) { return a / gcd(a, b) * b; }
ll lcm3(ll a, ll b, ll c) { return lcm(lcm(a, b), c); }
int main() {
ll n;
cin >> n;
if (n == 1) {
cout << 1 << endl;
} else if (n == 2) {
cout << 2 << endl;
} else {
ll ans = lcm3(n, n-1, n-2);
if (n >= 4) {
ans = max(ans, lcm3(n, n-1, n-3));
ans = max(ans, lcm3(n-1, n-2, n-3));
}
cout << ans << endl;
}
return 0;
}
Double-check n=4: the if n>=4 block executes both max, fine.
Test with given examples:
Input 9: n=9, ans = lcm3(9,8,7)=504; n>=4 true, compute lcm3(9,8,6) =? 9,8,6 LCM=72; lcm3(8,7,6)=168; max remains 504. Output 504.
Input 7: ans = lcm3(7,6,5)=210; lcm3(7,6,4)=84; lcm3(6,5,4)=60; max=210. Output 210.
Works.
Now, confirm for n=2: output 2. For n=1: output 1.
Thus final answer.
But we might also consider n=3: ans = lcm3(3,2,1)=6. Since n>=4 false, output 6.
Thus final.
We can also simplify: For n<3 directly output n; else compute the three candidates, but for n=3, only first candidate exists, but we can still compute lcm3(3,2,1) which uses n-2=1, n-3=0? Actually for n=3, n-3=0, but we are not using it. Our code only uses n-3 when n>=4. So safe.
Now, any pitfalls: The lcm function should handle a or b zero? But a,b,c are positive integers (≥1). So no zero. For n>=4, n-3 >=1, so fine.
Now, we must use 64-bit. Use long long.
Thus final answer.
Now I'll produce the final solution in the answer. ```cpp
#include
#include
using namespace std;
typedef long long ll;
// Greatest common divisor
ll gcd(ll a, ll b) {
return b ? gcd(b, a % b) : a;
}
// Least common multiple of two numbers
ll lcm(ll a, ll b) {
return a / gcd(a, b) * b;
}
// Least common multiple of three numbers
ll lcm3(ll a, ll b, ll c) {
return lcm(lcm(a, b), c);
}
int main() {
ll n;
cin >> n;
code
if (n == 1) {
cout << 1 << endl;
return 0;
}
if (n == 2) {
cout << 2 << endl;
return 0;
}
// n >= 3
ll ans = lcm3(n, n - 1, n - 2);
if (n >= 4) {
ans = max(ans, lcm3(n, n - 1, n - 3));
ans = max(ans, lcm3(n - 1, n - 2, n - 3));
}
cout << ans << endl;
return 0;