ProgrammingHardTCS NQT · Accenture pattern
What will be the output of the following pseudocode (counting inversions, brute force)? (Arrays are indexed from 0.)
What will be the output of the following pseudocode (counting inversions, brute force)? (Arrays are indexed from 0.)
Integer arr[5] = {2, 4, 1, 3, 5}
Integer cnt = 0
for (each i from 0 to 4)
for (each j from i + 1 to 4)
if (arr[i] > arr[j])
cnt = cnt + 1
end if
end for
end for
Print cnt- A. 2
- B. 4
- C. 3
- D. 5
Correct answer: C. 3
The out-of-order pairs (i<j but arr[i]>arr[j]) are (2,1), (4,1), (4,3) -- three inversions in total.
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 (counting pairs summing to a target, brute force)? (Arrays are indexed from 0.)…
- What will be the output of the following pseudocode (counting pattern occurrences, allowing overlaps)? Function countOcc(String text…
- What will be the output of the following pseudocode (manually parsing a signed integer string)? Function strToInt(String s) Integer sign =…
- What will be the output of the following pseudocode (counting trailing zeros in n factorial, via counting factors of 5)? (All variables…
- What will be the output of the following pseudocode (fast exponentiation)? (All variables are integers, so / is integer division.) Integer…
- What will be the output of the following pseudocode (checking if one string is a rotation of another)? Function isRotation(String a…