Form largest number from digits

QUESTION

Given an array of digits form 0 to 9 of size n, the task is to rearrange elements of the array such that after combining all the elements of the array number formed is maximum.\n\nInput:\nThe first line of input contains an integer T denoting the number of test cases. Then T test cases follow. The first line of each test case contains an integer n denoting the number of elements in the array. Then in the next line are n space seperated integers denoting the elements of the array.\n\nOutput:\nFor each test case print a single line a number denoting the largest number that can be achieved by rearranging the elements of the array.\n\nConstraints:\n1<=T<=100\n1<=n<=18.

“TESTCASE_1”: “2\n5\n9 0 1 3 0\n3\n1 2 3\n###—###SEPERATOR—###—\n93100\n321”, “TESTCASE_2”: “3\n4\n5 7 9 1\n2\n1 7\n8\n4 5 1 2 6 3 2 1\n###—###SEPERATOR—###—\n9751\n71\n65432211”, “TESTCASE_3”: “5\n4\n5 7 9 1\n2\n1 7\n8\n4 5 1 2 6 3 2 1\n5\n9 0 1 3 0\n3\n1 2 3\n###—###SEPERATOR—###—\n9751\n71\n65432211\n93100\n321”, “TESTCASE_4”: “0\n###—###SEPERATOR—###—\n0”, “TESTCASE_5”: “0\n###—###SEPERATOR—###—\n0

ANSWER

import java.util.*;
import java.lang.*;
import java.io.*;

class TestClass {
	public static void main (String[] args) {
		//code
		Scanner in = new Scanner(System.in);
        int tcases = in.nextInt();
        for (int i = 0; i < tcases; i++) {
            int num = in.nextInt();
            int[] arr = new int[num]; 
            for (int j = 0; j < arr.length; j++) {
                arr[j] = in.nextInt();
            }
            Arrays.sort(arr);
            for (int j = arr.length-1; j >= 0; j--) {
                System.out.printf("%d",arr[j]);
            }System.out.println("");
        }
	}
}
Ads Blocker Image Powered by Code Help Pro

Ads Blocker Detected!!!

We have detected that you are using extensions to block ads. Please support us by disabling these ads blocker.