logoNEURAL COOK
SearchAll PostsTagsNotesAuthor
instagramtwitterfacebookyoutubelinkedinmailgithubrss
Copyright © 2026 All rights reserved.
•
VIKI
Built with Vikramaditya Singh
Built with
VIKI - A posts created with Next.js and Tailwind.css
About
leetcode
Title
Table Of Contents
easy
13. Roman to Integer
14. Longest Common Prefix
20. Valid Parentheses
26. Remove Duplicates from Sorted Array
35. Search Insert Position
69. Sqrt(x)
70. Climbing Stairs
88. Merge Sorted Array (In-Place)
104. Maximum Depth of Binary Tree
118. Pascal's Triangle
121. Best Time to Buy and Sell Stock
205. Isomorphic Strings
242. Valid Anagram
283. Move Zeroes
345. Reverse Vowels of a String
605. Can Place Flowers
704. Binary Search
796. Rotate String
1071. Greatest Common Divisor of Strings
1299. Replace Elements with Greatest Element on Right Side
1431. Kids With the Greatest Number of Candies
1614. Maximum Nesting Depth of the Parentheses
1752. Check if Array Is Sorted and Rotated
1768. Merge Strings Alternately
1903. Largest Odd Number in String
2965. Find Missing and Repeated Values
hard
25. Reverse Nodes in k-Group
26. Remove Duplicates from Sorted Array
27. Remove Element
42. Trapping Rain Water
51. N-Queens
55. Jump Game
80. Remove Duplicates from Sorted Array II
88. Merge Sorted Array
121. Best Time to Buy and Sell Stock
122. Best Time to Buy and Sell Stock II
189. Rotate Array
274. H-Index
medium
2. Add Two Numbers
3. Longest Substring Without Repeating Characters
5. Longest Palindromic Substring
8. String to Integer (atoi)
11. Container With Most Water
15. 3Sum
17. Letter Combinations of a Phone Number
19. Remove Nth Node From End of List
22. Generate Parentheses
33. Search in Rotated Sorted Array
34. Find First and Last Position of Element in Sorted Array
48. Rotate Image
49. Group Anagrams
50. Pow(x, n)
53. Maximum Subarray
54. Spiral Matrix
56. Merge Intervals
61. Rotate List
73. Set Matrix Zeroes
74. Search a 2D Matrix
75. Sort Colors
78. Subsets
137. Single Number II
138. Copy List with Random Pointer
150. Evaluate Reverse Polish Notation
151. Reverse Words in a String
153. Find Minimum in Rotated Sorted Array
155. Min Stack
162. Find Peak Element
169. Majority Element
198. House Robber
207. Course Schedule
215. Kth Largest Element in an Array
227. Basic Calculator II
236. Lowest Common Ancestor of a Binary Tree
237. Delete Node in a Linked List
238. Product of Array Except Self
451. Sort Characters By Frequency
540. Single Element in a Sorted Array
560. Subarray Sum Equals K
636. Exclusive Time of Functions
875. Koko Eating Bananas
1011. Capacity To Ship Packages Within D Days
1101. The Earliest Moment When Everyone Become Friends
1283. Find the Smallest Divisor Given a Threshold
2140. Solving Questions With Brainpower
3169. Count Days Without Meetings
3481. Apply Substitutions
rotate-array-approach-1
189. Rotate Array (Approach 2 )
189. Rotate Array (Approach 1 )
number-of-islands-approach-1
200. Number of Islands (Approach 2)
200. Number of Islands (Approach 1)
meeting-rooms-ii-approach-1
253. Approach Using Priority Queue
253. Meeting Rooms II
misc
Calculate Floor & Ceiling
Find Nth Root Of M
Flatten A Linked List
Implement Lower Bound
Implement Upper Bound
Number of occurrence
take-u-forward
Recently Solved
arrays
The Ultimate Array Pattern Playbook
faqs-hard
Count Inversions in an Array
Find The Repeating And Missing Number
Majority Element II
Maximum Product Subarray
Merge Sorted Array (In-Place)
Reverse Pairs
faqs-medium
3. 3Sum
4. 4Sum
Maximum Subarray
Leaders in an Array
Majority Element
Next Permutation
Get specific element from Pascal's Triangle
Pascal's Triangle II (Row Generation)
Generate Pascal's Triangle (Full)
Spiral Matrix Traversal
Rearrange Array Elements by Sign
Rotate Image
Sort Colors (Dutch National Flag)
Two Sum
fundamentals
Check if Array is Sorted
Largest Element in an Array
Array Rotation (Left and Right)
Left Rotate Array by One
Linear Search
Max Consecutive Ones
Second Largest Element in an Array
Single Number
logic-building
Missing Number
Intersection of Two Sorted Arrays (with Duplicates)
Move Zeros to End
Remove Duplicates from Sorted Array
Union of Two Sorted Arrays
binary-search
About
d-arrays
Find Row With Maximum 1'S
Search In A 2D Matrix
faqs
Aggressive Cows
Allocate Books
Find Peak Element
Kth Element Of 2 Sorted Arrays
Minimise Max Distance to Gas Stations
Split Array - Largest Sum
fundamentals
Lower Bound
Binary Search
Upper Bound
logic-building
Count Occurrences in a Sorted Array
Find Minimum in Rotated Sorted Array
Find out how many times the array is rotated
Find First and Last Position of Element in Sorted Array
Floor and Ceil in Sorted Array
Kth Missing Positive Number
Search in Rotated Sorted Array
Search in Rotated Sorted Array II (With Duplicates)
Search Insert Position
Single Element in a Sorted Array
on-answers
Capacity To Ship Packages Within D Days
Find Nth Root of M
Find Square Root of a Number
Smallest Divisor Given a Threshold
Koko Eating Bananas (Minimum Rate)
Minimum Days to Make M Bouquets
Painter's Partition Problem
Split Array Largest Sum
hashing
Theory
faqs
Subarray Sum Equals K
Count Subarrays with Given XOR K
Largest Subarray with 0 Sum
Longest Consecutive Sequence
Longest Subarray with Sum K
recursion
faqs-medium
Combination Sum II (Unique Combinations)
Combination Sum (Unbounded)
Subset Sums
hard
Letter Combinations of a Phone Number
implementation-problems
Generate Parentheses
Power Function: pow(x, n)
Subsets (The Power Set)
subsequence-pattern-problems
Subsequence Sum Existence
Count All Subsequences With Sum K
sorting
Bubble Sort
Insertion Sort
Merge Sort
Quick Sort
Selection Sort
string
Isomorphic Strings
Largest Odd Number in String
Longest Common Prefix
Reverse Words in a String
Code Scratch›take u forward›arrays

