0
Maximum Number in Java
Given an array of numbers, arrange them in a way that yields the largest value. For example, if the given numbers are {54, 546, 548, 60}, the arrangement 6054854654 gives the largest value. Input: First line contains an integer N , Next line contains N integers separated by space. Output: Print the maximum number that can be obtained by using given numbers. Constraints: 1<=N<=1000 1<=Number<=1000000
5 Réponses
0
class MaxNumber
{
public void printMaxNumbers(int[] nums)
{
int maxOne = 0;
int maxTwo = 0;
for (int n : nums)
{
if (maxOne < n)
{
maxTwo = maxOne;
maxOne = n;
}
else if (maxTwo < n)
{
maxTwo = n;
}
}
System.out.println("Print Maximum Number : " + maxOne);
}
public static void main(String args[])
{
int num[] = {54,546,548,60};
MaxNumber ttmn = new MaxNumber();
ttmn.printMaxNumbers(num);
}
}
0
If i have understanded the problem, i dont think that its so simple... Try to figure out what is the output if input is {67, 675, 68}... I woud be 6867675
0
how you got like 6867675
0
You have 6 combinations:
6767568
6768675
6756768
6756867
6867675
6867567
That with greatest value is 6867675
(if you see it like an base10 integer)