Maximum Number by One Swap

You are provided with an array of N integers representing the digits of a number. You are allowed to perform an operation where you can swap the values at two different indices to form a maximum possible number. The operation can be used at most once.

Example:

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

From the array, the number represented is 1327. By swapping elements, the largest number 7321 is obtained by swapping the first and last digits.

Constraints:

  • 1 <= T <= 100
  • 2 <= N <= 104
  • 0 <= A[i] <= 9
  • Where 'A[i]' refers to the 'ith' element of the array.
  • Time limit: 1 second

Note:

Arrays might have leading zeros, such as [0, 3, 5, 7], which represents the number 357.

AnswerBot
4mo

Given an array of integers representing digits of a number, swap two values to form the maximum possible number.

  • Iterate through the array to find the maximum digit.

  • Swap the maximum digit with the firs...read more

Neha Patel
6mo
def find_maximum_number(arr): n = len(arr) # Convert the array to a string for easy manipulation str_arr = list(map(str, arr)) # Find the last occurrence of each digit last_occurrence = {int(x): i for...read more
Help your peers!
Select
Add answer anonymously...

Top Machine Learning Engineer Interview Questions Asked at JPMorgan Chase & Co.

Q. Subset Sum Equal To K Problem Statement Given an array/list of positive integers...read more
Q. Paths in a Matrix Problem Statement Given an 'M x N' matrix, print all the possi...read more
Q. Find Permutation Problem Statement Given an integer N, determine an array of siz...read more
Machine Learning Engineer 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