Heap Structure
Time limit3sMemory limit1024 MB
Given a min-heap of n nodes, count how many nodes can hold the k-th smallest element.
- Level
Medium6 of 10
- Topics
- Tree, Binary search, Math
- Solved
- No attempts yet
Problem
A minimum binary heap, often called a min-heap, is a binary tree-based data structure widely used in computer science. It has two main properties:
- Heap property: For any node, its value is less than or equal to the values of its children. So the minimum value in the heap is always at the root. The smallest element is always at the top.
- Binary tree structure: A min-heap is usually a complete binary tree. Every level is full except possibly the last, and the last level is filled from left to right. This structure lets min-heaps be implemented efficiently with arrays.
Min-heaps are often used to implement priority queues, which maintain a collection of elements with priorities. Because the minimum is kept at the root, a min-heap can retrieve and remove the element with the highest priority (lowest value) in logarithmic time. Removing any other element may take linear time.
Hank recently learned about min-heaps. He wonders how many nodes can hold the k-th smallest element in a min-heap of n nodes. Write a program to help him.
Input
The input contains two space-separated positive integers and .
Output
Output the number of nodes that can hold the -th smallest element in a min-heap of nodes.
Constraints
Hint
Assume that all elements are distinct.