The Ultimate Array Pattern Playbook

This playbook categorizes algorithmic array patterns not by how they are coded, but by how you spot them ("The Tell") and how you execute them ("The Blueprint").


# Part 1: Two Pointers & In-Place Segregation

# 1. Opposing Pointers (The KKK-Sum Paradigm)

  • The Tell: You need to find pairs, triplets, or quadruplets that sum to a target. The problem explicitly forbids duplicate combinations, and you need to return values (not original indices).
  • The Blueprint: 1. Always sort the array first (O(Nlog⁡N)O(N \log N)O(NlogN)). 2. For KKK-Sum, use K−2K-2K−2 nested for loops to "fix" the outer elements. 3. Use left and right pointers on the remaining subarray. If sum is too small, left++. If too large, right--. 4. Skip adjacent identical elements at every loop level to prevent duplicate combinations.
  • Problems: Two Sum (Sorted), 3Sum, 4Sum.

# 2. Slow/Fast Pointer Compaction (In-Place Segregation)

  • The Tell: Modify an array in-place (O(1)O(1)O(1) space) to group, remove, or push certain elements to one side while maintaining relative order (stability) of the rest.
  • The Blueprint: slow tracks the boundary of the "completed" zone. fast explores ahead. When fast finds a valid element to keep, swap/overwrite it at the slow index, then slow++.
  • Problems: Move Zeros to End, Remove Duplicates from Sorted Array.

# 3. Synchronized Linear Scan (The Merge Pattern)

  • The Tell: You are given two or more sorted arrays and need to find their intersection, union, or merge them in O(N+M)O(N+M)O(N+M) time.
  • The Blueprint: Place pointer i at Array A and j at Array B. Compare A[i] and B[j]. The pointer on the smaller value must move forward to "catch up." If equal, process the match and move both. (If merging in-place with extra space at the end, run this backwards).
  • Problems: Intersection of 2 Arrays, Union of 2 Arrays, Merge Sorted Arrays (In-Place).

# 4. Three-Way Partitioning (Dutch National Flag)

  • The Tell: Group an array containing a strictly limited set of distinct values (e.g., 0s, 1s, 2s) in O(N)O(N)O(N) time and O(1)O(1)O(1) space.
  • The Blueprint: Use low, mid, and high pointers. mid is the explorer. If mid finds a low value, swap with low and increment both. If it finds a high value, swap with high and decrement high (do NOT increment mid yet). If it's a middle value, just mid++.
  • Problems: Sort an array of 0s, 1s, and 2s.

