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

30th Jun 2018, 12:16 PM
Adam Dinesh
Adam Dinesh - avatar
5 odpowiedzi
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); } }
30th Jun 2018, 1:24 PM
Adam Dinesh
Adam Dinesh - avatar
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
30th Jun 2018, 1:41 PM
KrOW
KrOW - avatar
0
how you got like 6867675
30th Jun 2018, 1:46 PM
Adam Dinesh
Adam Dinesh - avatar
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)
30th Jun 2018, 2:17 PM
KrOW
KrOW - avatar