1749. Maximum Absolute Sum of Any Subarray | leetcode daily challenge | greedy | faang | shashcode

preview_player
Показать описание
Problem Link:

Problem Statement:
You are given an integer array nums. The absolute sum of a subarray [numsl, numsl+1, ..., numsr-1, numsr] is abs(numsl + numsl+1 + ... + numsr-1 + numsr).
Return the maximum absolute sum of any (possibly empty) subarray of nums.
Note that abs(x) is defined as follows:
If x is a negative integer, then abs(x) = -x.
If x is a non-negative integer, then abs(x) = x.

Solution Link:

Custom Comparator:

Lambda Expression:

Dynamic Programming:

Graph Playlist:

Java Plus DSA Placement Course Playlist:

Java Plus DSA Sheet:

Notes:

Telegram Link:

Ultimate Recursion Series Playlist:

Samsung Interview Experience:

Company Tags:
Facebook | Amazon | Microsoft | Netflix | Google | LinkedIn | Pega Systems | VMware | Adobe | Samsung

Timestamp:
0:00 - Basics
0:43 - understanding problem & brute force
4:12 - kadane's algorithm
7:50 - kadane for positive and negative number
12:49 - code
End
Рекомендации по теме
Комментарии
Автор

public int maxAbsoluteSum(int[] nums) {
int sum = 0, minSum = 0, maxSum = 0;
for (int num : nums) {
sum += num;
if (sum > maxSum) maxSum = sum;
if (sum < minSum) minSum = sum;
}
return Math.abs(maxSum - minSum);
}

prankcall
Автор

like target for this video is 180. Please do like if you have understood the explanation as well as the code.❤❤

shashwat_tiwari_st
Автор

Happy Mahashivratri to everyone and sir 🙏

KedarBhawthankar
Автор

bhaiya very very good explaination i loved it so much ❤❤❤❤

yashgoyal
Автор

I was looking for kadanes explanation. thankyou

workAP-gg
Автор

class Solution {
public int maxAbsoluteSum(int[] nums) {
int n = nums.length;
int maxEnding = nums[0];
int res1 = nums[0];
int minEnding = nums[0];
int res2 = nums[0];
for(int i=1;i<n;i++){
maxEnding = Math.max(nums[i], maxEnding+nums[i]);
res1 = Math.max(maxEnding, res1);

minEnding = Math.min(nums[i], minEnding+nums[i]);
res2 = Math.min(minEnding, res2);
}
return Math.max(Math.abs(res1), Math.abs(res2));
}
} 27% faster but why ?

nishantrathore