Skip to content

Commit 8dee24f

Browse files
committed
dsa add: Kadane's Algorithm
1 parent f13a550 commit 8dee24f

File tree

2 files changed

+66
-3
lines changed

2 files changed

+66
-3
lines changed

dsa/README.md

+66-3
Original file line numberDiff line numberDiff line change
@@ -35,9 +35,11 @@
3535
- [Graphs](#-graphs)
3636
- [Matrix (2D Arrays)](#-matrix-2d-arrays)
3737
3. [Algorithms](#-algorithms)
38-
- [Related Techniques and Tricks](#-related-techniques-and-tricks)
38+
- [Techniques and Tricks](#-techniques-and-tricks)
3939
- [Recursion](#-recursion)
4040
- [The Gauss' Trick](#-the-gauss-trick)
41+
- [Related to Arrays](#-related-to-arrays)
42+
- [Kadane's Algorithm](#-kadanes-algorithm)
4143
- [Sorting](#-sorting)
4244
- [Bubble Sort](#-bubble-sort)
4345
- [Selection Sort](#-selection-sort)
@@ -1646,14 +1648,14 @@ The structure of a 2D array allows for efficient access, modification, and trave
16461648
# 🟪 Algorithms
16471649

16481650
<p align="center">
1649-
<a href="#-related-techniques-and-tricks">Related Techniques and Tricks</a> •
1651+
<a href="#-techniques-and-tricks">Techniques and Tricks</a> •
16501652
<a href="#-sorting">Sorting</a> •
16511653
<a href="#-searching">Searching</a>
16521654
</p>
16531655

16541656
<br>
16551657

1656-
## 🔶 Related Techniques and Tricks
1658+
## 🔶 Techniques and Tricks
16571659

16581660
### 🔷 Recursion
16591661

@@ -1836,6 +1838,67 @@ S = (n / 2) * (a + l);
18361838

18371839
</details>
18381840

1841+
<br>
1842+
1843+
### 🔷 Related to Arrays
1844+
1845+
#### 🔻 Kadane's Algorithm
1846+
1847+
Kadane's Algorithm is an efficient `O(n)` method to find the maximum sum of a contiguous subarray in a given array.
1848+
1849+
> The main logic: If adding the next number improves the sum, keep going. Otherwise, start fresh from the next number.
1850+
1851+
<br>
1852+
1853+
Examples:
1854+
1855+
<p align="center">
1856+
<img src="./kd.png" height="auto" width="250">
1857+
</p>
1858+
1859+
- Without Kadane’s Algorithm: `O(n^2)`
1860+
1861+
```js
1862+
function maxSubArray(arr) {
1863+
let maxSum = arr[0];
1864+
1865+
for (let i = 0; i < arr.length; i++) {
1866+
let currentSum = 0;
1867+
1868+
for (let j = i; j < arr.length; j++) {
1869+
currentSum += arr[j];
1870+
maxSum = Math.max(maxSum, currentSum);
1871+
}
1872+
}
1873+
1874+
return maxSum;
1875+
}
1876+
1877+
maxSubArray([-2, -3, 4, -1, -2, 1, 5, -3]); // 7
1878+
```
1879+
1880+
- With Kadane’s Algorithm: `O(n)`
1881+
1882+
```js
1883+
function maxSubArray(arr) {
1884+
let maxSum = arr[0];
1885+
let currentSum = 0;
1886+
1887+
for (let num of arr) {
1888+
currentSum += num;
1889+
maxSum = Math.max(maxSum, currentSum);
1890+
1891+
if (currentSum < 0) {
1892+
currentSum = 0;
1893+
}
1894+
}
1895+
1896+
return maxSum;
1897+
}
1898+
1899+
maxSubArray([-2, -3, 4, -1, -2, 1, 5, -3]); // 7
1900+
```
1901+
18391902
<p align="right">
18401903
<a href="#data-structures--algorithms">back to top ⬆</a>
18411904
</p>

dsa/kd.png

4.81 KB
Loading

0 commit comments

Comments
 (0)