Reduction Operations to Make the Array Elements Equal | Intuition | Leetcode-1887

preview_player
Показать описание
This is the 68th Video on our Array Playlist
In this video we will try to solve a very good Array problem - Reduction Operations to Make the Array Elements Equal (Leetcode -1887).

I will explain the intuition so easily that you will never forget and start seeing this as cakewalk EASYYY.
We will do live coding after explanation and see if we are able to pass all the test cases.

Problem Name : Reduction Operations to Make the Array Elements Equal
Company Tags : Will update soon

╔═╦╗╔╦╗╔═╦═╦╦╦╦╗╔═╗
║╚╣║║║╚╣╚╣╔╣╔╣║╚╣═╣
╠╗║╚╝║║╠╗║╚╣║║║║║═╣
╚═╩══╩═╩═╩═╩╝╚╩═╩═╝

#coding #helpajobseeker #easyrecipes #leetcode #leetcodequestionandanswers #leetcodesolution #leetcodedailychallenge #leetcodequestions #leetcodechallenge #hindi #india #coding #helpajobseeker #easyrecipes #leetcode #leetcodequestionandanswers #leetcodesolution #leetcodedailychallenge#leetcodequestions #leetcodechallenge #hindi #india #hindiexplanation #hindiexplained #easyexplaination #interview#interviewtips #interviewpreparation #interview_ds_algo #hinglish #github #design #data #google #video #instagram #facebook
Рекомендации по теме
Комментарии
Автор

Sir, if you could include solutions of LeetCode contests, it would be greatly appreciated. Please add LeetCode solutions

DeepakSingh-fdix
Автор

Consistency 🔥🔥🔥🔥. Bhiaya question solve karne ki aadat lagane ke liye aur hume guide karne ke liye thank you.

anshukmishra
Автор

class Solution {
public:
int nums) {
sort(nums.begin(), nums.end());
int count = 0;
int size = nums.size() - 1;
for(int i = size - 1 ; i >= 0; i--){
if(nums[i] == nums[i+1]){
continue;
}
count += (size-i);
nums[i+1] = nums[i];
}
return count;
}
};

anshukmishra
Автор

My Logic - As we are decreasing the value...to be more precise...we are changing every element to its just smaller element...at the end...all the elements in the array will become equal to the minimum element of the array...so we just use a for loop and count how many operations it will take to make this particular element to minimum element....also the number of operations is equal to the number of unique values occured just before this number....so... :)




class Solution {
public:
int nums) {

int n = nums.size();

if(n == 1) return 0;

sort(nums.begin(), nums.end());

unordered_set<int> st;

int mini = nums[0];
int operations = 0;

for(int i = 0;i<n;i++) {
if(nums[i] == mini) {
continue;
}else if(nums[i] != nums[i-1]) {
st.insert(nums[i-1]);
operations += st.size();
}else{
operations += st.size();
}
}


return operations;

}
};

ammanchhetri
Автор

Leetcode 2939 - whenever you get time. No rush. Enjoy your Sunday

DevOpskagyaan
Автор

Great it by myself after watching half video.

creatorstudio
Автор

Great explanation... as always... thank you for making this video :)
ye wala khud se hi ban gya tha ....
the intuition that came to my mind after spending 3-5 mins was sort and count, ... after watching your video... i learned we could do reverse loop and then add (n - i)

class Solution {
public:
int nums) {
int n = nums.size();
sort(nums.begin(), nums.end());
int answer = 0;
int count = 0;
int previous = nums[0];

for (int i=0; i< n; i++){
if (previous != nums[i]){
count++;
}
answer += count;
previous = nums[i];
}
return answer;
}
};

mewaconite
Автор

My approach:

int ans=0, n=nums.size(), i=1, cnt=0;

sort(begin(nums), end(nums));

while(i<n && nums[i-1]==nums[i]) i+=1;

for(i;i<n;){
int j=i;
cnt+=1;
while(j<n && nums[j]==nums[i]) j+=1;
ans += (j-i)*cnt;
i=j;
}

return ans;

sauravchandra
Автор

class Solution {
public:

static bool comp(int a, int b){
return b<a;
}

int nums) {
sort(nums.begin(), nums.end(), comp);
int n=nums.size(), i=0, j=1, ans=0;
while(n>1 && i<j && j<n){
if(nums[i]>nums[j]){
nums[i]=nums[j];
ans+=(j-i);
}else if(nums[i]==nums[j]){
j++;
}
}
return ans;

}
};

mylearning
Автор

Hi All
Apart from the most optimal and amazing approach shared by code story with mik, I have also tried one more way to solve the same problem and the code is as follows : -
class Solution {
public int reductionOperations(int[] nums) {
TreeMap<Integer, Integer> tm = new
for(int i=0;i<nums.length;i++)
{
tm.put(nums[i], tm.getOrDefault(nums[i], 0)+1);
}

int biggest = tm.firstKey();
int smallest = tm.lastKey();
int prev = 0;
int curr = 0;

while(biggest != smallest)
{
curr = tm.get(biggest);
prev+=curr;
tm.remove(biggest);
biggest = tm.firstKey();
tm.put(biggest, tm.get(biggest)+curr);

}
return prev;
}
}

DurgaShiva
Автор

I also solved the question with the same approach before watching the video. As you told in the video .

saurabhKumar-hjyp
Автор

hey you! yes you mik ji,
thanks for making video like this 🥰

lofireverbz-wygo
Автор

broo contest kae k solutions bhi bnayae kriyaee .
your explaination good enough

pranjalyadav
Автор

Make video on today's 3rd question of contest.

chiragsingh
Автор

Hello bhaiyaa can you mention your time to solve that particular question because sometimes i feel ki mujhe itna time lag rha easy questions pe bhi ye comparison ke liye nhi apko mujhse bahot jyda ata hai ap to mentor jaise ho pr sirf generally time mention kr dena ki kitna time mai apse solve huaa video wala question even ani wali sari video mai

bobo_v
Автор

nums.sort()
p = 0
k = 0
for i in range(len(nums)-1):
if nums[i] != nums[i+1]:
k += 1
p += k

return p

infinitygaming
Автор

World Cup k baad contest ka last Qn dekhna bhaiya please 😄

FanIQQuiz
Автор

Bhaiya even i did thiss solution by own, but people have done with o(n)
By taking space like by using hash array and having freq and i didn't get that approach, can you please make a vedio on that, even i think yesterday or day before yesterday we got a question like that where we did sorting but on discuss form many have done with o(n).
Thx

Ramneet
Автор

Hi, can you record the videos in screen ratio same as other YouTube videos? Just a suggestion.

saumilvachheta
Автор

Reverse sort karke aur bhi easy lagta h. ops mei i+1 add krna h

S__Arslaan
welcome to shbcf.ru