Data Structures & AlgorithmsModerateGATE CS · Product placement
Assertion (A): Removing duplicates from a sorted array can be done in place in O(n) time with two pointers. Reason (R): In a sorted array, equal elements are adjacent, so a value is new exactly when it differs from the last kept value.
- A. Both A and R are true, and R is the correct explanation of A
- B. A is false, but R is true
- C. Both A and R are true, but R is not the correct explanation of A
- D. A is true, but R is false
Correct answer: A. Both A and R are true, and R is the correct explanation of A
Because duplicates are contiguous, one read pointer and one write pointer suffice; R is the reason this works without a hash set.
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
- Assertion (A): Quick sort with the last element as pivot takes Θ(n²) time on an array that is already sorted. Reason (R): On sorted input…
- A Python 2-D list is created two ways and one cell is set. What is printed? python a = [[0] 3] 3 b = [[0] 3 for in range(3)] a[2][2] = 5…
- Rearrange a linked list so that nodes at odd POSITIONS (1st, 3rd, …) come first, followed by the even-position nodes, keeping relative…
- What is the length of the longest contiguous substring of ((()((()(()) that is a valid (balanced) parentheses string?
- Reverse a singly linked list in groups of k (a final group shorter than k is left as it is). What is printed? python def revk(h, k): cnt…
- Merge two sorted linked lists by relinking nodes (ties take the node from the first list). What is printed? python def merge(a, b): d = t…