ProgrammingHardTCS NQT · Accenture pattern
What will be the output of the following pseudocode (counting distinct pairs whose difference is k)? (Arrays are indexed from 0.)
What will be the output of the following pseudocode (counting distinct pairs whose difference is k)? (Arrays are indexed from 0.)
Integer arr[5] = {1, 5, 3, 4, 2}
Integer k = 2
Set arrSet = toSet(arr)
Set pairs = empty set
for (each x in arr)
if (x + k in arrSet)
add(pairs, (min(x, x+k), max(x, x+k)))
end if
end for
Print size(pairs)- A. 2
- B. 4
- C. 3
- D. 5
Correct answer: C. 3
The pairs with a difference of 2 are (1,3), (2,4), (3,5); using a set of sorted pairs avoids counting each pair twice, giving 3 distinct 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 (2x2 matrix multiplication)? Integer A[2][2] = {{1, 2}, {3, 4}} Integer B[2][2] = {{5…
- What will be the output of the following pseudocode (binary string to decimal)? String bits = "110101" Integer val = 0 for (each b in…
- What will be the output of the following pseudocode (finding the 1-based position of the rightmost set bit)? (All variables are integers…
- What will be the output of the following pseudocode (checking if s3 is an interleaving of s1 and s2, via dynamic programming)? Function…
- 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…
- What will be the output of the following pseudocode (checking whether all characters in a string are unique)? Function…