Data Structures & AlgorithmsVery hardGATE CS · ICPC regional
How many index pairs i < j in the array [1, 3, 5, 7, 2, 6, 8] have a[i] + a[j] equal to a power of 2?
- A. 5
- B. 3
- C. 4
- D. 6
Correct answer: C. 4
A sum is a power of two exactly when x & (x-1) is zero, and small enough values can be checked pairwise or by hashing complements to each power.
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
- The string (()(()(( is scanned left to right: '(' opens and ')' closes the most recent unmatched '('. How many characters are unmatched in…
- What is the minimum number of brackets that must be inserted into ())(()(( to make it a balanced sequence?
- What is the length of the longest valid (well-formed) parentheses substring of )()())?
- Starting from an empty deque, the operations are: push front 1, push back 2, push front 3, pop back, push back 4. Let the deque contents…
- A robot starts at position 0 on a number line. For each move it steps right (R) or left (L). Count the number of distinct positions…
- A text cursor starts after the text abc (cursor at the end). Operations in order: type x, Left, Left, Backspace, Right, type y. Here Left…