Geeksforgeeks

Error

×

Leaderboard

Showing:

Handle | Score |
---|---|

@Ibrahim Nash | 6564 |

@blackshadows | 6400 |

@mb1973 | 5785 |

@Quandray | 5275 |

@akhayrutdinov | 5111 |

@saiujwal13083 | 5074 |

@kirtidee18 | 4639 |

@sanjay05 | 3762 |

@mantu_singh | 3712 |

@gfgaccount | 3627 |

@marius_valentin_dragoi | 3525 |

Complete Leaderboard | |

Handle | Score |

@yaswanthero123 | 625 |

@vikash619 | 611 |

@venkat11803610 | 501 |

@roshan13ar | 396 |

@DevangSingh | 379 |

@ravi11815721 | 312 |

@hrishipangavhane | 286 |

@aryanjain85297 | 280 |

@kommaraju11806903 | 277 |

@infosumitraut | 272 |

@ankitsharma5422 | 256 |

Complete Leaderboard |

Given an array of **n** integers and a positive number **k**. We are allowed to take any k integers from the given array. The task is to find the minimum possible value of the difference between maximum and minimum of **k** numbers.

**Example 1:**

**Input:
**N=7
K=3
arr[] = {10, 100, 300, 200, 1000, 20, 30}
**Output:
**20
**Explanation**:
20 is the minimum possible difference
between any maximum and minimum of any
k numbers.Given k = 3, we get the result
20 by selecting integers {10, 20, 30}.
max(10, 20, 30) - max(10, 20, 30) = 30 - 10
= 20.

**Example 2:**

**Input:
**N=2
K=2
arr[] = { 1,1 }
**Output:
**0
**Explanation**:
max({1,1}) - min({1,1}) = 1 - 1 = 0

**Your Task:**

Since, this is a function problem. You don't need to take any input, as it is already accomplished by the driver code. You just need to complete the function **minDiff**() that takes **array arr, integer N** **and integer K **as parameters and return the value of friendliness for the given array.

**Expected Time Complexity:** O(NLogN).

**Expected Auxiliary Space:** O(1).

**Constraints:**

2 ≤ N ≤ 10^{5}

Login to report an issue on this page.

We strongly recommend solving this problem on your own before viewing its editorial. Do you still want to view the editorial?

YesLoading...

Minimum Difference among K

...