Array 03
Array 03
Link : https://leetcode.com/problems/maximum-subarray/description/
Example 1 :
Example 2 :
Example 3:
Constraints :
Intuition :
We will check the sum of every possible subarray and consider the maximum
among them. To get every possible subarray sum, we will be using three
nested loops. The first loops(say i and j) will iterate over every possible
starting index and ending index of a subarray. Basically, in each iteration,
the subarray range will be from index i to index j. Using another loop we will
get the sum of the elements of the subarray [i…..j]. Among all values of the
sum calculated, we will consider the maximum one.
Approach :
• First, we will run a loop(say i) that will select every possible starting
index of the subarray. The possible starting indices can vary from
index 0 to index n-1(n = size of the array).
• Inside the loop, we will run another loop(say j) that will signify the
ending index of the subarray. For every subarray starting from the
index i, the possible ending index can vary from index i to n-1(n = size
of the array).
• After that for each subarray starting from index i and ending at index
j (i.e. arr[i….j]), we will run another loop to calculate the sum of all the
elements(of that particular subarray).
Note : We are selecting every possible subarray using two nested loops and
for each of them, we add all its elements using another loop.
Dry Run :
Complexity Analysis :
Intuition :
If we carefully observe, we can notice that to get the sum of the current
subarray we just need to add the current element(i.e. arr[j]) to the sum of
the previous subarray i.e. arr[i….j-1].
Assume previous subarray = arr[i……j-1]
current subarray = arr[i…..j]
Sum of arr[i….j] = (sum of arr[i….j-1]) + arr[j]
This is how we can remove the third loop and while moving j pointer, we can
calculate the sum.
Approach :
• First, we will run a loop(say i) that will select every possible starting
index of the subarray. The possible starting indices can vary from
index 0 to index n-1(n = array size).
• Inside the loop, we will run another loop(say j) that will signify the
ending index as well as the current element of the subarray. For every
subarray starting from index i, the possible ending index can vary from
index i to n-1(n = size of the array).
• Inside loop j, we will add the current element to the sum of the
previous subarray i.e. sum = sum + arr[j]. Among all the sums the
maximum one will be the answer.
Dry Run :
Complexity Analysis :
Intuition :
Here, we will iterate the given array with a single loop and while iterating we
will add the elements in a sum variable. Now, if at any point the sum
becomes less than 0, we will set the sum as 0 as we are not going to consider
any subarray with a negative sum. Among all the sums calculated, we will
consider the maximum one.
Thus we can solve this problem with a single loop.
Approach :
Note: In some cases, the question might say to consider the sum of the
empty subarray while solving this problem. So, in these cases, before
returning the answer we will compare the maximum subarray sum calculated
with 0(i.e. The sum of an empty subarray is 0). And after that, we will
return the maximum one.
For e.g. if the given array is {-1, -4, -5}, the answer will be 0 instead of -1 in
this case.
But if this case is not explicitly mentioned we will not consider this case.
Output : The maximum subarray sum is: 6
Complexity Analysis :
Submission on Leetcode :
MAXIMUM CONSECUTIVE ONE'S
15 February 2024 11:11
Link : https://leetcode.com/problems/max-consecutive-ones-
iii/description/
Example 1 :
Example 2 :
Constraints :
Solution :
Optimal Approach :
Constraints :
k zeros flip (means how much we have given ‘k’ values then we can flip that
zeros to get consecutive maximum 1's)
We get subarrays
Algorithm :
• First initialize two pointers both will start from beginning. These
pointers basically represent left and right boundary of sliding window.
• Then increment the right pointer ‘j’ unless it will be equal to ‘k’. This
ensures the number of zeros in sliding window is not greater than ‘k’.
• If the size of zeros exceeds then we stop ‘j’ and then moves ‘i’ to
shrink the window until it met the condition again.
• At each step we will update our initialized variable ‘ans’ that will keep
track of number of one’s.
Complexity Analysis :
Link : https://leetcode.com/problems/maximum-difference-
between-increasing-elements/description/
Example 1 :
Example 2 :
Example 3:
Constraints :
n == nums.length
2 <= n <= 1000
1 <= nums[i] <= 109
Complexity Analysis :
Complexity Analysis :
First find the difference between the adjacent elements of the array
and store all differences in an auxiliary array diff[] of size n-1. Now
this problems turns into finding the maximum sum subarray of this
difference array. Thanks to Shubham Mittal for suggesting this
solution.
Output :
Maximum difference is 98
Complexity Analysis :
Submission on Leetcode :