ProgrammingHardAccenture · Cognizant
Array a = [2, 5, 3, 5]. For every pair of positions i < j, count the pairs where a[i] is at most a[j] (a[i] <= a[j]). What is the printed count?
Array a = [2, 5, 3, 5]. For every pair of positions i < j, count the pairs where a[i] is at most a[j] (a[i] <= a[j]). What is the printed count?
count = 0
for i = 0 to n - 2
for j = i + 1 to n - 1
if a[i] <= a[j] then count = count + 1
end for
end for
Print count- A. 4
- B. 2
- C. 6
- D. 5
Correct answer: D. 5
The pairs with a[i] <= a[j] are (2,5), (2,3), (2,5), (5,5) and (3,5), which is 5. Forgetting the equal pair gives 4, reversing the comparison gives 2, and counting every pair gives 6.
Practise more Pseudocode and Code Tracing questionsPseudocode and Code Tracing: notes and key points
More Pseudocode and Code Tracing questions
- Array a = [4, -2, 5, -1, 3]. Exactly k = 2 negations are applied: each one chooses the currently smallest element and replaces it by its…
- Tasks (duration, deadline): (3,5), (2,4), (4,9), (1,3). Earliest deadline first is applied: tasks are sorted by deadline and run back to…
- Two positive integers are a = 252 and b = 105. The procedure below finds their gcd by repeated subtraction. How many times does the loop…
- Let N = 360. The procedure below counts the divisors of N by testing every i from 1 to N. What is printed? count = 0 for i = 1 to N if N…
- A number's digit sum is computed by the procedure below for N = 98765. What is printed? s = 0; m = N while m > 0 s = s + (m mod 10) m = m…
- The procedure below uses the sieve of Eratosthenes on the range 2 to 50, marking composites starting from ii. The inner marking statement…