Kadane's Algorithms for FAANG interviews
Let's talk about the most popular and most asked interview questions.
š š š¢š§š šš”š š¦šš±š¢š¦š®š¦ š¬š®š¦ šØš š ššØš§šš¢š š®šØš®š¬ š¬š®ššš«š«šš².
So how do you solve this question? š¤
So many approaches are there, but the most popular approach is
using KĢ³aĢ³dĢ³aĢ³nĢ³eĢ³āĢ³sĢ³ Ģ³aĢ³lĢ³gĢ³oĢ³rĢ³iĢ³tĢ³hĢ³mĢ³
šŗš šššš šš š²šš šššāš šššššššššš¤?
š Kadane's algorithm is used to find the maximum sum of a contiguous subarray within an oĢ³nĢ³eĢ³-Ģ³dĢ³iĢ³mĢ³eĢ³nĢ³sĢ³iĢ³oĢ³nĢ³aĢ³lĢ³ Ģ³aĢ³rĢ³rĢ³aĢ³yĢ³ Ģ³oĢ³fĢ³ Ģ³nĢ³uĢ³mĢ³bĢ³eĢ³rĢ³sĢ³, which is especially useful in financial or stock market analysis, weather data processing, and similar scenarios where trends or streaks need to be identified.
š šØšššššššš šŗšššš:
1. Initialize two variables:
- currentSum to track the ongoing sum of the subarray.
- maxSum to keep the maximum value found.
2. Iterate through the array:
- For each element, decide whether to add it to currentSum or start fresh with the current element (whichever is larger).
- Update maxSum if currentSum becomes greater than maxSum.
3. Return maxSum after processing all elements.
ššš'š¬ šš¢š¬šš®š¬š¬ šš”š š©š¬šš®ššØššØšš (š²šØš® š”ššÆš ššØ š¤š§šØš° š”šØš° ššØ š°š«š¢šš š©š¬šš®ššØššØšš šš¬ šš”š š¢š§ššš«šÆš¢šš°šš« šš¬š¤šš š²šØš® ššØ š°š«š¢šš š©š¬šš®ššØššØšš).
function kadaneAlgorithm(array):
Hereās the pseudocode for Kadane's Algorithm to find the maximum sum of a contiguous subarray:
Kadaneās Algorithm Pseudocode
function kadaneAlgorithm(array):
# Initialize variables
maxSum = array[0] # Start with the first element as max sum
currentSum = array[0] # Start with the first element as current sum
# Iterate over the array starting from the second element
for i from 1 to length(array) - 1:
# Update currentSum by choosing the maximum between the current element
# or the current element added to currentSum
currentSum = max(array[i], currentSum + array[i])
# Update maxSum if currentSum is greater
maxSum = max(maxSum, currentSum)
return maxSum
ššššššØšš š©š«šØšš„šš¦š¬:
- https://lnkd.in/gxZn4vBM
- https://lnkd.in/gqSFpNEp
- Maximum difference of 0's and 1's in a binary string
- Maximum Sum Circular array
- Smallest sum contiguous subarray
- Largest sum increasing contiguous subarray
- Maximum Product Subarray
- Largest sum contiguous subarray with only non-negative elements.
- Largest sum contiguous subarray with unique elements.
- Maximum Alternating Sum Subarray
- Maximum Sum Rectangle In A 2D Matrix
š¹ššš ššššš ššššššš šššššššš:
Imagine tracking daily temperature changes in a city for a month. Kadaneās algorithm could help you identify the warmest streak of consecutive days.
IĢ³ Ģ³hĢ³oĢ³pĢ³eĢ³ Ģ³yĢ³oĢ³uĢ³ Ģ³lĢ³iĢ³kĢ³eĢ³ Ģ³tĢ³hĢ³eĢ³ Ģ³eĢ³xĢ³pĢ³lĢ³aĢ³nĢ³aĢ³tĢ³iĢ³oĢ³nĢ³ Ģ³aĢ³nĢ³dĢ³ Ģ³uĢ³nĢ³dĢ³eĢ³rĢ³sĢ³tĢ³aĢ³nĢ³dĢ³iĢ³nĢ³gĢ³ Ģ³oĢ³fĢ³ Ģ³AĢ³lĢ³gĢ³oĢ³rĢ³iĢ³tĢ³hĢ³mĢ³sĢ³
Ģ³
Ģ³FĢ³oĢ³lĢ³lĢ³oĢ³wĢ³ Ģ³aĢ³nĢ³dĢ³ Ģ³cĢ³oĢ³nĢ³nĢ³eĢ³cĢ³tĢ³ Ģ³fĢ³oĢ³rĢ³ Ģ³mĢ³oĢ³rĢ³eĢ³ Ģ³cĢ³oĢ³dĢ³iĢ³nĢ³gĢ³ Ģ³aĢ³nĢ³dĢ³ Ģ³WĢ³eĢ³bĢ³ Ģ³dĢ³eĢ³vĢ³eĢ³lĢ³oĢ³pĢ³mĢ³eĢ³nĢ³tĢ³ Ģ³pĢ³oĢ³sĢ³tĢ³sĢ³ Ģ³ Ravi Kumar