Something went wrong!

Some error occured while loading page for you. Please try again.

  • Practice
  • Compete
  • Jobs
  • Leaderboard
  1. Dashboard
  2. Algorithms
  3. Greedy
  4. Max Min

Max Min

by amititkgp
  • Problem
  • Submissions
  • Leaderboard
  • Discussions
  • Editorial
  • Topics

Given a list of integers, your task is to select integers from the list such that its unfairness is minimized.

if are numbers selected from the list , the unfairness is defined as

where max denotes the largest integer among the elements of , and min denotes the smallest integer among the elements of .

Note: Integers in the list may not be unique.

Input Format

Input Format
The first line contains an integer .
The second line contains an integer .
lines follow. Each line contains an integer that belongs to the list .

Constraints

Constraints


Output Format

Output Format
An integer that denotes the minimum possible value of unfairness.

Sample Input 0

7
3
10
100
300
200
1000
20
30

Sample Output 0

20

Explanation 0

Here ; selecting the integers , unfairness equals

max(10,20,30) - min(10,20,30) = 30 - 10 = 20

Sample Input 1

10
4
1
2
3
4
10
20
30
40
100
200

Sample Output 1

3

Explanation 1

Here ; selecting the integers , unfairness equals

max(1,2,3,4) - min(1,2,3,4) = 4 - 1 = 3
Medium
Submitted 39690 timesMax Score 35

Need Help?

View Discussions
View Editorial Solution
View Top Submissions

Rate This Challenge:

Resources

  • Two Pointer Technique
  • Greedy Technique
  • Sorting
Download problem statement
Download sample test cases
Suggest Edits
Set as default

You can always change back later.

Contest Calendar | Blog | Scoring | Environment | FAQ | About Us | Support | Careers | Terms Of Service | Privacy Policy | Request a Feature