सोचिए कि एक AI किसी live video feed पर संदिग्ध गतिविधि monitor कर रहा है। वो हर सेकंड पूरी recording दोबारा नहीं देख सकता - उसे stream पर एक attention की window slide करनी होती है, बस उतना context रखते हुए जितना ज़रूरी है। Two pointers और sliding window patterns sequential data के साथ बिल्कुल यही करते हैं।
दोनों techniques brute-force O(n²) scans को elegant O(n) passes में बदल देती हैं।
किसी sequence में दो references रखो और उन्हें एक rule के हिसाब से move करो। इसके दो मुख्य तरीके हैं।
एक pointer शुरू में और एक अंत में रखो, फिर अंदर की तरफ़ move करो। Sorted data के लिए बेहतरीन है।
Classic problem - Pair Sum: एक sorted array दिया है, दो numbers ढूँढो जिनका sum target के बराबर हो।
function pairSum(arr, target):
left = 0
right = arr.length - 1
while left < right:
sum = arr[left] + arr[right]
if sum == target: return (left, right)
if sum < target: left += 1
else: right -= 1
return null
क्योंकि array sorted है, left को right move करने से sum बढ़ता है, और right को left move करने से sum घटता है - इससे कोई भी valid pair miss नहीं होती।
दोनों pointers आगे की तरफ़ move करते हैं, लेकिन अलग-अलग speed से। Partitioning या cycle detection के लिए useful है।
Example - Duplicates in-place remove करो: एक slow pointer write position mark करता है; एक fast pointer आगे scan करता है।
function removeDuplicates(arr):
slow = 0
for fast in 1..arr.length - 1:
if arr[fast] != arr[slow]:
slow += 1
arr[slow] = arr[fast]
return slow + 1
Converging two-pointer technique में sorted array पर, जब current sum target से कम हो तो क्या करना चाहिए?
Sign in to join the discussion
Sliding window एक contiguous sub-sequence maintain करती है जिसका size k (fixed) या variable हो सकता है, और aggregates को scratch से recalculate करने की बजाय incrementally update करती है।
बिल्कुल k elements की window को array पर slide करो।
Example - किन्हीं k consecutive elements का maximum sum:
function maxSubarraySum(arr, k):
windowSum = sum(arr[0..k-1])
best = windowSum
for i in k..arr.length - 1:
windowSum += arr[i] - arr[i - k]
best = max(best, windowSum)
return best
हर step में window में आने वाला नया element add होता है और बाहर जाने वाला subtract - O(1) per move।
ज़्यादा data include करने के लिए window expand करो, फिर जब constraint violate हो तो shrink करो।
Example - Longest substring without repeating characters:
function longestUnique(s):
seen = {}
left = 0
best = 0
for right in 0..s.length - 1:
if s[right] in seen and seen[s[right]] >= left:
left = seen[s[right]] + 1
seen[s[right]] = right
best = max(best, right - left + 1)
return best
| Pattern | AI Use Case | |---------|------------| | Fixed window | पिछली k sensor readings पर anomaly detection | | Variable window | सभी query keywords वाला सबसे छोटा text span ढूँढना (search engines) | | Converging pointers | Sorted embedding dimensions में efficient nearest-neighbour checks | | Same-direction pointers | Multiple retrieval models से sorted result lists को merge करना |
वो सबसे छोटा subarray ढूँढने के लिए कौन सा pattern चुनोगे जिसका sum एक दी गई value से ज़्यादा हो?
Brute-force nested loop को two-pointer या sliding-window solution में बदलने से typically कौन सी time complexity मिलती है?