Lecture 29: Kadane's Algorithm || Prefix and Suffix Sum || Array into 2 equal Sum Subarray
ฝัง
- เผยแพร่เมื่อ 7 ก.พ. 2025
- In Array, Today we learn about Prefix and Suffix.
1: Kadane's Algorithm: practice.geeks...
2: Maximum Difference between 2 element: www.geeksforge...
(We only have article for 2nd problem, so solve it on your local compiler)
3: Maximum prefix sum for a given range: practice.geeks...
4: Equal Sums: practice.geeks...
HomeWork Sheet: drive.google.c...
Join Our Whatsapp Channel: whatsapp.com/c...
Day 39/180, #180daysofcode #180 hard
We have made a whole video in c++, How to solve pattern print problem. We explained everything with the help of code.
We are doing 180 days challenge and going to complete the whole course within the duration with quality content on TH-cam. I am on the mission to create a tech revolution in our country and in upcoming future we want to create a tech which will create many jobs in India.
Video will come on Mon-Fri at 6am in the morning
CoderArmy Website (Buy premium feature at 99 Only for 6 months): www.coderarmy.in
Premium Feature includes:
1: Doubt Support
2: Mentorship session
3: Placement Support to Top Students
4: Resume and Linkedin Profile Building
5: Coding contest twice a month
6: Course completion Certificate
7: Home work Discussion
DSA Course for free
C++ Free Course
Rohit Negi DSA Course C++
Coder Army DSA Course c++
Function in C++
Pass by value.
Pass by reference
C++ important topics
connect to me on Instagram: rohit978.page....
Linkedin: rohit978.page....
Telegram: rohit978.page....
You Guys are Love❤
Good Morning Bhaiya ❣️
@CoderArmy9 is love
bhaiya your debugging process in blackboard is making you diffrent then others keep it up BestOluck
Thank you bhaiya
❤❤
Bhaiya aap ek baat universal truth hai, ki starting me sab easy lagta hai, fir hard iske baaad, fir se sab easy lagne lagta hai ☺☺☺❤❤❤, main to aise hi feel kar rha hu,
1:15:03 hw maximum diffrence between two elements:-
int maximumdiffrencebetweentwoelements(int arr[],int n){
int suffix[n];
suffix[n-1] = arr[n-1];
for(int i=n-2;i>=0;i--){
suffix[i] = max(suffix[i+1],arr[i]);
}
int ans = INT_MIN;
for(int i=0;i
Aap ek baar iss solution main mistake bta do please
int suffix=nums[n-1];
int ans=INT_MIN;
for(int i =N-2;i>=0;i--)
{
suffix=max(nums[i],suffix);
ans=max(ans,suffix-nums[i]);
} return ans;
} return -1;
@@Anjali-ty2fk first you have to calcualte the difference then update suffix...otherwise suffix = nums[n-1] will update first...
@@Anjali-ty2fk dont know this method i solved through two pointers mthd#include
using namespace std;
int main() {
int arr[8] = {9, 8, 8, 12, 0, 3, 7, 4};
int s = 0, e = 1; // Start and end pointers
int maxdiff = 0; // To store the maximum difference
int n = 8; // Size of the array
while (e < n) {
if (arr[e] > arr[s]) {
// Calculate the difference
int diff = arr[e] - arr[s];
// Update maxdiff if the current difference is greater
if (diff > maxdiff) {
maxdiff = diff;
}
} else {
// Move the start pointer to the current end pointer
s = e;
}
// Always move the end pointer forward
e++;
}
cout
Day 39/180 done and dusted.... Awesome explanation and exceptional learning.. Thank you bhaiya... #consistency
0:00:01 [Prefix and Suffix Sum]
0:09:50 [Divide Array in two Sub arrays with equal sum ]
0:28:15 [Code Part]
0:35:15 [Largest Sum Contiguous Sub array]
0:47:40 [Kadane's Algorithm ]
1:01:15 [Code Part]
1:03:08 [Max. Difference b/w 2 elements]
To print all subarrays:
#include
using namespace std;
int main(){
int n;
cin>>n;
int arr[n];
for(int i=0; i> arr[i];
for(int i=0; i
I run your code and I did not get the ans, your code is wrong.
5:08 Likh Diya!!
// Homework: Sum of suffix
#include
#include
using namespace std;
int main()
{
int vect[] = {1,2,3,4,5};
int n = 5;
for (auto i : vect) {
cout
5:10 H/W Suffix Sum:
suffix[n-1] = arr[n-1]
for(int i=n-2; i>=0; --i)
{
suffix[i] = suffix[i+1] + arr[i]
}
why you are giving answer ☹️
@@lotifurshabbir7302 To help others 😊
Will never forget kadane's algorithm now ✨
suffixsum array H.W-
vectorsuff(n);
suff[n-1]=arr[n-1];
for(int i = n-2;i>=0;i--){
suff[i] = suff[i+1]+arr[i];
}
Chamk gya hai concept ❤ bhiya
1:15:03 - Find Largest Difference between 2 elements (HOMEWORK)
Time Complexity - O(N)
Code :
int largestDiff(vector& v, int index)
{
// 1) initialization :
int size = v.size()-1;
int difference = 0, ans=INT_MIN, findMax=INT_MIN;
// 2) handling edge cases :
if(index size)
return -1;
// 3) find max difference :
for(int i = size; i>=index; i--){
findMax = max(findMax, v[i]);
difference = findMax - v[i];
ans = max(ans, difference);
}
return ans;
}
PS - Suggestions / Replies are welcomed :)
59:29
//Kadane's algorithm
class Solution {
public:
// Function to find the sum of contiguous subarray with maximum sum.
int maxSubarraySum(vector &arr) {
int prefix = 0,MaxSum = INT_MIN;
for(int i = 0;i
Gltt ha bhai..prefix vala line sum kr nicche aayga
never saw this much of dedication
At 48:45 when you add 7 and -5 it is 2 but don't replace it with maxi as maxi contains 7.
1:14:30
int maxDifference(int arr[],int n){
int ans = INT_MIN ;
int suffix = 0 ;
for(int i = n-1 ; i >=0 ; i--){
suffix = max(suffix , arr[i]);
ans = suffix - arr[i];
cout
Good explanation of previous videos
Good morning bhai❤
Day 39/180..Growing strong each day and learning new everyday. You are making us consistent and confident. Thank you.
best mentor ever
yaha tk smja ya ni smja ,haan ji bhaiya samj gya😂
Print all Subarray in an array:
for i in range(len(res)):
for j in range(i+1,len(res)):
print(res[i:j])
#Chamaka diya bhaiya it was an amazing session.
Kadane's algo to be honest outstanding explanation Thanks bhaijaan
Chamak gya bhaiya 😊
Thank you so much sir 🥰
Answer for the last question:
int n = nums.size();
int suffix = nums[n-1];
int ans = 0;
int diff;
for(int i = n-2; i>=0; i--){
diff = suffix - nums[i];
ans = max(ans , diff);
suffix = max(suffix , nums[i]);
}
if(ans==0) return -1;
else return ans;
o(n) T.C
HW problem ans 1:15:03
int max_difference(int arr[], int n){
int suffix = arr[n-1]; int maxi = INT32_MIN ; int diff = 0;
for(int i = n-2; i>=0; i--){
suffix = max(suffix , arr[i]);
diff = suffix - arr[i];
maxi = max(maxi , diff);
}
return maxi;
}
maza aa gaya bhaiya🙌
Simply Awesome
Chamka bhaiya thank you 👍🏻
at 32:17 why dont we used for(int i=0; i
n-1 refers to the last element
If we take i
Great Video as always sir🙏🙇♂✨💖
code of the last one,
int suffix=arr[n-1], ans=INT_MIN;
for ( i=n-2 ; i>=0 ; i--)
{
ans = max(ans, suffix-arr[i]);
suffix=max(suffix,arr[i]);
}
cout
alhamdulillah lec done
Maza aa gya 😊😊😊😊❤
Thanks Bhaiya...❤🎉
Done Bhaiya... 39/180 ✅
Amazing explanation bhai as always🔥🔥🔥
Bhaiyo itna badiya content bhaiya hme de rhe hai to Subscribe to bnta hai sabhi kre
Thank you so much Bhaiya for this amazing DSA series 😊😊👍👍
amazing lecture bhaiya
chamak gaya bhaiya.
Great effort
tq for ur lots of efforts
9:32 H/W Solution: Print all subarrays:
for (int size = 1; size
@@ddoy7378bhai compiler me run karke dekho.
Amazing Content !!!
best lecture bhai
u are the best
int equalSubarray(vectorA)
{
int prefix=0,Totalsum=0;
for(int i=0;i
thankyou bhaiyaaa
bhaiya, i think there is slight mistake at 48:54 prefix( mein 2 aane pe) maxi(jo pehle se 7 tha ) usko 2 update nhi karenge , balki 7 hi rehne denge , i hope someone finds its correct 🕊
completed,samajh aya
Good morning Bhaiya 😊🤗🤗
bhaiya yaar apne pucha tha ki beech me to ni aa rhe h ad, but aa rhe h-av just aaya jab main 33:15 time stamp pe pahucha aur jaise hi ek question complete hua!!
Completed 😊.
49:10 time stap pae toh 7 bada ha toh vohi rahega na sir
Good morning negi bhai❤
Maximum Difference between 2 element
code:
int ans=0;
int suffix_max=arr[n-1];
for(int i = n - 2 ; i >= 0 ; i - -){
ans=max(ans , suffix_max - arr[i]);
if(arr[i] > suffix_max)
suffix_max = arr[i] ;
}
return ans ;
Good Morning Bhaiya❤
23:08
//i tried it myslef without watching
//In this approch i made two verctors one is prefix other one is suffix
//if the perticular index in prefix is same as the element present at index+1 in suffix then return true otherwise false(for all elements)
class Solution {
public:
bool canPartition(vector& nums) {
int n = nums.size();
vectorprefix(n);
vectorsuffix(n);
prefix[0] = nums[0];
for(int i = 1;i=0;i--){
suffix[i] = suffix[i+1] + nums[i];
}
for(int i = 0;i
Good morning boss ❤❤❤
bhaiya apki vajah se improvement dekh para hu bhot jyada bas revision ka tarika bhi bata do aap
Good morning Bhaiya..❤️
Good morning bhai❤
🔥✅️
smallest sum contingous subarray is also a variation of kadane's algorithm;
bhai mujhse homework nhi ho paa rha, jbke me solution sochta rehta hoon 1-2 hours but code me convert ho hi nhi paa rha hai?
9:29 howework
Good morning bhaiya ❤❤
Good morning bhai❤
@@CoderArmy9 😊😊
I AM UNABLE TO SOLVE LAST 2 Questions OF THE HOMEWORK SHEET
I FOUND THIS LECTURE A BIT TOUGH CONSIDERING ALL THE PREVIOUS LECTURES,BECAUSE IN ALL PREVIOUS HOMEWORK SHEET I ATLEAST THINK OF AN APPROACH TO SOLVE BUT IN THIS LECTURE I AM UNABLE TO DO THAT ALSO.I HAVE TO PRACTICE HARD:(
Any one know in which lactures he covers the operator overloading and prefis and post fix operator overloading function
Thank you bhaiya
Thanks
chamak gaya elder brother😊😊 09-Nov-24
Love u bhai
MAX DIFFERENCE
#include
using namespace std;
int main()
{
int n,max_diff=0,ans;
int arr[10];
coutn;
cout=0;i--)
{
if(arr[i]>prefix)
prefix=arr[i];
else
{
ans=prefix-arr[i];
max_diff=max(ans,max_diff);
}
}
cout
Good morning 🌞
Good morning bhai❤
radhe radhe
Attendance marked ✅
1:00:59 vaia here if i wrote that if condition before updating the maxi line, will it work correctly?
48:55 error 7>2 so no update needed
Good morning sir
Bhaiya aaj aapne day nhi dala video discription m ,ki konsa day h 180 days challenge ka ☺️
First ❤
Good morning bhai❤
Day's 39/180 full power full power ❤❤❤
Day 39/180 👍✅ Chamak Gaya
last wale 2-3 lecture nhi dekh paaya yaha se continue kr skata honnn?????????/
@coder army
H/W Solutoin: : Maximum prefix sum for a given range
vector ans;
for (int i = 0; i < Q; ++i)
{
int start = L[i];
int end = R[i];
int maxPrefix = INT_MIN;
int prefix = 0;
for (int j = start; j
❤
i < Q what does it mean in third line
function me 2nd for lop n-1 tak chalege na
Bhaia 51:58 pe 13 kaise hoga 7+6+2 = 15 hoga n
Logic behind kadanes algorithm 55:00 to 59:08 kadanes ke pichhe ka kala such 😅
Great explanation but i think at 18 min par jo inner loop par 2n kah rahe hai wo 2n nahi hoga waha par dono inner loop milakar n time he chala tho n hona chahiye 😅
In worst case, first loop will run n-1 times and the second will 1 time. So n((n-1)+1) = n². It could be for both end so this is why it's 2n.
❤❤❤
Day 39
Bhaiya ek motivational video chahiye apka🎉🎉❤
Day 39/180 👍👍👍
homework done
suffix[n-1] = arr[n-1];
for(int i = n-2; i>=0; i--){
suffix[i] = suffix[i+1] + arr[i];
}
bhaiya dutch national flag problem bhi samjha do pointer ki help se
Day1🙏🙏🙏
Dya 39/180 complete # hard
if I'm not wrong bhaiya n-1 indicating last element position
Good morning coders ❤
lecture 29✅