Minimum Halvings for Alternating Parity
Updated
Problem statement
You are given an array nums of positive integers. In one operation, choose one index and replace its value with floor(nums[i] / 2). You may operate on the same index more than once.
Make the final array alternate in parity. Either odd indices may hold even values and even indices odd values, or the opposite pattern may be used.
Return the minimum total number of operations.
Function
minHalvingsForAlternatingParity(nums: int[]) → intExamples
Example 1
nums = [6,12,5,10]return = 1Use the pattern odd, even, odd, even. Halve 6 once to obtain 3; the other three values already match the pattern.
Example 2
nums = [3,12,5,10]return = 0The array already follows the pattern odd, even, odd, even.
Example 3
nums = [8,7,4,3]return = 0The array already follows the pattern even, odd, even, odd.
Constraints
1 <= nums.length < 10^51 <= nums[i] <= 10^9
More IBM Online Assessment questions
- Count Strictly Increasing Contiguous WindowsOA · Seen Nov 2026
- Total Each Letter's Count in an Encoded StringOA · Seen Nov 2026
- Count Servers With No Requests in a Time WindowOA · Seen Nov 2026
- Maximum Easy Tasks After RearrangingOA · Seen Nov 2026
- Tree Pythagorean TriplesOA · Seen Oct 2026
- Minimum Power-of-Two Removal OperationsOA · Seen Oct 2026
- Maximum Compatible Time WindowsOA · Seen Oct 2026
- Last Object Exit TimeOA · Seen Oct 2026
- Query Type Frequency WindowOA · Seen Oct 2026
- Get Triplet CountOA · Seen Oct 2026
Similar questions at other companies
- Minimum Rounds to Complete All TasksInfosys · OAInfosys interview questions
- Count Subarrays with a Perfect-Square ProductMicrosoft · OAMicrosoft interview questions
- Repeated Mirror XORMathWorks · OAMathWorks interview questions
- Minimize Adjacent Squared-Difference Cost with One InsertionMcKinsey & Company · OAMcKinsey & Company interview questions
- Maximize Revenue from Decreasing InventoryChubb · OAChubb interview questions
- Minimum Values with Even Powers of TwoVisa · OAVisa interview questions
- Resource AllocationMaven Securities · OAMaven Securities interview questions
- Rock JumpingIMC Trading · OAIMC Trading interview questions
- Minimum Total Batch ExpenseAmazon · OA
- Server SelectionPoint72 · OAPoint72 interview questions