Question

In: Computer Science

Rattapoom is a boy that loves to play with numbers. He received digit cards as a...

Rattapoom is a boy that loves to play with numbers. He received digit cards as a present on his birthday, where he joyously showed it to his friends in the neighborhood. He received N cards of number, where each card contains a single digit. He wishes to arrange all his cards in such a way that the digits form a smallest possible N-digit number, and that N-digit number must not begin with 0.

Given N digit cards that Rattapoom received, arrange the digits to form an N-digit number that is the smallest possible and does not start with 0.

INPUT

The first line defines an integer N (2 ≤ N ≤ 1,000). The next line defines a sequence of N digits, separated by whitespace, representing each card of digit. Assume that there will be at least one digit that is not zero.

OUTPUT

A line holding an N-digit number that does not start with 0. All digits are displayed without any whitespaces in between them.

Sample Input

Sample Output

4

9 4 6 2

2469

6

3 0 8 1 3 3

103338

Write a Java program using methods to implement this case.

Solutions

Expert Solution

The N digits should be stored in an array of size N. After storing the digits in the array, function named minimum is called. Inside the function minimum the array is sorted in ascending order, after that the elements of the array are printed from first element to last element, if first element is 0 (zero) then second element is printed first and 0 is printed as second digit.

code:

import java.util.*;
 
public class Minimum_number
{
        public static void main(String[] args) 
        {               
                Scanner in = new Scanner(System.in);
                int N = in.nextInt();
                int[] arr = new int[N];
//entering the digits in an array
                for(int i=0;i<N;i++)
                arr[i]=in.nextInt();
//calling minimum method by reference 
                minimum(arr,N);
        } // main method closed
static void minimum(int array[], int n)
{
//using bubble sort to sort the array.
for(int i=0;i<n-1;i++)
for(int j=0;j<n-i-1;j++)
{
//swapping consecutive elements if Nth element is greater than (N+1)th element
if(array[j]>array[j+1])
{
int t=array[j];
array[j]=array[j+1];
array[j+1]=t;
}
}
System.out.println();
//if first element is zero then print second element first and 0 as 2nd element
if(array[0]==0)
{
System.out.print(array[1]); 
System.out.print(array[0]);
for(int i=2;i<n;i++)
System.out.print(array[i]);
}
else
{
// print the elements of array
for(int i=0;i<n;i++)
System.out.print(array[i]); 
}
System.out.println();
}// minimum method closed
} // class closed

Output:

Please hit thumbs up if you like the answer.


Related Solutions

Banks issue credit cards with 16 digit numbers. If you've never thought about it before you...
Banks issue credit cards with 16 digit numbers. If you've never thought about it before you may not realize it, but there are specific rules for what those numbers can be. For example, the first few digits of the number tell you what kind of card it is - all Visa cards start with 4, MasterCard numbers start with 51 through 55, American Express starts with 34 or 37, etc. Automated systems can use this number to tell which company...
Question 6: Winston, a dog, loves to play fetch. He catches each ball mid-air independently with...
Question 6: Winston, a dog, loves to play fetch. He catches each ball mid-air independently with probability 0.4. Write a simulation in R in which you throw Winston five balls and compute how many of the five balls he catches mid-air. Repeat this simulation N=5,000 times. a) Plot a histogram of the number of catches Winston has made in each series of five catches. b) What proportion of the time does Winston catch exactly two balls mid-air? c) What proportion...
a) How many 3-digit numbers are there? b) How many 3-digit numbers can you make with...
a) How many 3-digit numbers are there? b) How many 3-digit numbers can you make with all three digits different? c) How many of the numbers is part b) are odd?
How many​ 7-digit telephone numbers are possible if the first digit cannot be eight and ​(a)...
How many​ 7-digit telephone numbers are possible if the first digit cannot be eight and ​(a) only even digits may be​ used? ​(b) the number must be a multiple of 10​ (that is, it must end in​ 0)? ​(c) the number must be a multiple of 1,000? ​(d) the first 2 digits are 92? ​(e) no repetitions are​ allowed?
In this game, you will play in pairs with a single deck of cards (face cards...
In this game, you will play in pairs with a single deck of cards (face cards removed). Aces count as ones and all numbered cards count at face value. Players take turns flipping over two cards and finding the product. Regardless of who flipped the cards, Player 1 always gets one point if the product is even, and Player 2 always gets one point if the product is odd. Continue playing until one player reaches 20 points. Would you rather...
In a lottery, each ticket has 5 one-digit numbers 0-9 on it. (with no digit repeating...
In a lottery, each ticket has 5 one-digit numbers 0-9 on it. (with no digit repeating twice) You win only if your ticket has the digits in the required order. What are your chances of winning?
Question1: How many of the 5-digit numbers that can be written with the numbers 1, 2,...
Question1: How many of the 5-digit numbers that can be written with the numbers 1, 2, 3, 4 contain both numbers 1 and 2? question2: 3 married couples shown as a1a2, b1b2, c1c2 want to sit in a row. How many different ways can these married couples sit in a row, without a husband and a wife from the same couple coming together?
George loves to cook and is deciding in regards to whether he should open a pizza...
George loves to cook and is deciding in regards to whether he should open a pizza place or a sandwich shop however he would like your help to understand the break-even for each type of restaurant and the markup percentage for each of the main products he will sell at each store. If George opens a pizza shop he will have to buy a new pizza oven which will cost him $40,000. He will also have to buy different kitchen...
4. The check digit for ISBNs is one of the numbers 0, 1, 2, . ....
4. The check digit for ISBNs is one of the numbers 0, 1, 2, . . . , 9, or the letter X. One of the your fellow students comments ”Gee, it sure is a pain to have to use that X all time. Why don’t they just compute the check digit sum modulo 10 instead of modulo 11, so that we can get rid of the X?” Would this plan work? Prove your answer. Assume that all ISBNs are...
Fill in the blanks in the problem with the 1 or 2 digit numbers from above:...
Fill in the blanks in the problem with the 1 or 2 digit numbers from above: A mass of ______ kg is attached to a spring, and stretches it by _____ cm. The mass is then pulled to a distance of ______ ?? below the spring’s equilibrium length, and given a downward velocity of ________ ?/? . Note: you may assume normal Earth gravity (g=9.8 m/s2 ) and that the spring is undamped. 1) What is the spring constant? 2)a)...
ADVERTISEMENT
ADVERTISEMENT
ADVERTISEMENT