Connection Status:
Competition Arena > MicrowaveSelling
Member SRM 485 · 2010-03-12 · by gojira_tc · Simple Search, Iteration
Class Name: MicrowaveSelling
Return Type: int
Method Name: mostAttractivePrice
Arg Types: (int, int)
Problem Statement

Problem Statement

Manao lives in a country where the prices of all goods are positive integers. He is setting up a company that sells microwaves, and needs to decide the best price at which to sell them. Recently, he heard of a new psychological discovery: the more trailing 9's a price has, the more attractive it is to the customer. For example, 9099 has two trailing 9's, and 8909 has only one trailing 9, so the first price is more attractive.

Manao knows that he can only afford to sell microwaves at a price no less than minPrice, and believes that customers will only buy the microwaves if they cost no more than maxPrice. Help Manao by finding an integer between minPrice and maxPrice, inclusive, which has the maximum possible number of trailing 9's. If there are several candidates, return the largest one.

Constraints

  • minPrice will be between 1 and 1,000,000, inclusive.
  • maxPrice will be between minPrice and 1,000,000, inclusive.
Examples
0)
460
680
Returns: 599

Of all the prices between 460 and 680, 499 and 599 have the maximum number of trailing 9's. Since 599 is larger, it is Manao's price of choice.

1)
10
1000
Returns: 999

999 has three trailing 9's, and no other number in the given range has this property.

2)
1255
2999
Returns: 2999

Note that 2999 is still an acceptable price.

3)
20
25
Returns: 25

There are no numbers with trailing 9's between 20 and 25.

4)
1
1000000
Returns: 999999

Submissions are judged against all 121 archived test cases, of which 5 are shown here. Case numbers match the judge’s.

Coding Area

Language: C++17 · define a public class MicrowaveSelling with a public method int mostAttractivePrice(int minPrice, int maxPrice) · 121 test cases · 2 s / 256 MB per case

Submitting as anonymous