Data Structures & AlgorithmsModerateCodeforces Div2 C/D · Amazon SDE OA
Find the smallest positive integer x such that x ≡ 2 (mod 3), x ≡ 3 (mod 5) and x ≡ 2 (mod 7). Solve by brute-force search.
Find the smallest positive integer x such that x ≡ 2 (mod 3), x ≡ 3 (mod 5) and x ≡ 2 (mod 7). Solve by brute-force search.
print(next(x for x in range(1,10**6) if x%3==2 and x%5==3 and x%7==2))What is printed?
- A. 98
- B. 23
- C. 8
- D. 7
Correct answer: B. 23
The residues fix x modulo 105, and the unique residue class satisfying all three is found by search. Requiring x mod 7 = 0 changes the class, dropping the mod 7 condition gives the smallest x with only two residues, and adding the residues is not a valid method.
More Coding Interview Problems questions
- How many trailing zeros does 100! have in base 10? Use Legendre's formula with powers of 5, counted by running the loop over p = 5, 25…
- The string s = "abcbdab" needs characters deleted to become a palindrome. What is the minimum number of deletions? Use n minus the longest…
- A 4×4 grid has rows "..#.", ".##.", "##.#", "#...". Entering a cell costs 1 if it is '#' and 0 otherwise, and you may move in four…
- Array c = [1, 2, 3, 0, 3, 3, 0, 6]. In how many ways can it be split into three non-empty contiguous parts whose sums are all equal? Run…
- Array d = [1, 2, 1, 3, 4, 3, 5]. Count the contiguous subarrays containing exactly three distinct values. Run the brute-force code. python…
- Houses have values e = [2, 7, 9, 3, 1, 8, 4, 6] and a robber cannot take two adjacent houses. Find the maximum sum that can be taken…