Data Structures & AlgorithmsVery hard
Sort 7 8 3 12 5 6 15 4 by the NUMBER OF 1-BITS in binary, ascending; values with the same bit count are ordered by value ascending. Give the result.
- A. 3 4 5 6 7 8 12 15
- B. 15 7 3 5 6 12 4 8
- C. 4 8 3 5 6 12 7 15
- D. 8 4 12 6 5 3 7 15
Correct answer: C. 4 8 3 5 6 12 7 15
Use the key (popcount, value): popcount orders the groups and the value breaks ties within each group. Sorting by value alone, descending popcount, or reversing the tie-break gives other sequences.
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
- A queue holds 1 2 3 4 5 6 7 8 from front to back. Interleave it: take the front element of the first half, then the front of the second…
- Starting from an empty stack, perform in order: push 4, push 9, pop, push 2, push 7, pop, pop, push 5. Reading from the bottom to the top…
- Take the values of 4 9 2 7 3 8 5 6 1 10 at the ODD 0-based indices (1, 3, 5, …) in order, forming a new sequence. What is the length of…
- Split 8 3 5 1 9 2 7 4 into consecutive blocks of 4 (positions 0–3 and 4–7). For each block take (max − min), and add these two numbers…
- In 3 1 4 1 5 9 2 6 5 3 5 8, count how many positions hold a NEW strict prefix maximum (the element is strictly greater than every element…
- How many DISTINCT strings are produced by all cyclic rotations of "abcabc" (including the identity rotation)?