ProgrammingHardInfosys · Cognizant
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 negation (ties: first occurrence). What is printed as the final sum?
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 negation (ties: first occurrence). What is printed as the final sum?
repeat k times
find the first index of the smallest element; negate it
end repeat
Print sum of a- A. 13
- B. 15
- C. 5
- D. 9
Correct answer: B. 15
Negating -2 gives 2, then the smallest element is -1, which becomes 1, so the sum is 4+2+5+1+3 = 15. Applying only one negation gives 13, negating the first k elements gives 5, and skipping the negations gives 9.
Practise more Pseudocode and Code Tracing questionsPseudocode and Code Tracing: notes and key points
More Pseudocode and Code Tracing questions
- 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…
- Compute 3^13 mod 7 with the binary method below (a = 3, e = 13, m = 7). What is printed? result = 1; base = a mod m; e = 13 while e > 0 if…