Skip to main content
All Questions

Minimize altered quantities so a compressed array fits within I bytes.

Unlock detailed company stats for this questionUpgrade

Quantities at checkpoints are stored in an array of size n. Each unique quantity requires k bits, where k is the smallest integer >= log2(number of unique quantities), so total storage is n * k bits. To compress, choose a range x <= y: quantities greater than y become y, quantities less than x become x, quantities within [x, y] are unchanged. Compress so the data fits within I bytes while minimizing the number of altered quantities.

Input: first line n and I (available storage in bytes); second line n space-separated integers. Output: a single integer, the minimum number of quantities that must be altered.

Sample 1: '6 2' / '2 1 2 3 4 3' -> 0 (4 unique values, k = 2, 12 bits = 1.5 bytes, already within 2 bytes). Sample 2: '6 1' / '2 1 2 3 4 3' -> 2.

Constraints: 1 <= n <= 4 * 10^5, 1 <= I <= 10^8.

Format · Online assessment.

Community answers

No answers contributed by the community yet.

Related courses

Course

Software Engineering Interview Prep

Land your dream software engineering role at Google, Amazon, Microsoft, Meta, Apple, and other top companies. Learn from mock interviews, frameworks, and advice from senior candidates—practice data structures, algorithms, system design, people management, behavioral interviews, and more.

Course

System Design Interviews

Learn how to answer the latest system design questions across product, infrastructure, and AI domains. Features in-depth video examples, written breakdowns, and helpful reference patterns. Watch senior engineers and managers answer these questions in mock interview videos.