Maximum Number With Single Swap

You are given an array of N elements that represent the digits of a number. You can perform one swap operation to exchange the values at any two indices. Your task is to determine the maximum possible number that can be achieved by performing at most one swap.

Example:

Input:
array = [1,3,2,7]
Output:
[7,3,2,1]
Explanation:

The input array represents the number 1327. After exploring possible swaps, swapping indices 1 and 4 yields the maximum number 7321. Thus, the output array is [7, 3, 2, 1].

Constraints:

  • 1 <= T <= 100
  • 2 <= N <= 104
  • 0 <= A[i] <= 9
  • The array can include leading zeros.
Note:

For each test case, the output should be printed where elements are space-separated, on a new line for each test case.

AnswerBot
4mo

Given an array of digits, swap two elements to get the maximum number possible.

  • Iterate through the array to find the maximum digit.

  • Swap the maximum digit with the leftmost occurrence of that digit.

  • If ...read more

Help your peers!
Select
Add answer anonymously...

LrnEd Associate Professional interview questions & answers

An Associate Professional was asked Q. Which programming language, Python or Java, do you believe will be more powerful...read more
An Associate Professional was asked Q. Maximum Number With Single Swap You are given an array of N elements that repres...read more
An Associate Professional was asked Q. Reverse the String Problem Statement You are given a string STR which contains a...read more

Popular interview questions of Associate Professional

An Associate Professional was asked Q1. Which programming language, Python or Java, do you believe will be more powerful...read more
An Associate Professional was asked Q2. Maximum Number With Single Swap You are given an array of N elements that repres...read more
An Associate Professional was asked Q3. Reverse the String Problem Statement You are given a string STR which contains a...read more
LrnEd Associate Professional 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