FastPrepMinimum Swaps to Group Values at Most K
Problems/Minimum Swaps to Group Values at Most K

Minimum Swaps to Group Values at Most K

Myntra logoMyntra● MediumINTERNONSITE INTERVIEW

Updated Nov 2026

QuestionFunctionExamplesConstraints

Problem statement

Source note: Reported in a technical round of a Myntra intern onsite interview. The task is to find the minimum swaps needed to bring all values at most K together. The report points to sliding-window reasoning. We chose that a swap can use any two positions. We also chose the limits and the function interface.

Our version is about 96% the same as the reported question.

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

Examples

Example 1

nums = [2,1,5,6,3]k = 3return = 1

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

All 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

  • Detect a Server RAM Capacity BreachONSITE INTERVIEW · Seen May 2026
  • Minimum Route Reversals to Warehouse ZeroONSITE INTERVIEW · Seen May 2026
  • Smallest Binary-Digit MultipleONSITE INTERVIEW · Seen May 2026
  • Maximum Capped Histogram Area After One RemovalOA · Seen May 2026

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
See Myntra hiring insights
Saved
class Solution {
    public int minSwapsToGroup(int[] nums, int k) {
        // Return the minimum number of arbitrary-position swaps.
    }
}
Checking account…