Lexicographically Smallest Array Problem Statement

You are given an array ARR of 'N' integers and a positive integer 'K'.

Your task is to determine the lexicographically smallest array that can be obtained by performing at most 'K' swaps of consecutive elements.

Example:

Input:
ARR = [70, 60, 90, 21, 11], K = 3
Output:
[21, 70, 60, 90, 11]
Explanation:

You perform following swaps to achieve this:
- Swap 1: Swap 90 and 21 to get [70, 60, 21, 90, 11].
- Swap 2: Swap 60 and 21 to get [70, 21, 60, 90, 11].
- Swap 3: Swap 70 and 21 to get [21, 70, 60, 90, 11].
The resultant array is the lexicographically smallest one after at most 3 swaps.

Input:

The first line of input contains an integer 'T' representing the number of test cases. For each test case:
- The first line contains two integers 'N' and 'K'.
- The second line contains 'N' space-separated integers that represent the array.

Output:

Output the lexicographically smallest array possible after at most 'K' swaps for each test case.

Constraints:

  • 1 <= T <= 100
  • 1 <= N <= 5000
  • 1 <= K <= 10^9
  • 0 <= ARR[i] <= 10^5
Note:

Focus on implementing the function; input and output handling is managed for you.

AnswerBot
4mo

The task is to determine the lexicographically smallest array that can be obtained by performing at most 'K' swaps of consecutive elements.

  • Sort the array in non-decreasing order and keep track of the ...read more

Help your peers!
Select
Add answer anonymously...

Paytm Software Developer interview questions & answers

A Software Developer was asked 4mo agoQ. Given a sorted array of integers nums and an integer target, write a function to...read more
A Software Developer was asked 6mo agoQ. What is multithreading?
A Software Developer was asked 7mo agoQ. Given a 2D grid map of '1's (land) and '0's (water), count the number of islands...read more

Popular interview questions of Software Developer

A Software Developer was asked 4mo agoQ1. Given a sorted array of integers nums and an integer target, write a function to...read more
A Software Developer was asked 6mo agoQ2. What is multithreading?
A Software Developer was asked 7mo agoQ3. Given a 2D grid map of '1's (land) and '0's (water), count the number of islands...read more
Paytm Software Developer Interview Questions
Stay ahead in your career. Get AmbitionBox app
play-icon
play-icon
qr-code
Trusted by over 1.5 Crore job seekers to find their right fit company
80 L+

Reviews

10L+

Interviews

4 Cr+

Salaries

1.5 Cr+

Users

Contribute to help millions

Made with ❤️ in India. Trademarks belong to their respective owners. All rights reserved © 2025 Info Edge (India) Ltd.

Follow Us
  • Youtube
  • Instagram
  • LinkedIn
  • Facebook
  • Twitter
Profile Image
Hello, Guest
AmbitionBox Employee Choice Awards 2025
Winners announced!
awards-icon
Contribute to help millions!
Write a review
Write a review
Share interview
Share interview
Contribute salary
Contribute salary
Add office photos
Add office photos
Add office benefits
Add office benefits