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.
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
Top LrnEd Associate Professional interview questions & answers
Popular interview questions of Associate Professional
Reviews
Interviews
Salaries
Users/Month