Minimize the maximum difference between the heights | Greedy Algorithm | Interview Preparation

preview_player
Показать описание
For Code and notes join Our Discord Server

GitHub for Codes :

Join Us
========

Given heights of n towers and a value k. We need to either increase or decrease the height of every tower by k (only once) where k 0. The task is to minimize the difference between the heights of the longest and the shortest tower after modifications and output this difference.

Time Stamps
===========
00:00 Intro
00:33 Problem Statement
06:41 Approach Building
10:13 Core Logic
13:00 Steps
14:08 Code
Рекомендации по теме
Комментарии
Автор

Nice explaination fully understand the cocept

AIT-Pune
Автор

Everyone says excellent explanation, actually i don't see any explanation here. He is not even touching any logic. Ham e karenga o karenga hen larenga ten karenga. Are vai kiu karega o to batao.

zahansafallwa
Автор

Awesome explanation of this problem....🔥

pankajjoshi
Автор

Kya Explain karte hain Sir🙌🙌, Kindly make videos on Love babbar DSA sheets Questions apart from you had already made as your Explanation is Top level Sir🙏

Prabhat_
Автор

this approach is damn easy for this question

mayankraj
Автор

At 3:42 minimum diff 8-4=4 you said it, ,
why not 7-5 or 7-6 ?

techkid
Автор

great explaination, just one end case is there for e.g. K=5 [1, 4, 4, 5, , 5, 6, 7, 7, 8, 9] ans should be 8 but your code gives 7, because we need to skip the case when minimum is less than 0.

adityasahu
Автор

if(minEle<0)continue;
add this condition before line 18

pranshuadhwaryu
Автор

Pretty good explanation on the intuition bro 👌, Can you explain how it comes under Greedy paradigm & how we can approach it with greedy

manikantagadde
Автор

why X = arr[i-1] and not arr[i], in line 12 of code ?

vibhasyadav
Автор

i m confused that i understood this problem or not !?

summitrawat
Автор

Nice explanation yr, finally i understood intuition behind the approch

sanchitbhadgal
Автор

Finally samjh aya thanks for the clarification seriously hard Q tha ye samjh nhi aa rha tha

yatri
Автор

Checked so many videos for this problem but this is the "THE BEST" explanation for this problem in internet

Emundadhu
Автор

GFG added more testcases, so this won't work, you just have to skip when the minEle is negative, see code below

int getMinDiff(int arr[], int n, int k) {

sort(arr, arr+n);

int ans = arr[n-1] - arr[0];

for(int i = 1; i < n; i++){
int _max = max(arr[n-1] - k, arr[i-1] + k);
int _min = min(arr[0] + k, arr[i] - k);

if(_min < 0) continue;

int diff = _max - _min;

ans = min(ans, diff);
}

return ans;
}

tusharnain
Автор

Nice explanation. But if we take pair of 2, 3 in 13:00 time stamp we will reduce 3 by 4 then we will get -1 at that point. But it is wrong because in problem statement it should be non negative. Please correct me if i am wrong

sriharshasamayamanthula
Автор

Awesome explaination.was stuck in this problem for 2, 3 days.😊.now its clear..thanks..

riteshhraj
Автор

Awesome explanation.. so many big channels have explained this very poorly but you did extremely well to make us understand 👍

rsrivastav
Автор

sabse badhiya explanation mila hai is problem ka abhi tak, thanks bro

anupampal
Автор

best explanation i have seen, lot of thanks sir

Iimran