FastPrepMinimum Halvings for Alternating Parity
Problems/Minimum Halvings for Alternating Parity

Minimum Halvings for Alternating Parity

IBM logoIBM● MediumNEW GRADOA

Updated Nov 2026

QuestionFunctionExamplesConstraints

Problem statement

Source note: Reported in an IBM new grad online assessment. The report's walkthrough has one wrong parity label after halving 5 to 2. The stated operation, the target patterns and the final answer show what was meant, so we follow that. Our version is about 97% the same as the reported question.

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[]) → int

Examples

Example 1

nums = [6,12,5,10]return = 1

Use 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 = 0

The array already follows the pattern odd, even, odd, even.

Example 3

nums = [8,7,4,3]return = 0

The array already follows the pattern even, odd, even, odd.

Constraints

  • 1 <= nums.length < 10^5
  • 1 <= 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

IBM interview experiences

  • IBM Software Intern Résumé Rounds: 2 Experiences (Part 3 of 3)
  • IBM Intern Blitz Interviews: 23 Experiences (Part 2 of 3)
  • IBM Software Intern Interviews: 44 Experiences (Part 1 of 3)
  • IBM Software Intern Interview Questions: 15 Experiences
All IBM interview questionsSee IBM hiring insights
Saved
class Solution {
    public int minHalvingsForAlternatingParity(int[] nums) {
        // write your code here
    }
}
Checking account…