Max sum in the configuration | gfg potd | 06-06-2024 | GFG Problem of the day
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++
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.
Very well explained brow.
Done subscribing👍👍
Thanks a lot 🙏🏻
Great explanation bhaiya ❤
Thanks 🙏🏻
Thanks bro 😁
❤️
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
you are rotating anti clockwise and telling clockwise in video
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.
bhai explain kon karega ?
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
@@CodeGenius316 how you get the formula , did you study some where ??
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🥰
Bhaiya Java me yeh fail ho jaa rha hai
Saare test case nai pass ho rhe hai
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;
}
}