Maximum sum of consecutive differences circular array | Max product subset | Love Babbar DSA sheet

preview_player
Показать описание
A great E-book on Data structures and algorithms written by a software engineer who has himself worked in MNC's. Its available for a very affordable price.
Do check it out by clicking on the link below and boost your preparation.

Problems and solutions:
1.Maximum sum of consecutive differences in a circular array:
solution:

2.Maximum product subset of array:

Playlist link:

Link to the questions:

Connect with me on Linkedin:

Connect with me on instagram:

#LoveBabbardsasheet #greedy #dsa #Placement #Coding #Fun #SDE #GFG #greedy #sorting
Рекомендации по теме
Комментарии
Автор

Thank you for making such great videos

gcgc
Автор

We can do the first question without a separate array, |1-8| + |8-2| + | 2-4| + | 4-1| can be written as |8-1| + |8-2| + | 4-2| + | 4-1|
so to maximize the sum we know that the larger element will always contribute to the sum and the smaller element will always reduce from the sum.

long long int maxSum(int arr[], int n)
{
long long int sum = 0;
sort(arr, arr+n);
int i=0, j = n-1;
while(i<j)
{
sum += arr[j--] - arr[i++];
}
return 2*sum;}

parthpareek
Автор

same question that you have solved in video 87 Q-2 maximum sum of absolute difference of permutaion

ashpreetsinghanand
Автор

For question 2, sorting is not required

stockgeeky
Автор

//sort this array
sort(arr, arr+n);

int sum=0;

int x=0; //traverse from front
int y=n-1; //traverse from backward

bool i=false;

while(x < y){
if(i == false){
sum += abs(arr[x] - arr[y]);
x++;
i=true;
} else{
sum += abs(arr[y] - arr[x]);
y--;
i=false;
}
}

sum += abs(arr[y] - arr[0]);
return sum;

charchitmangal