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!
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
Interview Questions Asked to Machine Learning Engineer at Other Companies
Top Skill-Based Questions for JPMorgan Chase & Co. Machine Learning Engineer
Machine Learning Interview Questions and Answers
250 Questions
Algorithms Interview Questions and Answers
250 Questions
Python Interview Questions and Answers
200 Questions
Stay ahead in your career. Get AmbitionBox app


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
AmbitionBox Awards
Get AmbitionBox app

