Asked inWalmart,SDE-2

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.

Be the first one to answer
Add answer anonymously...
Walmart SDE-2 Interview Questions
Stay ahead in your career. Get AmbitionBox app
qr-code
Helping over 1 Crore job seekers every month in choosing their right fit company
65 L+

Reviews

4 L+

Interviews

4 Cr+

Salaries

1 Cr+

Users/Month

Contribute to help millions

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

Follow us
  • Youtube
  • Instagram
  • LinkedIn
  • Facebook
  • Twitter