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....

ความคิดเห็น • 211

  • @CoderArmy9
    @CoderArmy9  ปีที่แล้ว +158

    You Guys are Love❤

    • @YashSaini007
      @YashSaini007 ปีที่แล้ว +1

      Good Morning Bhaiya ❣️

    • @Kalamkaar2607
      @Kalamkaar2607 ปีที่แล้ว +1

      @CoderArmy9 is love

    • @bonkai.movies
      @bonkai.movies ปีที่แล้ว +2

      bhaiya your debugging process in blackboard is making you diffrent then others keep it up BestOluck

    • @HONESTLYALOK
      @HONESTLYALOK ปีที่แล้ว

      Thank you bhaiya

    • @VivekMishra-yq5us
      @VivekMishra-yq5us ปีที่แล้ว

      ❤❤

  • @Updatedknowledge9
    @Updatedknowledge9 ปีที่แล้ว +13

    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,

  • @inspirationalyouthicons4998
    @inspirationalyouthicons4998 ปีที่แล้ว +29

    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

    • @Anjali-ty2fk
      @Anjali-ty2fk 2 หลายเดือนก่อน +2

      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;

    • @ronitapatra
      @ronitapatra หลายเดือนก่อน

      @@Anjali-ty2fk first you have to calcualte the difference then update suffix...otherwise suffix = nums[n-1] will update first...

    • @ujjwalgaur5460
      @ujjwalgaur5460 23 วันที่ผ่านมา +1

      @@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

  • @Sajal_Mallick_vlogs
    @Sajal_Mallick_vlogs 10 หลายเดือนก่อน +2

    Day 39/180 done and dusted.... Awesome explanation and exceptional learning.. Thank you bhaiya... #consistency

  • @imavinashsingh
    @imavinashsingh 10 หลายเดือนก่อน +9

    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]

  • @rvpool9830
    @rvpool9830 ปีที่แล้ว +15

    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

    • @xavdok
      @xavdok 27 วันที่ผ่านมา +1

      I run your code and I did not get the ans, your code is wrong.

  • @maroofgpm
    @maroofgpm ปีที่แล้ว +3

    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

  • @joydeep-halder
    @joydeep-halder ปีที่แล้ว +12

    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]
    }

  • @yashisrivastava6196
    @yashisrivastava6196 ปีที่แล้ว +7

    Will never forget kadane's algorithm now ✨

  • @sarikasaxena5567
    @sarikasaxena5567 ปีที่แล้ว +2

    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];
    }

  • @VivekMishra-yq5us
    @VivekMishra-yq5us ปีที่แล้ว +3

    Chamk gya hai concept ❤ bhiya

  • @PotatoPcGamer01
    @PotatoPcGamer01 7 หลายเดือนก่อน +4

    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 :)

  • @atifmalik8012
    @atifmalik8012 3 หลายเดือนก่อน

    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

    • @AryanShrivastav-n2v
      @AryanShrivastav-n2v 27 วันที่ผ่านมา

      Gltt ha bhai..prefix vala line sum kr nicche aayga

  • @rockrock350
    @rockrock350 ปีที่แล้ว +1

    never saw this much of dedication

  • @PandaSingh-o3j
    @PandaSingh-o3j 7 หลายเดือนก่อน +3

    At 48:45 when you add 7 and -5 it is 2 but don't replace it with maxi as maxi contains 7.

  • @arjungurjar7702
    @arjungurjar7702 14 วันที่ผ่านมา

    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

  • @mohdshabaz9675
    @mohdshabaz9675 ปีที่แล้ว +6

    Good explanation of previous videos

    • @CoderArmy9
      @CoderArmy9  ปีที่แล้ว +6

      Good morning bhai❤

  • @TheSilentObserver30
    @TheSilentObserver30 ปีที่แล้ว +15

    Day 39/180..Growing strong each day and learning new everyday. You are making us consistent and confident. Thank you.

  • @rohanbarwade578
    @rohanbarwade578 5 หลายเดือนก่อน

    best mentor ever

  • @rahulpatil6871
    @rahulpatil6871 8 หลายเดือนก่อน +10

    yaha tk smja ya ni smja ,haan ji bhaiya samj gya😂

  • @ajaythedaredevil7220
    @ajaythedaredevil7220 5 หลายเดือนก่อน +1

    Print all Subarray in an array:
    for i in range(len(res)):
    for j in range(i+1,len(res)):
    print(res[i:j])

  • @aayushrajpurohit321
    @aayushrajpurohit321 9 หลายเดือนก่อน +1

    #Chamaka diya bhaiya it was an amazing session.

  • @Anonymous_EngineerX
    @Anonymous_EngineerX 8 หลายเดือนก่อน

    Kadane's algo to be honest outstanding explanation Thanks bhaijaan

  • @OsamaShabih
    @OsamaShabih 2 หลายเดือนก่อน

    Chamak gya bhaiya 😊

  • @GayatriShejule-o1k
    @GayatriShejule-o1k หลายเดือนก่อน

    Thank you so much sir 🥰

  • @AnuragSingh-im2md
    @AnuragSingh-im2md 24 วันที่ผ่านมา

    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

  • @mahuaranu4435
    @mahuaranu4435 7 หลายเดือนก่อน +1

    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;
    }

  • @Coder-rohits
    @Coder-rohits ปีที่แล้ว

    maza aa gaya bhaiya🙌

  • @Sangi-sj2dy
    @Sangi-sj2dy 7 หลายเดือนก่อน +1

    Simply Awesome

  • @heetpatel3037
    @heetpatel3037 11 หลายเดือนก่อน

    Chamka bhaiya thank you 👍🏻

  • @RohitCoder
    @RohitCoder 11 หลายเดือนก่อน +1

    at 32:17 why dont we used for(int i=0; i

    • @bee.eee.
      @bee.eee. 6 หลายเดือนก่อน +2

      n-1 refers to the last element
      If we take i

  • @_hulk748
    @_hulk748 ปีที่แล้ว

    Great Video as always sir🙏🙇‍♂✨💖

  • @Coder_Avi
    @Coder_Avi 2 วันที่ผ่านมา

    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

  • @rejaulislam2593
    @rejaulislam2593 20 วันที่ผ่านมา

    alhamdulillah lec done

  • @Updatedknowledge9
    @Updatedknowledge9 ปีที่แล้ว

    Maza aa gya 😊😊😊😊❤

  • @ankushladani496
    @ankushladani496 ปีที่แล้ว +1

    Thanks Bhaiya...❤🎉

  • @ankushladani496
    @ankushladani496 ปีที่แล้ว +1

    Done Bhaiya... 39/180 ✅

  • @abhisheksinghmehra9576
    @abhisheksinghmehra9576 ปีที่แล้ว

    Amazing explanation bhai as always🔥🔥🔥

  • @GoogleSoftwareEnginear
    @GoogleSoftwareEnginear ปีที่แล้ว

    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 😊😊👍👍

  • @arpitkhandelwal8280
    @arpitkhandelwal8280 6 หลายเดือนก่อน

    amazing lecture bhaiya

  • @Rishi_s.corner
    @Rishi_s.corner 11 หลายเดือนก่อน

    chamak gaya bhaiya.

  • @amritpathak8630
    @amritpathak8630 5 หลายเดือนก่อน

    Great effort

  • @csforcommonsense3360
    @csforcommonsense3360 ปีที่แล้ว

    tq for ur lots of efforts

  • @joydeep-halder
    @joydeep-halder ปีที่แล้ว +14

    9:32 H/W Solution: Print all subarrays:
    for (int size = 1; size

    • @joydeep-halder
      @joydeep-halder ปีที่แล้ว

      ​@@ddoy7378bhai compiler me run karke dekho.

  • @study-yd6es
    @study-yd6es 5 หลายเดือนก่อน

    Amazing Content !!!

  • @sourabhgupta1466
    @sourabhgupta1466 7 หลายเดือนก่อน

    best lecture bhai

  • @NoorulAbdeenSiddiqui
    @NoorulAbdeenSiddiqui 7 หลายเดือนก่อน

    u are the best

  • @atifmalik8012
    @atifmalik8012 3 หลายเดือนก่อน

    int equalSubarray(vectorA)
    {
    int prefix=0,Totalsum=0;
    for(int i=0;i

  • @Lifeofmoana22
    @Lifeofmoana22 5 หลายเดือนก่อน

    thankyou bhaiyaaa

  • @saadrml
    @saadrml 4 หลายเดือนก่อน +1

    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 🕊

  • @GOATSOFFOOTBALL
    @GOATSOFFOOTBALL 6 หลายเดือนก่อน

    completed,samajh aya

  • @swapnadip
    @swapnadip ปีที่แล้ว

    Good morning Bhaiya 😊🤗🤗

  • @PinkuModi-c6i
    @PinkuModi-c6i 6 หลายเดือนก่อน

    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!!

  • @akashkewat1219
    @akashkewat1219 ปีที่แล้ว

    Completed 😊.

  • @Hiisbnxkskkwl
    @Hiisbnxkskkwl หลายเดือนก่อน

    49:10 time stap pae toh 7 bada ha toh vohi rahega na sir

  • @DesiMrBean7
    @DesiMrBean7 ปีที่แล้ว

    Good morning negi bhai❤

  • @nishikantDalal
    @nishikantDalal 3 หลายเดือนก่อน

    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 ;

  • @shailjayadav7995
    @shailjayadav7995 ปีที่แล้ว

    Good Morning Bhaiya❤

  • @atifmalik8012
    @atifmalik8012 3 หลายเดือนก่อน

    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

  • @AMITKUMAR-ds4hp
    @AMITKUMAR-ds4hp ปีที่แล้ว

    Good morning boss ❤❤❤

  • @nitinpal880
    @nitinpal880 ปีที่แล้ว

    bhaiya apki vajah se improvement dekh para hu bhot jyada bas revision ka tarika bhi bata do aap

  • @shubhamkumarjha9192
    @shubhamkumarjha9192 ปีที่แล้ว

    Good morning Bhaiya..❤️

    • @CoderArmy9
      @CoderArmy9  ปีที่แล้ว

      Good morning bhai❤

  • @devanshu9167
    @devanshu9167 ปีที่แล้ว +1

    🔥✅️

  • @neerajrawat4294
    @neerajrawat4294 ปีที่แล้ว +2

    smallest sum contingous subarray is also a variation of kadane's algorithm;

  • @AzeemKhan-oe8rt
    @AzeemKhan-oe8rt ปีที่แล้ว +2

    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?

  • @asad_iliyas
    @asad_iliyas ปีที่แล้ว +1

    9:29 howework

  • @joydeep-halder
    @joydeep-halder ปีที่แล้ว

    Good morning bhaiya ❤❤

  • @SnatanGaming
    @SnatanGaming 28 วันที่ผ่านมา +1

    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:(

  • @TayyabaYasmine
    @TayyabaYasmine 3 หลายเดือนก่อน

    Any one know in which lactures he covers the operator overloading and prefis and post fix operator overloading function

  • @diyaseth9688
    @diyaseth9688 ปีที่แล้ว

    Thank you bhaiya

  • @GaziZai
    @GaziZai 11 หลายเดือนก่อน

    Thanks

  • @Codewithkumar-gc1kr
    @Codewithkumar-gc1kr 3 หลายเดือนก่อน

    chamak gaya elder brother😊😊 09-Nov-24

  • @arunrajput8153
    @arunrajput8153 ปีที่แล้ว

    Love u bhai

  • @santanudebnath1283
    @santanudebnath1283 ปีที่แล้ว +1

    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

  • @jiraiyagaming6491
    @jiraiyagaming6491 ปีที่แล้ว

    Good morning 🌞

    • @CoderArmy9
      @CoderArmy9  ปีที่แล้ว

      Good morning bhai❤

  • @SurajKumar-vc4du
    @SurajKumar-vc4du ปีที่แล้ว

    radhe radhe

  • @punitpubggaming7531
    @punitpubggaming7531 ปีที่แล้ว

    Attendance marked ✅

  • @lotifurshabbir7302
    @lotifurshabbir7302 ปีที่แล้ว

    1:00:59 vaia here if i wrote that if condition before updating the maxi line, will it work correctly?

  • @parthh1112
    @parthh1112 11 หลายเดือนก่อน +1

    48:55 error 7>2 so no update needed

  • @PravilaKumari-uv5be
    @PravilaKumari-uv5be ปีที่แล้ว

    Good morning sir

  • @vermapowerhub
    @vermapowerhub ปีที่แล้ว +1

    Bhaiya aaj aapne day nhi dala video discription m ,ki konsa day h 180 days challenge ka ☺️

  • @ankittjindal
    @ankittjindal ปีที่แล้ว

    First ❤

    • @CoderArmy9
      @CoderArmy9  ปีที่แล้ว

      Good morning bhai❤

  • @drlovephukn
    @drlovephukn ปีที่แล้ว

    Day's 39/180 full power full power ❤❤❤

  • @AakashParvani
    @AakashParvani ปีที่แล้ว

    Day 39/180 👍✅ Chamak Gaya

  • @AzeemKhan-oe8rt
    @AzeemKhan-oe8rt ปีที่แล้ว +2

    last wale 2-3 lecture nhi dekh paaya yaha se continue kr skata honnn?????????/
    @coder army

  • @joydeep-halder
    @joydeep-halder ปีที่แล้ว +2

    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

  • @OnlyPc-c9n
    @OnlyPc-c9n 11 หลายเดือนก่อน

    function me 2nd for lop n-1 tak chalege na

  • @sahilCSE999
    @sahilCSE999 11 หลายเดือนก่อน

    Bhaia 51:58 pe 13 kaise hoga 7+6+2 = 15 hoga n

  • @karankumar3983
    @karankumar3983 ปีที่แล้ว

    Logic behind kadanes algorithm 55:00 to 59:08 kadanes ke pichhe ka kala such 😅

  • @Saumyasingh0001
    @Saumyasingh0001 11 หลายเดือนก่อน

    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 😅

    • @divyanshsharma673
      @divyanshsharma673 10 หลายเดือนก่อน

      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.

  • @yuvrajnikam8094
    @yuvrajnikam8094 ปีที่แล้ว

    ❤❤❤

  • @Armoured_Traveller
    @Armoured_Traveller ปีที่แล้ว

    Day 39
    Bhaiya ek motivational video chahiye apka🎉🎉❤

  • @deep_singh01
    @deep_singh01 ปีที่แล้ว

    Day 39/180 👍👍👍

  • @Hermitsays007
    @Hermitsays007 5 หลายเดือนก่อน

    homework done
    suffix[n-1] = arr[n-1];
    for(int i = n-2; i>=0; i--){
    suffix[i] = suffix[i+1] + arr[i];
    }

  • @bsufeotxe
    @bsufeotxe ปีที่แล้ว +1

    bhaiya dutch national flag problem bhi samjha do pointer ki help se

  • @rajatjana2513
    @rajatjana2513 ปีที่แล้ว +1

    Day1🙏🙏🙏

  • @GopalKumar-xu3iw
    @GopalKumar-xu3iw ปีที่แล้ว

    Dya 39/180 complete # hard

  • @techmcu
    @techmcu 5 หลายเดือนก่อน

    if I'm not wrong bhaiya n-1 indicating last element position

  • @vermapowerhub
    @vermapowerhub ปีที่แล้ว

    Good morning coders ❤

  • @anshikathakur3567
    @anshikathakur3567 11 หลายเดือนก่อน

    lecture 29✅