All Tracks Math Problem

The Close-i-ness
Tag(s):

Easy

Problem
Editorial
Analytics

Abhisek likes to play with numbers. One day, he was solving math puzzles and he came across an interesting problem.

A sorted list of N numbers are given and a number Num. We have to find the five numbers that are closest (numerically) to Num. A number x in the array is closest to Num if |Num-­x| is the smallest among all such x possible.

Note: If the Num is present in given list, then it should be in the output.

Constraints:

5 < N <20

­1 <Num< 10000

Input:

First input line consists of 2 integers N followed by Num

Second input line consists of N integers m1 m2 m3 .. mN denoting the numbers given in the list.

where mi's are N numbers in sorted order.

Output:

p1 p2 p3 p4 p5

where pi's are closest numbers in increasing order of absolute difference from Num. If there are two numbers with same difference, print the larger one first.

SAMPLE INPUT
10 6
1 2 3 4 5 6 7 8 9 10
SAMPLE OUTPUT
6 7 5 8 4
Time Limit: 1.0 sec(s) for each input file.
Memory Limit: 256 MB
Source Limit: 1024 KB
Marking Scheme: Marks are awarded when all the testcases pass.
Allowed Languages: C, C++, C++14, Clojure, C#, D, Erlang, F#, Go, Groovy, Haskell, Java, Java 8, JavaScript(Rhino), JavaScript(Node.js), Julia, Kotlin, Lisp, Lisp (SBCL), Lua, Objective-C, OCaml, Octave, Pascal, Perl, PHP, Python, Python 3, R(RScript), Racket, Ruby, Rust, Scala, Swift, Visual Basic

CODE EDITOR

Initializing Code Editor...
Your Rating:

Contributor

Notifications
View All Notifications