Array Permutation
The function accepts an integer array ‘arr’ of length ‘size’ as the argument. Implement the function to find and return the maximum number that can be formed by any permutation or arrangement of the digits obtained from all the numbers present in the array. You have to return the number formed as a string.
Note: You may need to rearrange the digits of the numbers to form the maximum number.
Example:
Input: 34 79 58 64
Output: 98765443
Explanation:
All digits from all the numbers of the array are 3,4,7,9,5,8,6,4. Maximum number obtained after rearranging all these digit gives 98765443.
Sample input:
21 90 23
Sample Output:
932210
Solution in Python 3:
#https://codewindow.in
#join our telegram channel
from itertools import permutations
s=input()
s=list(s)
a=[]
#print(s)
for i in s:
if i!=' ':
a.append(i)
l=len(a)
b = permutations(a,l)
y = [''.join(i) for i in b]
#print(y)
y=sorted(y)
ans=y[-1:]
print(''.join(ans))
# Telegram @codewindow
Solution in C++ :
//https://codewindow.in
//join our telegram channel @codewindow
#include <iostream>
#include <string.h>
#include <bits/stdc++.h>
using namespace std;
int main()
{
char str[1000];
cin.getline(str, 1000);
string newstr = "";
for (int i = 0; i < strlen(str); i++)
if(str[i] != ' ')
newstr += str[i];
sort(newstr.begin(), newstr.end());
reverse(newstr.begin(), newstr.end());
cout << newstr;
}
Solution in JAVA :
//https://codewindow.in
//join our telegram channel @codewindow
import java.util.*;
class CodeWindow {
public static void main(String[] args)
{
Scanner sc = new Scanner(System.in);
String str = sc.nextLine();
String newstr = "";
for (int i = 0; i < str.length(); i++)
if(str.charAt(i) != ' ')
newstr += str.charAt(i);
char [] arr = newstr.toCharArray();
Arrays.sort(arr);
for (int i = arr.length - 1; i >= 0; i--)
System.out.print(arr[i]);
}
}
Output:
21 90 23
932210
34 79 58 64
98765443