Loading...
Loading...
Riya runs a small online store. She has n products in her catalogue and wants to list every order a customer could place. An order is any group of one or more different products.
Write the product names of each order in alphabetical order, separated by spaces. Print the orders in dictionary order: compare the first names, then the second names, and so on. An order that is the start of a longer order comes first.
Example 1
Input
3 laptop cable webcam
Output
cable cable laptop cable laptop webcam cable webcam laptop laptop webcam webcam
Sorted, the products are cable, laptop, webcam. Every order starting with cable comes first, and cable alone comes before cable laptop.
Example 2
Input
1 bag
Output
bag
One product gives exactly one order.
Try the problem first. Open one hint at a time.
Sort the names first. Then you only ever add a product that comes after the last product you picked.
Keep a list of the products picked so far. Each time you add a product, the list is a new order: print it.
After adding product i, go on to pick more products from i + 1 onwards. When that returns, remove product i and try the next one.
Write your Java code, run it on the examples and submit it to hidden tests.
Solve in the editor