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.

  • Iterate through the array and swap elements to make the array...read more

Help your peers!
Select
Add answer anonymously...

APT Research Private Limited Data Analytics interview questions & answers

A Data Analytics was asked Q. K-th Largest Number in a BST Given a binary search tree (BST) consisting of inte...read more
A Data Analytics was asked Q. Longest Common Prefix Problem Statement You are given an array ‘ARR’ consisting ...read more
A Data Analytics was asked Q. Rotting Oranges Problem Statement You are given a grid containing oranges where ...read more

Popular interview questions of Data Analytics

A Data Analytics was asked Q1. K-th Largest Number in a BST Given a binary search tree (BST) consisting of inte...read more
A Data Analytics was asked Q2. Longest Common Prefix Problem Statement You are given an array ‘ARR’ consisting ...read more
A Data Analytics was asked Q3. Rotting Oranges Problem Statement You are given a grid containing oranges where ...read more
APT Research Private Limited Data Analytics 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