🚀 LeetCode Day Problem Solving 🚀 Day-83 📌 Problem: Find the smallest common element present in both sorted arrays 🔥 If no common element exists → return -1 🧠 Example: Input: nums1 = [1,2,3] nums2 = [2,4] ✅ Output: 2 💡 Why? Common elements are: [[2]] Smallest common element = 2 ✅ 💡 Key Insight (Very Important 🔥): 👉 Both arrays are already SORTED ⚡ So instead of brute force checking every pair ❌ We can use: #️⃣ Two Pointer Technique 🚀 ⚡ Core Idea: Maintain pointers: ✔ i for nums1 ✔ j for nums2 Case 1️⃣: If: [nums1[i] = nums2[j]] ✅ Found smallest common element immediately because arrays are sorted 🔥 Case 2️⃣: If: [nums1[i] < nums2[j]] 👉 Move i forward because smaller value cannot match future larger values Case 3️⃣: If: [nums1[i] > nums2[j]] 👉 Move j forward 🧠 Why This Works? Since arrays are sorted: ✔ Once a smaller value is skipped, it can never become useful later So we eliminate unnecessary comparisons efficiently ⚡ ⚡ Algorithm: 1️⃣ Initialize: ✔ i = 0 ✔ j = 0 2️⃣ Traverse both arrays: ✔ If equal → return value ✔ Else move pointer with smaller value 3️⃣ If traversal ends → return -1 🔥 Example Walkthrough: nums1 = [1,2,3,6] nums2 = [2,3,4,5] Step 1: Compare: [1 \text{ and } 2] 1 < 2 👉 Move first pointer Step 2: Compare: [2 \text{ and } 2] ✅ Match found Answer = 2 📊 Complexity Analysis: ⏱ Time Complexity: [O(n+m)] where n and m are array sizes 📦 Space Complexity: O(1) 🧠 What I Learned: ✔ Two Pointer Technique ✔ Efficient traversal of sorted arrays ✔ Avoiding brute force comparisons 🔥 Day 83 Completed! Sorted arrays often unlock elegant linear-time solutions 💪 #Leetcode #DSA #ProblemSolving #BitManipulation #CodingJourney #InterviewPreparation #Consistency #MilanSahoo 🚀
Find Smallest Common Element in Sorted Arrays
More Relevant Posts
-
🚀 #LeetCode Day Problem Solving 🚀 Day-63 📌 Problem: Find the smallest common element present in both sorted arrays 🔥 If no common element exists → return -1 🧠 Example: Input: nums1 = [1,2,3] nums2 = [2,4] ✅ Output: 2 💡 Why? Common elements are: [[2]] Smallest common element = 2 ✅ 💡 Key Insight (Very Important 🔥): 👉 Both arrays are already SORTED ⚡ So instead of brute force checking every pair ❌ We can use: #️⃣ Two Pointer Technique 🚀 ⚡ Core Idea: Maintain pointers: ✔ i for nums1 ✔ j for nums2 Case 1️⃣: If: [nums1[i] = nums2[j]] ✅ Found smallest common element immediately because arrays are sorted 🔥 Case 2️⃣: If: [nums1[i] < nums2[j]] 👉 Move i forward because smaller value cannot match future larger values Case 3️⃣: If: [nums1[i] > nums2[j]] 👉 Move j forward 🧠 Why This Works? Since arrays are sorted: ✔ Once a smaller value is skipped, it can never become useful later So we eliminate unnecessary comparisons efficiently ⚡ ⚡ Algorithm: 1️⃣ Initialize: ✔ i = 0 ✔ j = 0 2️⃣ Traverse both arrays: ✔ If equal → return value ✔ Else move pointer with smaller value 3️⃣ If traversal ends → return -1 🔥 Example Walkthrough: nums1 = [1,2,3,6] nums2 = [2,3,4,5] Step 1: Compare: [1 \text{ and } 2] 1 < 2 👉 Move first pointer Step 2: Compare: [2 \text{ and } 2] ✅ Match found Answer = 2 📊 Complexity Analysis: ⏱ Time Complexity: [O(n+m)] where n and m are array sizes 📦 Space Complexity: O(1) 🧠 What I Learned: ✔ Two Pointer Technique ✔ Efficient traversal of sorted arrays ✔ Avoiding brute force comparisons 🔥 Day 63 Completed! Sorted arrays often unlock elegant linear-time solutions 💪 #Leetcode #DSA #ProblemSolving #BitManipulation #CodingJourney #InterviewPreparation #Consistency
To view or add a comment, sign in
-
-
🚀 LeetCode Day Problem Solving 🚀 Day-79 📌 Problem: Find the minimum element in a sorted rotated array 🔥 👉 Array was originally sorted in ascending order 👉 Then rotated multiple times ⚡ Need solution in: [O(\log n)] 🧠 Example: Input: nums = [4,5,6,7,0,1,2] ✅ Output: 0 💡 Key Insight (Very Important 🔥): 👉 Rotated sorted array contains TWO sorted halves Example: [ [4,5,6,7 ; | ; 0,1,2] ] Minimum element is exactly where rotation happened ⚡ 🔥 Core Idea: Use Binary Search 🚀 Because: ✔ One half will always remain sorted ✔ Minimum lies in the unsorted / smaller half ⚡ Important Observation: Compare middle element with rightmost element Case 1️⃣: If: [nums[mid] > nums[right]] 👉 Minimum lies on RIGHT side because rotation point is there Case 2️⃣: If: [nums[mid] < nums[right]] 👉 Minimum lies on LEFT side (including mid) 🧠 Why Binary Search Works? The array structure preserves sorted properties even after rotation So we can eliminate half the search space every step 🔥 ⚡ Algorithm: 1️⃣ Initialize: ✔ left = 0 ✔ right = n-1 2️⃣ While left < right: ✔ Find mid ✔ Compare nums[mid] with nums[right] ✔ Move search space accordingly 3️⃣ Final answer = nums[left] 🔥 Example Walkthrough: [4,5,6,7,0,1,2] Step 1: mid = 7 [7 > 2] 👉 Move right half Step 2: Search in [0,1,2] Eventually reach: ✅ 0 📊 Complexity Analysis: ⏱ Time Complexity: O(log n) 📦 Space Complexity: O(1) 🧠 What I Learned: ✔ Modified Binary Search ✔ Rotated array properties ✔ Efficient searching in partially sorted arrays 🔥 Day 79 Completed! Binary Search becomes extremely powerful with the right observation 💪 #Leetcode #DSA #ProblemSolving #BitManipulation #CodingJourney #InterviewPreparation #Consistency #MilanSahoo 🚀
To view or add a comment, sign in
-
-
🚀 #LeetCode Day Problem Solving 🚀 Day-59 📌 Problem: Find the minimum element in a sorted rotated array 🔥 👉 Array was originally sorted in ascending order 👉 Then rotated multiple times ⚡ Need solution in: [O(\log n)] 🧠 Example: Input: nums = [4,5,6,7,0,1,2] ✅ Output: 0 💡 Key Insight (Very Important 🔥): 👉 Rotated sorted array contains TWO sorted halves Example: [ [4,5,6,7 ; | ; 0,1,2] ] Minimum element is exactly where rotation happened ⚡ 🔥 Core Idea: Use Binary Search 🚀 Because: ✔ One half will always remain sorted ✔ Minimum lies in the unsorted / smaller half ⚡ Important Observation: Compare middle element with rightmost element Case 1️⃣: If: [nums[mid] > nums[right]] 👉 Minimum lies on RIGHT side because rotation point is there Case 2️⃣: If: [nums[mid] < nums[right]] 👉 Minimum lies on LEFT side (including mid) 🧠 Why Binary Search Works? The array structure preserves sorted properties even after rotation So we can eliminate half the search space every step 🔥 ⚡ Algorithm: 1️⃣ Initialize: ✔ left = 0 ✔ right = n-1 2️⃣ While left < right: ✔ Find mid ✔ Compare nums[mid] with nums[right] ✔ Move search space accordingly 3️⃣ Final answer = nums[left] 🔥 Example Walkthrough: [4,5,6,7,0,1,2] Step 1: mid = 7 [7 > 2] 👉 Move right half Step 2: Search in [0,1,2] Eventually reach: ✅ 0 📊 Complexity Analysis: ⏱ Time Complexity: O(log n) 📦 Space Complexity: O(1) 🧠 What I Learned: ✔ Modified Binary Search ✔ Rotated array properties ✔ Efficient searching in partially sorted arrays 🔥 Day 59 Completed! Binary Search becomes extremely powerful with the right observation 💪 #Leetcode #DSA #ProblemSolving #BitManipulation #CodingJourney #InterviewPreparation #Consistency
To view or add a comment, sign in
-
-
🚀 #LeetCode Day Problem Solving 🚀 Day-67 📌 Problem: Given an array arr[] and an integer d. From index i, you can jump: 👉 Left or Right up to distance d But only if: ✔ arr[i] > arr[j] ✔ Every element between i and j is also smaller than arr[i] Find the maximum number of indices you can visit. 🧠 Example: Input: arr = [6,4,14,6,8,13,9,7,10,6,12] d = 2 ✅ Output: 4 💡 Key Insight (Very Important 🔥): 👉 From every index, we try all valid jumps But many paths repeat again and again ❌ So we use: ✔ DFS ✔ Memoization (DP) to store already computed answers. ⚡ Core Idea: Let: dp[i] = maximum indices visitable starting from index i For every index: ✔ Try jumping left ✔ Try jumping right ✔ Stop when: distance exceeds d a higher/equal element blocks the path 🔥 Important Observation: You can jump only to strictly smaller values. And once a larger/equal value appears in between: ❌ further jumping in that direction is impossible. ⚡ How DFS Works: From each index: 1️⃣ Explore all valid left jumps 2️⃣ Explore all valid right jumps 3️⃣ Take maximum path length among them 4️⃣ Store result in DP array 🧠 Example Flow: Starting from index 10 → value 12 Possible path: 10 → 8 → 6 → 7 Values: 12 → 10 → 9 → 7 Total visited indices = 4 ⚡ Why Memoization Helps? Without memoization: ❌ Same states recomputed many times With memoization: ✔ Each index solved once only Huge optimization 🚀 📊 Complexity Analysis: ⏱ Time Complexity: O(n × d) 📦 Space Complexity: O(n) 🧠 What I Learned: ✔ DFS + DP optimization ✔ Recursive state caching ✔ Valid jump constraints handling ✔ Breaking conditions in traversal ✔ Graph-style thinking on arrays ✅ Day 67 Completed 🚀 Improving in Dynamic Programming & DFS Problems 💪 #Leetcode #DSA #ProblemSolving #BitManipulation #CodingJourney #InterviewPreparation #Consistency
To view or add a comment, sign in
-
-
🚀 LeetCode Day Problem Solving 🚀 Day-77 📌 Problem: Make the array complementary with minimum moves 🔥 👉 For every index i: [ nums[i] + nums[n-1-i] ] must become the SAME value ⚡ 🧠 Example: Input: nums = [1,2,4,3] limit = 4 ✅ Output: 1 💡 Key Insight (Very Important 🔥): 👉 We process elements in PAIRS: [ (nums[i], nums[n-1-i]) ] Each pair contributes to a target sum ⚡ Brute Force Problem ❌ Trying every target sum for every pair would be too slow: O(n × limit) 🔥 Smart Observation: For one pair (a,b): 0 moves needed: If target sum = [ a+b] 1 move needed: We can change one element Possible sums become: [ [min(a,b)+1 ; , ; max(a,b)+limit] ] 2 moves needed: All remaining sums ⚡ Core Idea: 👉 Use Difference Array / Prefix Sum Trick Instead of updating every target individually 🧠 Transition Logic: Initially assume: ✔ Every target sum needs 2 moves Then optimize ranges where: ✔ Only 1 move needed ✔ 0 moves needed 🔥 Difference Array Updates: For pair (a,b): Let: [ low = min(a,b)+1 ] [ high = max(a,b)+limit ] [ sum = a+b ] Then: ✔ [low, high] → at most 1 move ✔ sum → 0 moves ⚡ Algorithm: 1️⃣ Process all mirrored pairs 2️⃣ Use difference array for range updates 3️⃣ Compute prefix sums 4️⃣ Find minimum moves among all target sums 📊 Complexity Analysis: ⏱ Time Complexity: O(n + limit) 📦 Space Complexity: O(limit) 🧠 What I Learned: ✔ Difference array optimization ✔ Prefix sum range processing ✔ Pair contribution analysis 🔥 Day 77 Completed! Hard problems become easier after identifying contribution ranges 💪 #Leetcode #DSA #ProblemSolving #BitManipulation #CodingJourney #InterviewPreparation #Consistency #MilanSahoo 🚀
To view or add a comment, sign in
-
-
🚀 #LeetCode Day Problem Solving 🚀 Day-57 📌 Problem: Make the array complementary with minimum moves 🔥 👉 For every index i: [ nums[i] + nums[n-1-i] ] must become the SAME value ⚡ 🧠 Example: Input: nums = [1,2,4,3] limit = 4 ✅ Output: 1 💡 Key Insight (Very Important 🔥): 👉 We process elements in PAIRS: [ (nums[i], nums[n-1-i]) ] Each pair contributes to a target sum ⚡ Brute Force Problem ❌ Trying every target sum for every pair would be too slow: O(n × limit) 🔥 Smart Observation: For one pair (a,b): 0 moves needed: If target sum = [ a+b] 1 move needed: We can change one element Possible sums become: [ [min(a,b)+1 ; , ; max(a,b)+limit] ] 2 moves needed: All remaining sums ⚡ Core Idea: 👉 Use Difference Array / Prefix Sum Trick Instead of updating every target individually 🧠 Transition Logic: Initially assume: ✔ Every target sum needs 2 moves Then optimize ranges where: ✔ Only 1 move needed ✔ 0 moves needed 🔥 Difference Array Updates: For pair (a,b): Let: [ low = min(a,b)+1 ] [ high = max(a,b)+limit ] [ sum = a+b ] Then: ✔ [low, high] → at most 1 move ✔ sum → 0 moves ⚡ Algorithm: 1️⃣ Process all mirrored pairs 2️⃣ Use difference array for range updates 3️⃣ Compute prefix sums 4️⃣ Find minimum moves among all target sums 📊 Complexity Analysis: ⏱ Time Complexity: O(n + limit) 📦 Space Complexity: O(limit) 🧠 What I Learned: ✔ Difference array optimization ✔ Prefix sum range processing ✔ Pair contribution analysis 🔥 Day 57 Completed! Hard problems become easier after identifying contribution ranges 💪 #Leetcode #DSA #ProblemSolving #BitManipulation #CodingJourney #InterviewPreparation #Consistency
To view or add a comment, sign in
-
-
Most people solve DSA problems. Very few actually understand the pattern behind them. Today, I focused on mastering one such powerful pattern — Two Pointers. Instead of jumping across topics, I went deep and solved these problems using optimal approaches: 🔹 Move Zeroes (LeetCode 283) 🔹 Two Sum (LeetCode 1) 🔹 3Sum (LeetCode 15) 🔹 Sort Colors – Dutch National Flag Algorithm (LeetCode 75) 🔹 Container With Most Water (LeetCode 11) 💡 What actually improved today: Turning brute force thinking into optimized solutions Knowing exactly when to use left-right vs slow-fast pointers Handling tricky edge cases like duplicates and boundaries Writing cleaner, more interview-ready code This is what real progress looks like — not just solving problems, but recognizing patterns. One step closer to getting better at problem solving, every single day. #DSA #LeetCode #CodingJourney #TwoPointers #100DaysOfCode #SoftwareEngineering
To view or add a comment, sign in
-
🚀 LeetCode Day Problem Solving 🚀 Day-67 📌 Problem: Check if string s can become goal after some number of left shifts 👉 One shift = move first character to the end 🧠 Example: Input: s = "abcde" goal = "cdeab" ✅ Output: true 💡 Key Insight (Very Important 🔥): ✔ After multiple shifts, s forms all its rotations 👉 Instead of simulating shifts ❌ 👉 Use a trick ✅ ⚡ Core Idea: 👉 Concatenate string with itself s + s = "abcdeabcde" ✔ Now check: 👉 If goal is a substring of (s + s) ⚡ Condition: ✔ Length must be equal ✔ goal must exist in (s + s) 🔥 Why it works? ✔ All cyclic rotations of s are present inside s + s Example: abcdeabcde contains: bcdea cdeab deabc eabcd ⚡ Algorithm: 1️⃣ If len(s) != len(goal) → return false 2️⃣ Check: 👉 (s + s).contains(goal) 3️⃣ If yes → true, else → false 📊 Complexity Analysis: ⏱ Time Complexity: O(n) 📦 Space Complexity: O(n) 🧠 What I Learned: ✔ String rotation trick ✔ Avoid brute force shifting ✔ Smart use of concatenation ✅ Day 67 Completed 🚀 Improving in String Manipulation Tricks 💪 #Leetcode #coding #learning #CodingChallenge #ProblemSolving #DSA #Math #Simulation #Digits #ProblemSolving #BitManipulation #CodingJourney #InterviewPreparation #Consistency #MilanSahoo 🚀
To view or add a comment, sign in
-
-
🚀 Day 54 of 100 Days LeetCode Challenge Problem: Sum of Distances Day 54 is a smart prefix sum + hashmap optimization problem 🔥 💡 Key Insight: For each index i, we need: 👉 Sum of distances to all indices having the same value Brute force would be: O(n²) ❌ Too slow 🔍 Core Approach (Optimized): 1️⃣ Group Indices by Value Use a HashMap: value → list of indices Example: 1 → [0, 3, 5] 2️⃣ Use Prefix Sums For each index list: Compute prefix sums of positions 👉 This allows distance calculation in: O(1) per index 3️⃣ Distance Formula For position idx inside a group: 👉 Left contribution: idx * countLeft - sumLeft 👉 Right contribution: sumRight - idx * countRight Add both for total distance ✅ 💡 Why This Works: Instead of recalculating distances repeatedly: ⚡ Prefix sums reuse previous computations efficiently 🔥 Time Complexity: O(n) Efficient and scalable 🚀 🔥 What I Learned Today: Prefix sums are powerful beyond arrays Grouping similar values simplifies complex problems Math-based optimization can remove nested loops entirely 📈 Challenge Progress: Day 54/100 ✅ More than halfway done! LeetCode, Prefix Sum, HashMap, Arrays, Optimization, Distance Calculation, DSA Practice, Problem Solving #100DaysOfCode #LeetCode #DSA #CodingChallenge #PrefixSum #Hashing #Arrays #ProblemSolving #TechJourney #ProgrammerLife #SoftwareDeveloper #CodingLife #LearnToCode #Developers #Consistency #GrowthMindset #InterviewPrep
To view or add a comment, sign in
-
-
🚀 #LeetCode Day Problem Solving 🚀 Day-55 📌 Problem: Separate the digits of every number in the array and store them in the same order 🔢 🧠 Example: Input: nums = [13,25,83,77] ✅ Output: [1,3,2,5,8,3,7,7] 💡 Key Insight (Very Important 🔥): 👉 We simply need to extract every digit from each number ✔ Digits must remain in original order ⚡ Core Idea: For every number: 1️⃣ Convert number into digits 2️⃣ Append each digit into answer array 🔥 Important Observation: 👉 Using modulo directly gives digits in reverse order ❌ Example: 123 % 10 → 3 first So: ✔ Either reverse afterward ✔ Or use string conversion ⚡ Efficient Approach: 👉 Convert each integer to string Then: ✔ Traverse every character ✔ Convert back to integer digit ✔ Push into answer array 🧠 Why it works? ✔ String traversal preserves digit order automatically Example: "83" → '8', '3' ⚡ Algorithm: 1️⃣ Initialize empty answer array 2️⃣ For every number in nums: ✔ Convert to string ✔ Traverse characters ✔ Append digits to answer 3️⃣ Return answer array 📊 Complexity Analysis: ⏱ Time Complexity: O(total digits) 📦 Space Complexity: O(total digits) 🧠 What I Learned: ✔ Digit extraction techniques ✔ Order preservation tricks ✔ Simple array construction problems 🔥 Day 55 Completed! Even simple problems improve implementation skills 💪 hashtag #Leetcode #DSA #ProblemSolving #BitManipulation #CodingJourney #InterviewPreparation #Consistency
To view or add a comment, sign in
-