Make Layer Times Odd
Updated
Problem statement
A neural network has n layers. The time layer i takes to compute is computationalTime[i].
You may apply the following operation any number of times: choose an even integer c, and every layer whose time is exactly c has its time reduced to c / 2.
Return the minimum number of operations needed so that the time of every layer is odd.
Function
minOperations(computationalTime: int[]) → intExamples
Example 1
computationalTime = [2, 4, 8, 16]return = 4- Choose
c = 16: the layer with time16becomes8, giving[2, 4, 8, 8]. - Choose
c = 8: both layers with time8become4, giving[2, 4, 4, 4]. - Choose
c = 4: all three layers with time4become2, giving[2, 2, 2, 2]. - Choose
c = 2: all four layers become1, giving[1, 1, 1, 1]. - That is
4operations. Each of the values16,8,4and2has to be chosen at least once, so fewer is not possible.
Example 2
computationalTime = [40, 6, 40, 3, 20, 1]return = 4- Choose
c = 40: both layers with time40become20, giving[20, 6, 20, 3, 20, 1]. - Choose
c = 20: all three layers with time20become10. - Choose
c = 10: those three layers become5. - Choose
c = 6: that layer becomes3, giving[5, 3, 5, 3, 5, 1]. - The values
40,20,10and6each have to be chosen at least once, so the answer is4.
Example 3
computationalTime = [1, 7, 9]return = 0Every time is already odd, so no operation is needed and the answer is 0.
Constraints
1 <= n == computationalTime.length <= 1041 <= computationalTime[i] <= 109
More Patreon interview questions
- Three SumOA · Seen Apr 2025
- Sum of Subarray RegionsOA · Seen Oct 2024
- Binary Cardinality SortingOA · Seen Apr 2023
- Closest NumbersOA · Seen Apr 2023
- Maximum Books From BundlesOA · Seen Apr 2023
- Professors Teaching Outside Their DepartmentOA · Seen Apr 2023
- Botany QueryOA · Seen Apr 2023
- Math with Lego BlocksOA · Seen Apr 2023
- Sliding-Window API Rate LimiterPHONE SCREEN · ONSITE INTERVIEW · Seen Oct 2026
- Employee Importance from a DictionaryPHONE SCREEN · Seen Feb 2026
Similar questions at other companies
- Minimum Rounds to Complete All TasksInfosys · OAInfosys interview questions
- Maximum MEXAtlassian · OAAtlassian interview questions
- Count Valid Square PairsAuxoAI · OA
- Server InvestmentRippling · OARippling interview questions
- Basic Item DiscountsPalantir · OAPalantir interview questions
- Maximum Three-Server Difficulty ScoreVisa · OAVisa interview questions
- Distinct Products After Removing One ElementHSBC · OAHSBC interview questions
- Maximum Distinct Values After SwapsAkuna Capital · OAAkuna Capital interview questions
- Min OperationAmazon · OA
- Find Max Sum with Same First and Last DigitMicrosoft · OAMicrosoft interview questions