ProgrammingHardTCS NQT · Accenture
Array a = [5, 1, 4, 2, 3, 7]. The procedure below counts the interior local maxima, that is, positions i from 1 to n - 2 whose value is strictly greater than both neighbours. What is printed?
Array a = [5, 1, 4, 2, 3, 7]. The procedure below counts the interior local maxima, that is, positions i from 1 to n - 2 whose value is strictly greater than both neighbours. What is printed?
count = 0
for i = 1 to n - 2
if a[i] > a[i-1] and a[i] > a[i+1] then count = count + 1
end for
Print count- A. 3
- B. 1
- C. 2
- D. 6
Correct answer: B. 1
Only 4 is greater than both neighbours (1 and 2) among the interior positions, so the count is 1. Counting elements greater than the left neighbour gives 3, counting interior local minima gives 2, and the array length is 6.
Practise more Pseudocode and Code Tracing questionsPseudocode and Code Tracing: notes and key points
More Pseudocode and Code Tracing questions
- The KMP prefix function pi[i] is the length of the longest proper prefix of s[0..i] that is also a suffix of it. For s = "abacabab" the…
- Pattern "aba" is searched in the text "abababa" with overlapping matches allowed. The procedure below counts the starting positions where…
- For the string s = "forgeeksskeegfor", the procedure below expands around each centre to find the length of the longest palindromic…
- Count the palindromic substrings (substrings of length 1 or more that read the same forwards and backwards, counted by position) of the…
- Run-length encoding replaces each run of equal characters by the character followed by its run length. The procedure below encodes s =…
- A Caesar cipher shifts each lowercase letter forward by 3 places, wrapping from z back to a. The procedure below encodes the word "xyz"…