ProgrammingHardAmazon OA · TCS Digital
What will be the output of the following pseudocode?
What will be the output of the following pseudocode?
Integer a[5] = {2, 4, 1, 3, 5}, c = 0
for (each i from 0 to 3)
for (each j from i + 1 to 4)
if (a[i] > a[j]) c = c + 1
end for
end for
Integer b[5] = {5, 4, 3, 2, 1}, d = 0
for (each i from 0 to 3)
for (each j from i + 1 to 4)
if (b[i] > b[j]) d = d + 1
end for
end for
Print c, d- A. 4 5
- B. 2 10
- C. 3 9
- D. 3 10
Correct answer: D. 3 10
c counts inversions: (2, 1), (4, 1), (4, 3) → 3. A fully reversed array of 5 has every pair inverted: C(5, 2) = 10. Output 3 10.
Practise more Pseudocode and Code Tracing questionsPseudocode and Code Tracing: notes and key points
More Pseudocode and Code Tracing questions
- Activities are given as (start, finish): (1,4) (3,5) (0,6) (5,7) (3,9) (5,9) (6,10) (8,11) (8,12) (2,14) (12,16). The procedure below runs…
- Five jobs each take one time unit. Job (name, deadline, profit): a (2,100), b (1,19), c (2,27), d (1,25), e (3,15). A job earns its profit…
- Train (arrival, departure) times in HHMM form: arrivals 900, 940, 950, 1100, 1500, 1800 and departures 910, 1200, 1120, 1130, 1900, 2000…
- Coin denominations are 1, 4 and 5. The greedy method for amount 8 considers denominations from largest to smallest and takes each coin…
- Fractional knapsack with capacity 50. Items as (value, weight): (60,10), (100,20), (120,30). Items are sorted by value per unit weight…
- Gas stations on a circle: gas = [1,2,3,4,5], cost = [3,4,5,1,2]. Station i gives gas[i] and then costs cost[i] to leave for station i+1…