Practice
n by marking multiples of odd primes only, and why?Solution
Step 1: Identify outer loop complexity
The outer loop runs over odd numbers up to sqrt(n), roughly O(sqrt(n)/2).Step 2: Analyze inner marking loop
Each composite number is marked only once by its smallest prime factor, leading to total marking cost O(n log log n).Final Answer:
Option D -> Option DQuick Check:
Classic sieve time complexity is O(n log log n) [OK]
- Confusing with brute force O(n sqrt(n))
- Assuming linear O(n) due to single visits
- Overestimating to O(n log n)
def gcd_iterative(a, b):
while b != 0:
a, b = b, a % b
return a
def lcm_safe(a, b):
gcd = gcd_iterative(a, b)
return a * b // gcd
if __name__ == '__main__':
a, b = 1000000, 1000000
print("LCM =", lcm_safe(a, b))What is the subtle bug in this code?Solution
Step 1: Analyze lcm_safe function
The function computes LCM as a * b // gcd. Multiplying a and b first can overflow for large inputs before division.Step 2: Identify the bug
To avoid overflow, division by gcd should happen before multiplication: (a // gcd) * b.Final Answer:
Option A -> Option AQuick Check:
Multiplying large numbers before division risks overflow [OK]
- Ignoring overflow risk in LCM calculation
- Misunderstanding Euclidean algorithm base case
def reverse(x: int) -> int:
INT_MAX = 2**31 - 1
INT_MIN = -2**31
sign = -1 if x < 0 else 1
x = abs(x)
def helper(num, rev):
if num == 0:
return rev
pop = num % 10
# Bug: Missing overflow check here
rev = rev * 10 + pop
return helper(num // 10, rev)
rev = helper(x, 0)
return sign * rev if rev != 0 else 0
What is the root cause of incorrect behavior in this code?Solution
Step 1: Identify missing overflow check
The code multiplies rev by 10 and adds pop without checking if rev will overflow 32-bit integer limits.Step 2: Consequence of missing check
Without overflow check before multiplication, the reversed integer can exceed INT_MAX causing incorrect results or runtime errors.Final Answer:
Option A -> Option AQuick Check:
Overflow check must precede rev update to avoid errors [OK]
- Ignoring overflow before multiplying rev by 10
- Assuming sign handling fixes overflow
- Misunderstanding digit extraction for negatives
n but now the input n can be up to 10^12, too large for a standard sieve. Which approach is best suited to handle this scenario efficiently?Solution
Step 1: Understand memory constraints
Standard sieve requires O(n) space, which is infeasible for n=10^12.Step 2: Identify suitable algorithm
Segmented sieve divides the range into manageable blocks, using O(sqrt(n)) memory and still marking primes efficiently.Step 3: Compare alternatives
Brute force is too slow, DP is not applicable, and standard sieve is too large.Final Answer:
Option A -> Option AQuick Check:
Segmented sieve is standard for large n prime counting [OK]
- Trying standard sieve for huge n
- Using brute force for large ranges
- Misapplying DP to prime counting
Solution
Step 1: Understand LCM of multiple numbers
LCM(a,b,c) = LCM(LCM(a,b), c). Computing pairwise LCMs iteratively is standard and safe.Step 2: Evaluate options
Multiplying all numbers first risks overflow. Brute force is inefficient. Computing GCD of all and dividing product is incorrect because GCD of all does not directly give LCM.Final Answer:
Option B -> Option BQuick Check:
Pairwise LCM with overflow-safe method is correct and efficient [OK]
- Multiplying all numbers first causing overflow
- Misusing GCD of all numbers to compute LCM
