ProgrammingHardTCS NQT · Accenture pattern
What will be the output of the following pseudocode (counting pairs (i,j) with i<=j<=n whose product is divisible by n)?
What will be the output of the following pseudocode (counting pairs (i,j) with i<=j<=n whose product is divisible by n)?
Integer n = 12, c = 0
for (each i from 1 to n)
for (each j from i to n)
if ((i * j) mod n == 0)
c = c + 1
end if
end for
end for
Print c- A. 18
- B. 21
- C. 24
- D. 15
Correct answer: B. 21
Checking every pair i<=j in 1..12 for whether i*j is a multiple of 12 (e.g. i=3,j=4 gives 12; i=6,j=any even j works since 6*even is divisible by 12 when the other factor supplies the remaining factor of 2, etc.) gives a total count of 21 qualifying pairs.
Practise more Pseudocode and Code Tracing questionsPseudocode and Code Tracing: notes and key points
More Pseudocode and Code Tracing questions
- What will be the output of the following pseudocode (checking whether all characters in a string are unique)? Function…
- What will be the output of the following pseudocode (Hamming distance: number of differing bits between two integers)? (^ is bitwise XOR…
- What will be the output of the following pseudocode (finding the minimum in a rotated sorted array via binary search)? (Arrays are indexed…
- What will be the output of the following pseudocode (moving all zeros to the end while preserving the relative order of non-zero elements…
- What will be the output of the following pseudocode (length of the longest substring without repeating characters, sliding window)?…
- What will be the output of the following pseudocode (number of bits needed to represent n in binary)? Function bitsNeeded(Integer n)…