Sum of Absolute Differences in a Sorted Array | Build Intuition | 2 Approaches | Leetcode - 1685

  Рет қаралды 3,876

codestorywithMIK

codestorywithMIK

Күн бұрын

Пікірлер
@ytritik-st7gr
@ytritik-st7gr Жыл бұрын
Bhaiya please it's a request from my side, never stop teaching .... you are a true gem in teaching
@codestorywithMIK
@codestorywithMIK Жыл бұрын
Means a lot to me 😇🙏❤️ Sure thing
@DeepakSingh-fd2ix
@DeepakSingh-fd2ix Жыл бұрын
very true bro
@aws_handles
@aws_handles Жыл бұрын
True
@wearevacationuncoverers
@wearevacationuncoverers Жыл бұрын
0:09 This always boosts my confidence.
@deepakdass4710
@deepakdass4710 Жыл бұрын
Sir, ONE REQUEST from my side I have been following your channel since last 2 months. I just love your teaching style, you have helped me a lot in making my problem solving skills better. I am about to start following your GRAPH playlist after my exams end. I have one request for you. Please try to make videos for LEETCODE contests as well, if possible, I think that will help us to learn devloping intuition in contest.
@codestorywithMIK
@codestorywithMIK Жыл бұрын
Yes sure thing. I have slowly started it. As of now, i am able to take out time for one problem (last Qn of contest or any which is required most in the comments - most Qn 3 and Qn 4) Soon with time, I will try covering all contest problems 🙏❤️ Thank you so much for watching my contents 😇🙏
@deepakdass4710
@deepakdass4710 Жыл бұрын
@@codestorywithMIK Yes Q3 and 4 will be fine for now. Thanks sir 😍😍😍😍
@akan3shaD
@akan3shaD Жыл бұрын
​@@codestorywithMIK yess sir please make intuition videos on LC contest as well 🔥
@DeepakSingh-fd2ix
@DeepakSingh-fd2ix Жыл бұрын
yes @codestorywithmik please sir post the contest solution also because no one can explain the question easy as you explain
@dchennaraidu
@dchennaraidu Жыл бұрын
Bhai, legend ho app! 🙏🏼 Appreciate your skills and patience to break it down and explain clearly!
@codestorywithMIK
@codestorywithMIK Жыл бұрын
It means a lot 🙏🙏❤️❤️
@ammanchhetri6716
@ammanchhetri6716 Жыл бұрын
tags - Adobe
@codestorywithMIK
@codestorywithMIK Жыл бұрын
Thank you so much 😇❤️🙏
@ammanchhetri6716
@ammanchhetri6716 Жыл бұрын
@@codestorywithMIK anytime 🤝
@thor1626
@thor1626 Жыл бұрын
Solved the question by my own after looking at the test cases, but watching your video made me understand the underlying logic. My java sol : (3 ms Beats 99.36% of users with Java.) class Solution { public int[] getSumAbsoluteDifferences(int[] nums) { int len = nums.length; int[] prefixSum = new int[len + 1]; prefixSum[0] = 0; int sum = 0; int index = 1; for (int i = 0; i < len; i++) { sum += nums[i]; prefixSum[index] = sum; index++; } for (int i = 0; i < len; i++) { int x = (prefixSum[len] - prefixSum[i]) - (nums[i] * (len - i)); int y = (nums[i] * i) - prefixSum[i]; nums[i] = x + y; } return nums; } }
@Ramneet04
@Ramneet04 Жыл бұрын
I got the intuition that we have to make and imade two arrays prefix and suffix sum array but unable to get the formula.Glad I came up till this solution,I even solved yesterday's ,say before yesterday question by own,thx a lot, even did with 0(1) space solution.
@FanIQQuiz
@FanIQQuiz Жыл бұрын
Done. It was easy as compared to usual Weekends problems. Thanks a lot for your explanation
@BiswajitDas-lk7pp
@BiswajitDas-lk7pp 10 ай бұрын
Amazing Explanation Bro ❤
@codeandtalk6
@codeandtalk6 Жыл бұрын
Absolute makes problem hard 😢 but bro explained like ❤❤❤❤
@de_coder1
@de_coder1 Жыл бұрын
Was able to do it by approach 1, calculating prefix sum on the way was good.
@sauravchandra10
@sauravchandra10 Жыл бұрын
Seemed like an easy one, and like always your way of thinking is so clear and your explanations so simple. My implementation: class Solution { public: vector getSumAbsoluteDifferences(vector& nums) { int n=nums.size(), pre=0; vector preSum(n,0); for(int i=0;i=0;i--){ int leftSum = (pre-preSum[i]) - (nums[i]*(n-1-i)); int rightSum= 0; if(i>0) rightSum += (nums[i]*i)-preSum[i-1]; preSum[i]=leftSum+rightSum; } return preSum; } };
@saurabhKumar-hj6yp
@saurabhKumar-hj6yp Жыл бұрын
You are really an awesome person.
@aws_handles
@aws_handles Жыл бұрын
You are just fabulous 🔥
@nirmaljaat9029
@nirmaljaat9029 Жыл бұрын
Very Easy to Understand class Solution { public: vector getSumAbsoluteDifferences(vector& nums) { int sum=0; for(int & i: nums){ sum+=i; } int n=nums.size(); vector ans; int l=nums[0]; ans.push_back(sum-nums[0]*n); sum-=nums[0]; for(int i=1;i
@dhairyachauhan6622
@dhairyachauhan6622 Жыл бұрын
Took me like 40 mins to think of the solution but happy to tell that i did it on my own :) my solution :- class Solution { public: typedef long long ll; vector getSumAbsoluteDifferences(vector& nums) { int n = nums.size(); vectorprefix(n,0); vectorsuffix(n,0); int sum = 0; for(int i = 0;i=0;j--){ sum += nums[j]; suffix[j] = sum - (n-j)*nums[j]; } vectorans(n,0); for(int i =0 ;i N , -VE, -VE 3-> +VE ,N,-VE 5-> +VE,+VE,N THE ABSOLUTE VALUE OF 2 AND 5 WERE CORRECT BUT NOT FOR 3 REASON BEING THERE WAS ONE +VE AND ONE -VE, EITHER USE ALL +VE OR ALL -VE. SO I THOUGH SINCE IT WAS SORTED ALL THE VALUES TILL AND BEFORE THE CURRENT NO. WILL ALWAYS BE POSITIVE SO I STORED (CURRENT NO. * LENGTH - SUM TILL THE LENGTH IN THE ARRAY) [prefix[i] = (i+1)*nums[i] - sum;] EG: [2,3,5] IN THE SUFFIX ARRAY AS (suffix[j] = sum - (n-j)*nums[j];) NOW BOTH WILL BE POSITIVE AND I WILL GET MY ANSWER. YES I COULD HAVE USED ONLY ONE PREFIX ARRAY BUT IT IS MORE UNDERSTANDABLE IN 2 ARRAYS.
@codestorywithMIK
@codestorywithMIK Жыл бұрын
Awesome 🔥💪❤️
@dhairyachauhan6622
@dhairyachauhan6622 Жыл бұрын
@@codestorywithMIK
@infinitygaming7192
@infinitygaming7192 Жыл бұрын
class Solution: def getSumAbsoluteDifferences(self, nums: List[int]) -> List[int]: prefix_sum = [nums[0]] for i in range(1,len(nums)): prefix_sum.append(nums[i] + prefix_sum[i-1]) arr = [] for i in range(len(nums)): left = ((nums[i]*(i+1) )-prefix_sum[i]) right = (nums[i]*(len(nums) - (i+1))) - (prefix_sum[-1] - prefix_sum[i]) term = abs(left) + abs(right) arr.append(term) return arr
@55_hetpatel74
@55_hetpatel74 Жыл бұрын
Nice
@DeepakSingh-fd2ix
@DeepakSingh-fd2ix Жыл бұрын
Sir, this biweekly contest question about finding the minimum number of coins for fruits seems quite tricky and interesting. If you could explain it, it would help me a lot
@codestorywithMIK
@codestorywithMIK Жыл бұрын
Can you please share the qn link
@ashishj9497
@ashishj9497 3 ай бұрын
legend
@DevOpskagyaan
@DevOpskagyaan Жыл бұрын
Legend 🫡❤️
@souravjoshi2293
@souravjoshi2293 Жыл бұрын
Made it super easy.
@crazyduniya128
@crazyduniya128 Жыл бұрын
how you get confidence to directly submit your code, without running on example test cases
@codestorywithMIK
@codestorywithMIK Жыл бұрын
Hi there, actually before making video I submit my code as well in the GitHub and hence i sometimes submit directly. But i usually suggest everyone to first submit on example test cases and then after that submit.
@nk-fs8bj
@nk-fs8bj Жыл бұрын
❤❤❤
@biswarupacharjya2258
@biswarupacharjya2258 Жыл бұрын
Sir if possible linklist playlist 🙏🏻🙏🏻🙏🏻🙏🏻🙏🏻
@infinitygaming7192
@infinitygaming7192 Жыл бұрын
solved this question without any hints
@codestorywithMIK
@codestorywithMIK Жыл бұрын
Awesome 😇😇🙌
@factsmotivation7728
@factsmotivation7728 Жыл бұрын
Anyone can say me how can i join whatapp group,i am unable to join whatapp group, help...
@codestorywithMIK
@codestorywithMIK Жыл бұрын
Can you please try from this link - whatsapp.com/channel/0029Va6kVSjICVfiVdsHgi1A
@factsmotivation7728
@factsmotivation7728 Жыл бұрын
@@codestorywithMIK thank you Bhaiya
@shivam9048
@shivam9048 Жыл бұрын
🥰👍
@dayashankarlakhotia4943
@dayashankarlakhotia4943 Жыл бұрын
public int getSumAbsoluteDifferences (int[]nums){ int n=nums.length; int[]ans=new int [n];int[]prefix =new int [n]; int[]suffix =new int [n]; prefix[0]=nums[0]; for(int i=1;i=0;i--){ suffix [i]=suffix [i+1]+nums[i]; for(int i=0;i
Почему Катар богатый? #shorts
0:45
Послезавтра
Рет қаралды 2 МЛН
#behindthescenes @CrissaJackson
0:11
Happy Kelli
Рет қаралды 27 МЛН
-5+3은 뭔가요? 📚 #shorts
0:19
5 분 Tricks
Рет қаралды 13 МЛН
Counting Sort | Algorithms Lecture 24 | The cs Underdog
38:11
The cs Underdog
Рет қаралды 5
Josephus Problem | Game of Death in a circle | Execution in Circle
33:56
541 Leetcode problems are NOT enough.
7:12
Sahil & Sarra
Рет қаралды 161 М.
Arpit Bhayani talks about real engineering for 1 hour straight
1:16:23
Knuth-Morris-Pratt KMP String Matching Algorithm | Search Pattern | GFG POTD
1:05:36
Non-Decreasing Array - Leetcode 665 - Python
10:59
NeetCode
Рет қаралды 58 М.
Почему Катар богатый? #shorts
0:45
Послезавтра
Рет қаралды 2 МЛН