TGViewer
Programming Resources | Python | Javascript | Artificial Intelligence Updates | Computer Science Courses | AI Books Programming Resources | Python | Javascript | Artificial Intelligence Updates | Computer Science Courses | AI Books @programming_guide ยท 56K subscribers
Post #3463 2.21K
โœ… DSA Part 3 โ€“ Arrays & Sliding Window ๐Ÿ“Š๐Ÿง 

Arrays are the foundation of data structures. Mastering them unlocks many advanced topics like sorting, searching, and dynamic programming.

1๏ธโƒฃ What is an Array?
An array is a collection of elements stored at contiguous memory locations. All elements are of the same data type.

Python Example:
arr = [10, 20, 30, 40]
print(arr[2]) # Output: 30

C++ Example:
int arr[] = {10, 20, 30, 40};
cout << arr[2]; // Output: 30

Java Example:
int[] arr = {10, 20, 30, 40};
System.out.println(arr[2]); // Output: 30

2๏ธโƒฃ Basic Array Operations:
โ€ข Insert
โ€ข Delete
โ€ข Traverse
โ€ข Search
โ€ข Update

Python โ€“ Traversal:
for i in arr:
print(i)

C++ โ€“ Search:
for (int i = 0; i < n; i++) {
if (arr[i] == key) {
// Found
}
}

Java โ€“ Update:
arr[1] = 99;  // Updates second element

3๏ธโƒฃ Sliding Window Technique ๐ŸชŸ
Used to reduce time complexity in problems involving subarrays or substrings.

โ–ถ๏ธ Fixed-size window:
Find max sum of subarray of size k
โ–ถ๏ธ Variable-size window:
Find longest substring with unique characters

4๏ธโƒฃ Sliding Window โ€“ Max Sum Subarray (Size k)

Python:
def max_sum(arr, k):
window_sum = sum(arr[:k])
max_sum = window_sum
for i in range(k, len(arr)):
window_sum += arr[i] - arr[i - k]
max_sum = max(max_sum, window_sum)
return max_sum

print(max_sum([1, 4, 2, 10, 2, 3], 3)) # Output: 16

5๏ธโƒฃ Practice Tasks:
โœ… Find the second largest element in an array
โœ… Implement sliding window to find max sum subarray
โœ… Try variable-size window: longest substring without repeating characters

๐Ÿ‘‡ Solution for Practice Tasks

โœ… 1. Find the Second Largest Element in an Array

Python:
def second_largest(arr):
first = second = float('-inf')
for num in arr:
if num > first:
second = first
first = num
elif first > num > second:
second = num
return second if second != float('-inf') else None

print(second_largest([10, 20, 4, 45, 99])) # Output: 45

โœ… 2. Max Sum Subarray (Fixed-size Sliding Window)

Python:
def max_sum(arr, k):
window_sum = sum(arr[:k])
max_sum = window_sum
for i in range(k, len(arr)):
window_sum += arr[i] - arr[i - k]
max_sum = max(max_sum, window_sum)
return max_sum

print(max_sum([1, 4, 2, 10, 2, 3, 1, 0, 20], 4)) # Output: 24

โœ… 3. Longest Substring Without Repeating Characters (Variable-size Sliding Window)

Python:
def longest_unique_substring(s):
seen = {}
left = max_len = 0
for right in range(len(s)):
if s[right] in seen and seen[s[right]] >= left:
left = seen[s[right]] + 1
seen[s[right]] = right
max_len = max(max_len, right - left + 1)
return max_len

print(longest_unique_substring("abcabcbb")) # Output: 3 ("abc")

Double Tap โ™ฅ๏ธ For Part-4
  • โค 13
More from @programming_guide
  1. Oct 9, 2026๐Ÿ‡ฎ๐Ÿ‡ณ ๐—š๐—ข๐—ฉ๐—˜๐—ฅ๐—ก๐— ๐—˜๐—ก๐—ง ๐—ข๐—™ ๐—œ๐—ก๐——๐—œ๐—” โ€” ๐—”๐—œ๐—–๐—ง๐—˜ ๐—œ๐—ก๐—ง๐—˜๐—ฅ๐—ก๐—ฆ๐—›๐—œ๐—ฃ๐—ฆ ๐Ÿฎ๐Ÿฌ๐Ÿฎ๐Ÿฒ ๐Ÿš€โ€ฆ
  2. Oct 8, 2026๐ŸŽ“ ๐— ๐—ถ๐—ฐ๐—ฟ๐—ผ๐˜€๐—ผ๐—ณ๐˜ ๐—™๐—ฅ๐—˜๐—˜ ๐—–๐—ผ๐˜‚๐—ฟ๐˜€๐—ฒ๐˜€ ๐˜„๐—ถ๐˜๐—ต ๐—–๐—ฒ๐—ฟ๐˜๐—ถ๐—ณ๐—ถ๐—ฐ๐—ฎ๐˜๐—ฒ๐˜€! ๐Ÿš€๐Ÿ”ฅ Upgrโ€ฆ
  3. Oct 7, 2026๐Ÿš€๐—ฃ๐—ฎ๐˜† ๐—”๐—ณ๐˜๐—ฒ๐—ฟ ๐—ฃ๐—น๐—ฎ๐—ฐ๐—ฒ๐—บ๐—ฒ๐—ป๐˜ ๐—ง๐—ฟ๐—ฎ๐—ถ๐—ป๐—ถ๐—ป๐—ด | ๐—•๐—ฒ๐—ฐ๐—ผ๐—บ๐—ฒ ๐—ฎ ๐—™๐˜‚๐—น๐—น๐˜€๐˜๐—ฎ๐—ฐโ€ฆ
  4. Oct 7, 2026๐— ๐—ฎ๐˜€๐˜๐—ฒ๐—ฟ ๐—ฃ๐—ผ๐˜„๐—ฒ๐—ฟ ๐—•๐—œ ๐—ณ๐—ผ๐—ฟ ๐—™๐—ฅ๐—˜๐—˜! ๐Ÿ”ฅ Learn Power BI through these FREE learninโ€ฆ
  5. Sep 29, 2026๐Ÿง  Core Programming Concepts You Should Know ๐Ÿ’ป๐Ÿš€ These are the fundamental ideas behind aโ€ฆ
  6. Sep 29, 2026๐—™๐—ฅ๐—˜๐—˜ ๐—ฅ๐—ฒ๐˜€๐—ผ๐˜‚๐—ฟ๐—ฐ๐—ฒ๐˜€ ๐—ง๐—ผ ๐—Ÿ๐—ฒ๐—ฎ๐—ฟ๐—ป ๐—”๐—œ ๐—ถ๐—ป ๐Ÿฎ๐Ÿฌ๐Ÿฎ๐Ÿฒ๐Ÿš€ โ€‹ Explore 6 free resourceโ€ฆ
Threads Profile ViewerView any public Threads profile without an account.Open ThreadLook โ†’Writing with AI? Make it sound human.Metric37 rewrites AI drafts so they read naturally. Free AI detector, 1,500 words free.Try Metric37 โ†’