Data Structures & AlgorithmsModerateGATE CS · Codeforces Div2 B
What is the length of the longest run of consecutive integers (in any order) present in the array [9, 1, 4, 7, 3, -1, 0, 5, 8, -1, 6]?
- A. 6
- B. 8
- C. 7
- D. 10
Correct answer: C. 7
Put all values in a hash set and start counting only from values whose predecessor is absent, so each run is walked once. Duplicates such as the two -1 values must not inflate the count.
Practise more Arrays, Strings, Linked Lists, Stacks, Queues and Hashing questionsArrays, Strings, Linked Lists, Stacks, Queues and Hashing: notes and key points
More Arrays, Strings, Linked Lists, Stacks, Queues and Hashing questions
- How many index pairs i < j in the array [1, 5, 3, 4, 2, 7, 9] have absolute difference exactly 2?
- In the array [1, 5, 5, 5, 5, 5, 5, 5, 5, 9], how many distinct value pairs {x, y} with x <= y satisfy x + y = 10, where x and y both occur…
- What is the length of the longest substring of aabccbb that can be made into a string of one repeated letter by replacing at most 2…
- How many substrings of abcabc contain all of the three letters a, b and c at least once?
- How many substrings of aabacbebebe contain exactly two distinct characters?
- How many substrings of abcdabcabd are anagrams of the pattern abd (same letters with the same multiplicities)?