• Courses
  • Tutorials
  • Jobs
  • Practice
  • Contests
December 06, 2023 |1.0K Views
PROBLEM OF THE DAY : 05/12/2023 | Minimize the Heights II
Description
Discussion

Welcome to the daily solving of our PROBLEM OF THE DAY with Jay Dalsaniya. We will discuss the entire problem step-by-step and work towards developing an optimized solution. This will not only help you brush up on your concepts of Arrays but also build up problem-solving skills.

In this problem, we are given, an array arr[] denoting heights of N towers and a positive integer K.

For each tower, you must perform exactly one of the following operations exactly once.

- Increase the height of the tower by K
- Decrease the height of the tower by K

Find out the minimum possible difference between the height of the shortest and tallest towers after you have modified each tower.
You can find a slight modification of the problem here.
Note: It is compulsory to increase or decrease the height by K for each tower. After the operation, the resultant array should not contain any negative integers.

Example :

Input:
K = 2, N = 4
Arr[] = {1, 5, 8, 10}
Output:
5

Explanation:
The array can be modified as 
{1+k, 5-k, 8-k, 10-k} = {3, 3, 6, 8}. 
The difference between the largest and the smallest is 8-3 = 5.

Give the problem a try before going through the video. All the best!!!
Problem Link: https://practice.geeksforgeeks.org/problems/minimize-the-heights3351/1
Solution IDE Link: https://ide.geeksforgeeks.org/online-cpp-compiler/e7e02956-97dd-46d9-9d1d-9fc4549c27ac