Minimum Swaps to Group Values at Most K
Updated
Problem statement
You are given an integer array nums and an integer k. A value is qualifying when it is at most k.
In one operation, you may swap values at any two positions. Return the minimum swaps required to place all qualifying values in one contiguous block. If there are zero or one qualifying values, return 0.
Function
minSwapsToGroup(nums: int[], k: int) → intExamples
Example 1
nums = [2,1,5,6,3]k = 3return = 1There are three qualifying values. The window [2, 1, 5] contains one nonqualifying value, so one arbitrary-position swap is sufficient.
Example 2
nums = [1,2,3]k = 3return = 0All values qualify and already form one contiguous block.
Constraints
0 <= nums.length <= 10^5.-10^9 <= nums[i], k <= 10^9.
More Myntra interview questions
Similar questions at other companies
- Maximum Earnings Across WarehousesAmazon · ONSITE INTERVIEW
- Shortest Window with Required CountsOracle · ONSITE INTERVIEW
- Count Zero Request ServersSalesforce · ONSITE INTERVIEW
- Longest Subarray Within an Absolute-Difference LimitGoogle · ONSITE INTERVIEW
- Longest Binary Subarray With at Most K OnesLinkedIn · ONSITE INTERVIEW
- Max Consecutive Ones IIIMicrosoft · ONSITE INTERVIEW
- Longest Continuous Subarray Within an Absolute-Difference LimitUber · ONSITE INTERVIEW
- Sliding-Window Rate LimiterSnowflake · ONSITE INTERVIEW
- Count Equal Code Pairs Within Distance KInfosys · ONSITE INTERVIEW
- Maximize Element FrequencyGoldman Sachs · ONSITE INTERVIEW