Data Structures & AlgorithmsVery hardProduct Company SDE · CP practice
Problem: How many ordered pairs (a, b) with 1 <= a, b <= 30 have a^2 + b^2 a perfect square? Run the code.
Problem: How many ordered pairs (a, b) with 1 <= a, b <= 30 have a^2 + b^2 a perfect square? Run the code.
print(sum(1 for a in range(1, 31) for b in range(1, 31) if int(round((a * a + b * b) ** 0.5)) ** 2 == a * a + b * b))What is printed?
- A. 26
- B. 24
- C. 28
- D. 30
Correct answer: A. 26
Pairs such as (3,4), (4,3), (6,8) and (5,12) give Pythagorean triples; the count of ordered pairs is 26.
More Coding Interview Problems questions
- Problem: How many distinct strings can be formed by rearranging the letters of 'aabbc'? Run the code. python from itertools import…
- Problem: A 3-person committee is chosen from 6 people. How many committees include at least one of two specific people A or B? Run the…
- Problem: How many non-negative integer solutions (x, y, z) of x + y + z = 10 satisfy x <= 4? Run the code. python print(sum(1 for x in…
- Problem: How many pairs (x, y) with x <= y and x + y = 12 lie in 1..11? Run the code. python print(sum(1 for x in range(1, 12) for y in…
- Problem: How many inversions does the array [4, 3, 2, 1, 0] have? Run the code. python a = [4, 3, 2, 1, 0] print(sum(1 for i in…
- Problem: How many pairs (i < j) of [5, 6, 7, 9, 8] satisfy |ai - aj| = 1? Run the code. python a = [5, 6, 7, 9, 8] print(sum(1 for i in…