# 5. Strided Insertion (Stable Interleaving)

  • The Tell: Interleave elements (e.g., positive/negative) while preserving relative order in O(N)O(N)O(N) time.
  • The Blueprint: Stable, in-place interleaving in O(N)O(N)O(N) is practically impossible. Allocate a new result array. Use two "strided" pointers (e.g., pos = 0, neg = 1). Loop once, place the element, and jump the specific pointer by 2.
  • Problems: Rearrange Array Elements by Sign.

# Part 2: Subarrays & Stream Processing

# 6. Kadane’s Paradigm (Local vs. Global Extrema)

  • The Tell: Find a maximum or minimum metric (sum, product) derived from a contiguous subarray containing mixed positive and negative numbers.
  • The Blueprint: Track current_metric and max_metric. At every step, decide: "Do I extend my existing subarray with this element, or do I start a brand new subarray right here?" Update the global max immediately if the current max exceeds it.
  • Problems: Maximum Subarray (Kadane's), Maximum Product Subarray.

# 7. Right-to-Left State Carry (Reverse Traversal)

  • The Tell: An element's valid state depends on an aggregate metric (like max, sum, or visibility) of all elements to its right (its "future").
  • The Blueprint: Iterate backward (N−1N-1N−1 down to 0). Carry a state variable (e.g., max_right). Compute the answer for the current element in O(1)O(1)O(1) time by comparing it against the state variable, then update the state variable for the next element.
  • Problems: Leaders in an Array.

# 8. State Accumulator (Single-Pass Scan)

  • The Tell: Find a global extremum, verify a strict sequence, or find a simple contiguous local maximum in one unidirectional pass.
  • The Blueprint: Define state variables before the loop. Iterate once. Always identify your Reset/Early Exit Condition (e.g., return false the moment a drop is detected in a sorted check, or reset streak = 0 when a 0 is found).
  • Problems: Check if Array is Sorted, Max Consecutive Ones, Largest/Second Largest Element.

# Part 3: Mathematics, Frequency & Bit Manipulation

# 9. Mutual Cancellation (Boyer-Moore Majority Vote)

  • The Tell: Find elements appearing more than ⌊N/K⌋\lfloor N/K \rfloor⌊N/K⌋ times using strictly O(1)O(1)O(1) auxiliary space.
  • The Blueprint: Track K−1K-1K−1 candidates and K−1K-1K−1 counters. If an element matches a candidate, increment its counter. If not, and there's an empty slot, claim it. If no empty slots, decrement all counters. Mathematically, the true majorities survive. Must do a 2nd pass to verify the survivors.
  • Problems: Majority Element I (>N/2> N/2>N/2), Majority Element II (>N/3> N/3>N/3).

# 10. Bitwise Annihilation (XOR Trick)

  • The Tell: Elements are paired in exact duplicates, with exactly one missing its twin, OR you need to find a missing number in a strict sequence [0,N][0, N][0,N]. O(1)O(1)O(1) space is strictly required.
  • The Blueprint: XOR properties: X⊕X=0X \oplus X = 0X⊕X=0 and X⊕0=XX \oplus 0 = XX⊕0=X.
    • For pairs: XOR all array elements together. Duplicates annihilate to 0. The remainder is the unique element.
    • For sequences: XOR all array elements and all expected loop indices (111 to NNN). Every present number cancels its index. The remainder is the missing number.
  • Problems: Single Number, Missing Number.

# 11. Combinatorial Generation (Pascal's Suite)

  • The Tell: Problems involving combinations (nCrnCrnCr), grid paths, or Pascal's Triangle.
  • The Blueprint: * Full structure: Use DP/Tabulation (O(N2)O(N^2)O(N2)).
    • Single row/element: Use the Multiplicative Math trick in O(N)O(N)O(N) time: Current = Previous * (n - i + 1) / i. Gotcha: Use a 64-bit integer, and multiply before you divide to prevent truncation.
  • Problems: Pascal's Triangle I, II, III.

# Part 4: Array Transformations & Matrix Algorithms

# 12. In-Place Cyclic Shifts (The Reversal Algorithm)

  • The Tell: Rotate or shift an array by KKK places using O(1)O(1)O(1) auxiliary space.
  • The Blueprint: Normalize K=K(modN)K = K \pmod NK=K(modN).
    1. Reverse the first block.
    2. Reverse the second block.
    3. Reverse the entire array.
  • Problems: Left/Right Rotate Array by K, Rotate by 1.

# 13. Lexicographical Pivot (Next Permutation)

  • The Tell: Find the "next", "previous", or "KKK-th" arrangement of elements in-place.
  • The Blueprint: 1. Traverse right-to-left to find the first decreasing element (the pivot). 2. Traverse right-to-left again to find the smallest element strictly greater than the pivot. 3. Swap them. 4. Reverse everything to the right of the pivot to reset the suffix to its lowest order.
  • Problems: Next Permutation.

# 14. Matrix Linear Transformations (Composition)

  • The Tell: Rotate, reflect, or mirror a 2D matrix in-place.
  • The Blueprint: Break complex rotations into two mathematically simple reflections.
    • 90° Clockwise: Transpose (swap [i][j] with [j][i]), then Reverse each Row.
    • 90° Counter-Clockwise: Transpose, then Reverse each Column.
  • Problems: Rotate Matrix by 90 Degrees.

# 15. Boundary Shrink Simulation

  • The Tell: Non-linear traversal of a 2D grid (spiral, concentric circles).
  • The Blueprint: Maintain top, bottom, left, right boundaries. Write 4 isolated loops (Right, Down, Left, Up). After a directional sweep, shrink that specific boundary (e.g., top++). Wrap in a master while loop. Gotcha: Always re-check bounds before the Left and Up sweeps to prevent duplicate processing on non-square matrices.
  • Problems: Print Matrix in Spiral Manner.

# Part 5: Divide & Conquer

# 16. Modified Merge Sort

  • The Tell: Count pairs (i, j) that satisfy an inequality (e.g., nums[i]>nums[j]nums[i] > nums[j]nums[i]>nums[j]) where relative order matters (i<ji < ji<j). Brute force is O(N2)O(N^2)O(N2), required is O(Nlog⁡N)O(N \log N)O(NlogN).
  • The Blueprint: Hijack Merge Sort. During the merge step of the left and right sorted halves, if an element on the right is placed before an element on the left, it is out of order with every remaining element in the left half. Add the remaining size of the left half to your counter.
  • Problems: Count Inversions, Reverse Pairs.

# 🚀 Meta-Strategies to Always Keep in Mind

  1. The "Sorting Cheat Code": If the problem asks for values (not indices), and you are stuck on an O(N2)O(N^2)O(N2) algorithm, ask yourself: "Does sorting this first give me a linear pass?" Sorting takes O(Nlog⁡N)O(N \log N)O(NlogN), which is vastly better than O(N2)O(N^2)O(N2).
  2. The "Space-Time Tradeoff": If a linear search takes O(N)O(N)O(N) and a Hash Map takes O(N)O(N)O(N) space, clarify with the interviewer what the bottleneck is. (e.g., Two Sum: Hash Map vs Sorting + Two Pointers).
  3. The long Guard: Anytime a problem asks you to calculate a sum, product, or combination of numbers, default to using a 64-bit integer (long in Java/C++) for intermediate calculations before casting back to 32-bit.
  4. The Base Case Check: Before writing complex pointers, always write two lines of code to check if (nums == null || nums.length == 0) return ...;
PreviousRecently SolvedNextCount Inversions in an Array
CONTENTS
Part 1: Two Pointers & In-Place Segregation1. Opposing Pointers (The $K$-Sum Paradigm)2. Slow/Fast Pointer Compaction (In-Place Segregation)3. Synchronized Linear Scan (The Merge Pattern)4. Three-Way Partitioning (Dutch National Flag)5. Strided Insertion (Stable Interleaving)Part 2: Subarrays & Stream Processing6. Kadane’s Paradigm (Local vs. Global Extrema)7. Right-to-Left State Carry (Reverse Traversal)8. State Accumulator (Single-Pass Scan)Part 3: Mathematics, Frequency & Bit Manipulation9. Mutual Cancellation (Boyer-Moore Majority Vote)10. Bitwise Annihilation (XOR Trick)11. Combinatorial Generation (Pascal's Suite)Part 4: Array Transformations & Matrix Algorithms12. In-Place Cyclic Shifts (The Reversal Algorithm)13. Lexicographical Pivot (Next Permutation)14. Matrix Linear Transformations (Composition)15. Boundary Shrink SimulationPart 5: Divide & Conquer16. Modified Merge Sort🚀 Meta-Strategies to Always Keep in Mind