Data Structures & AlgorithmsModerateFlipkart/Uber OA · placement
Count pairs with a given difference k (k > 0) among DISTINCT values using a hash set. What is printed?
Count pairs with a given difference k (k > 0) among DISTINCT values using a hash set. What is printed?
a, k = [8, 7, 7, 10, 5, 11, 7, 6, 12, 9], 2 s = set(a) print(sum(1 for x in s if x + k in s), len(s))
- A. 5 8
- B. 8 8
- C. 6 10
- D. 6 8
Correct answer: D. 6 8
Iterating over the SET counts each distinct pair (x, x + k) once; iterating over the list counts duplicates repeatedly. Output: 6 8.
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
- Why is a tombstone (a 'deleted' marker) used instead of emptying a slot when deleting from an open-addressing hash table?
- Which statement about using a hash set versus sorting to detect duplicates in an array of n integers is correct?
- Can a string become a palindrome by deleting at most one character? Two pointers try skipping either side on the first mismatch. What is…
- How many contiguous subarrays of [4, 3, 1, 7, 5, 7, 4, 3, 2, 1] contain exactly 2 odd numbers? (Use exactly(k) = atMost(k) − atMost(k − 1).)
- Find the index of the first non-repeating character using a frequency table (−1 if none). What is printed? python from collections import…
- An equilibrium index i has (sum of elements left of i) = (sum of elements right of i), excluding a[i]. Which indices of [−4, 5, 8, −4, −4…