Skip to main content
All Questions

Find the minimum halving operations to make an array alternate in parity.

Unlock detailed company stats for this questionUpgrade

Given an array of positive integers, find the minimum number of operations required to make the array alternate in parity, following either Odd, Even, Odd, Even, ... or Even, Odd, Even, Odd, .... In one operation you can replace any element nums[i] = floor(nums[i] / 2), repeatably on the same element. Example: [6, 12, 5, 10] - the Even-Odd-Even-Odd pattern costs 3 operations; Odd-Even-Odd-Even costs 1, so the answer is 1. [3, 12, 5, 10] is already alternating, so the answer is 0. Constraints: 1 <= nums.size() < 10^5, nums[i] > 0.

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.