Max sum in the configuration | gfg potd | 06-06-2024 | GFG Problem of the day

Sdílet
Vložit
  • čas přidán 4. 06. 2024
  • Geeks for Geeks Problem of the Day(POTD) in C++ | Max sum in the configuration | Fully Explained🧠
    Solution Code :
    github.com/IshanSawhney/GfG_P...
    IMPORTANCE OF DSA FOR PLACEMENT:
    • Is DSA Still Important...
    BEST FREE WEB DEVELOPMENT COURSE:
    • BEST Web development c...
    🌐 Connect with Me:
    GitHub: github.com/IshanSawhney
    Linkedin: / ishansawhney
    #GFG #POTD #geeksforgeeks #problemoftheday #c++

Komentáře • 17

  • @CodeGenius316
    @CodeGenius316  Před měsícem +2

    The key observation is that each element a[i] shifts one position to the right, changing its multiplier from i to (i+1) % n.
    Instead of recalculating the sum from scratch, we leverage the previous sum, subtract the total sum of the array, and add the last element (multiplied by n) that moved to the front.

  • @sameerashaik5388
    @sameerashaik5388 Před měsícem +1

    Very well explained brow.
    Done subscribing👍👍

  • @gamerbrawl4039
    @gamerbrawl4039 Před měsícem

    Great explanation bhaiya ❤

  • @Gamer-uc8bh
    @Gamer-uc8bh Před měsícem

    Thanks bro 😁

  • @aarkojha5710
    @aarkojha5710 Před měsícem

    Brute force-I will first initialise a sum =0 and another one maxsum. After that i will rotate the array from 0 to n-1 number of times and using for loop will find the sum of i.arr[] in each case. then use max function to find the max value. Do u have any idea abt optiimsing this one. As i think TC for this brute force soln would be N2

  • @akashsahoo2421
    @akashsahoo2421 Před měsícem

    you are rotating anti clockwise and telling clockwise in video

    • @CodeGenius316
      @CodeGenius316  Před měsícem +1

      Dude it's clockwise rotation only try to visualise
      Start at 8312.Move to 3128.If we visualize this on a clock face:Starting at 8.Moving clockwise, we would pass 3, then 1, then 2.Thus, moving from 8312 to 3128 on a clock face represents a clockwise rotation.

  • @meme-ku1vs
    @meme-ku1vs Před měsícem +3

    bhai explain kon karega ?

    • @CodeGenius316
      @CodeGenius316  Před měsícem +4

      Bhai bataya to hai video me vo formula hum use krke different rotated arrays ke sums nikal sakte hain aur fir yehi baat example ke through bhi bata di taaki clear hojaye ...agar ab mai formula hi derive krta to 10 lines ka mathematical derivation hai vo unecessary hai and video 1 hr ki hojati isiliye nahi kiya

    • @pankajmehta3370
      @pankajmehta3370 Před měsícem

      @@CodeGenius316 how you get the formula , did you study some where ??

    • @CodeGenius316
      @CodeGenius316  Před měsícem +2

      No , actually i encountered similar problems like this previously and it's a traditional method to solve these kinds of problems. The key observation is that each element a[i] shifts one position to the right, changing its multiplier from i to (i+1) % n.
      Instead of recalculating the sum from scratch, we leverage the previous sum, subtract the total sum of the array, and add the last element (multiplied by n) that moved to the front.
      Btw I like pfp🥰

  • @SUNNYSINGH-ol4qn
    @SUNNYSINGH-ol4qn Před měsícem

    Bhaiya Java me yeh fail ho jaa rha hai

    • @SUNNYSINGH-ol4qn
      @SUNNYSINGH-ol4qn Před měsícem +1

      Saare test case nai pass ho rhe hai

    • @CodeGenius316
      @CodeGenius316  Před měsícem +1

      Same logic Java me lgega bhai ...ye lo code:
      class Solution {
      long max_sum(int a[], int n) {
      // Your code here
      //initilization long for a sum =0
      long sum = 0;
      for (int i = 0; i < n; ++i)
      {
      sum += a[i];
      }
      long currSum = 0;
      for (int i = 0; i < n; ++i)
      {
      currSum += (long)i * a[i];
      }
      long maxi = currSum;
      for (int i = 1; i < n; ++i)
      {
      currSum = currSum + sum - (long)n * a[n - i];
      maxi = Math.max(maxi, currSum);
      }
      return maxi;
      }
      }