This is my attemp to make the coding experience easier for you guys so that you can easily learn what to do in today's leetcode challenge.
Today's 02-01-24 Problem Link
Kadane's algorithm
- I simply applied Kadane's algorithm
- Stored the initial sum
- Trversed the array and kept track of elements from starting as well
- Updated the answer if got maximum till now
Have a look at the code , still have any confusion then please let me know in the comments Keep Solving.:)
- Time complexity :
$$O(n)$$ $$l$$ : length of given array
- Space complexity :
$$O(1)$$
// User function Template for Java
class Solution {
public long maxSumWithK(long a[], long n, long k){
long jor = 0; // to store the sum
long b = 0; // to keep track of left sum
int ib = 0; // to keep track of current leftsum index
for( int i = 0; i < (int)k; i++){
jor += a[i];
}
long jawab = Math.max( jor, Long.MIN_VALUE);
for( int i = (int)k; i < (int)n; i++){
jor += a[i];
b += a[ib++];
jawab = Math.max(jawab, jor);
if( b < 0){
jor -= b;
jawab = Math.max(jawab, jor);
b = 0;
}
}
return jawab;
}
}