Robot Project
InterviewTime limit5sMemory limit256 MB
Given a target length and up to a million rod lengths, find two rods summing exactly to the target with the maximum length difference, or report impossibility.
- Level
Medium4 of 10
- Topics
- Two pointers, Sorting, Array
- Solved
- No attempts yet
Problem
Sang-geun and Sun-young are building a robot for a school assignment. While building it, they realize they need exactly two LEGO pieces to plug a hole in the robot.
The hole is centimeters wide, and the lengths of the two pieces placed into the hole must sum to exactly the width of the hole. If they do not match exactly, the robot breaks during the demonstration and the two of them receive an F. The hole must always be plugged with two pieces.
They have already measured the length of every LEGO piece in the physics lab precisely. Write a program that finds two pieces that plug the hole perfectly.
Input
The input consists of several test cases and is processed until end of file.
The first line of each test case contains the hole width (, is an integer) in centimeters.
The second line contains the number of LEGO pieces ().
Each of the next lines contains the length of one LEGO piece. is a positive integer given in nanometers, and no piece is longer than centimeters ( nanometers).
(One centimeter equals nanometers.)
Output
Print one line for each test case. If there are no two pieces that plug the hole perfectly, print danger. Otherwise print yes ℓ1 ℓ2, where .
If several choices of two pieces are possible, print the one for which is largest.