🚀 LeetCode Day Problem Solving 🚀 Day-53 📌 Problem: You are given two non-increasing arrays nums1 and nums2. A pair (i, j) is valid if: ✔ i ≤ j ✔ nums1[i] ≤ nums2[j] 👉 Distance = j - i Your task is to find the maximum distance among all valid pairs. 🧠 Example: Input: nums1 = [55,30,5,4,2] nums2 = [100,20,10,10,5] ✅ Output: 2 📖 Explanation: Valid pairs include (2,4) → distance = 4 - 2 = 2 👉 This is the maximum possible distance. 💡 Key Insight: ✔ Arrays are already sorted (non-increasing) ✔ Use Two Pointer Technique 👉 Start with i = 0, j = 0 If nums1[i] ≤ nums2[j] → valid pair → update answer → move j++ Else → move i++ ⚡ This avoids checking all pairs (which would be slow). 📊 Complexity Analysis: ⏱ Time Complexity: O(n + m) 📦 Space Complexity: O(1) 🧠 What I Learned: ✔ Efficient use of Two Pointers ✔ Leveraging sorted properties ✔ Avoiding brute force with smart traversal ✅ Day 53 Completed 🚀 Leveling up in Arrays + Greedy + Two Pointers 💪 #Leetcode #DSA #ProblemSolving #BitManipulation #CodingJourney #InterviewPreparation #Consistency #MilanSahoo 🚀
Max Distance in Sorted Arrays with Two Pointers
More Relevant Posts
-
🚀 #LeetCode Day Problem Solving 🚀 Day-33 📌 Problem: You are given two non-increasing arrays nums1 and nums2. A pair (i, j) is valid if: ✔ i ≤ j ✔ nums1[i] ≤ nums2[j] 👉 Distance = j - i Your task is to find the maximum distance among all valid pairs. 🧠 Example: Input: nums1 = [55,30,5,4,2] nums2 = [100,20,10,10,5] ✅ Output: 2 📖 Explanation: Valid pairs include (2,4) → distance = 4 - 2 = 2 👉 This is the maximum possible distance. 💡 Key Insight: ✔ Arrays are already sorted (non-increasing) ✔ Use Two Pointer Technique 👉 Start with i = 0, j = 0 If nums1[i] ≤ nums2[j] → valid pair → update answer → move j++ Else → move i++ ⚡ This avoids checking all pairs (which would be slow). 📊 Complexity Analysis: ⏱ Time Complexity: O(n + m) 📦 Space Complexity: O(1) 🧠 What I Learned: ✔ Efficient use of Two Pointers ✔ Leveraging sorted properties ✔ Avoiding brute force with smart traversal ✅ Day 33 Completed 🚀 Leveling up in Arrays + Greedy + Two Pointers 💪 #Leetcode #DSA #ProblemSolving #BitManipulation #CodingJourney #InterviewPreparation #Consistency
To view or add a comment, sign in
-
-
🚀 LeetCode Day Problem Solving 🚀 Day-45 📌 Problem: Given an array nums[], find a good tuple (i, j, k) such that: ✔ nums[i] == nums[j] == nums[k] ✔ i < j < k 👉 Distance of tuple = |i - j| + |j - k| + |k - i| 🎯 Return the minimum possible distance among all such tuples ❌ If no valid tuple exists → return -1 🧠 Example: Input: nums = [1,2,1,1,3] ✅ Output: 6 📖 Explanation: Tuple → (0, 2, 3) Distance = 2 + 1 + 3 = 6 💡 Key Insight: ✔ For same values, indices matter ✔ Distance simplifies to: 👉 2 * (k - i) (since i < j < k) ✔ So we just need: 👉 Closest 3 indices of same number ⚡ Optimized Approach: ✔ Use HashMap / Array of lists to store indices ✔ For each value: 👉 Traverse indices and check every consecutive triplet 📊 Complexity Analysis: ⏱ Time Complexity: O(n) 📦 Space Complexity: O(n) 🧠 What I Learned: ✔ Pattern observation simplifies formula ✔ Grouping indices using hashmap ✔ Optimization by avoiding brute force O(n³) ✅ Day 45 Completed 🚀 Leveling up in Arrays + Optimization + Math Tricks 💪 #Leetcode #DSA #ProblemSolving #BitManipulation #CodingJourney #InterviewPreparation #Consistency #MilanSahoo 🚀
To view or add a comment, sign in
-
-
🚀 #LeetCode Day Problem Solving 🚀 Day-26 📌 Problem: Given an array nums[], find a good tuple (i, j, k) such that: ✔ nums[i] == nums[j] == nums[k] ✔ i < j < k 👉 Distance of tuple = |i - j| + |j - k| + |k - i| 🎯 Return the minimum possible distance among all such tuples ❌ If no valid tuple exists → return -1 🧠 Example: Input: nums = [1,2,1,1,3] ✅ Output: 6 📖 Explanation: Tuple → (0, 2, 3) Distance = 2 + 1 + 3 = 6 💡 Key Insight: ✔ For same values, indices matter ✔ Distance simplifies to: 👉 2 * (k - i) (since i < j < k) ✔ So we just need: 👉 Closest 3 indices of same number ⚡ Optimized Approach: ✔ Use HashMap / Array of lists to store indices ✔ For each value: 👉 Traverse indices and check every consecutive triplet 📊 Complexity Analysis: ⏱ Time Complexity: O(n) 📦 Space Complexity: O(n) 🧠 What I Learned: ✔ Pattern observation simplifies formula ✔ Grouping indices using hashmap ✔ Optimization by avoiding brute force O(n³) ✅ Day 26 Completed 🚀 Leveling up in Arrays + Optimization + Math Tricks 💪 #Leetcode #DSA #ProblemSolving #BitManipulation #CodingJourney #InterviewPreparation #Consistency
To view or add a comment, sign in
-
-
🚀 #LeetCode Day Problem Solving 🚀 Day-34 📌 Problem: You are given an array colors[] where each element represents the color of a house. 🏠 Your task is to find the maximum distance between two houses having different colors. 👉 Distance = |i - j| 🧠 Example: Input: colors = [1,1,1,6,1,1,1] ✅ Output: 3 📖 Explanation: ✔ House 0 → color 1 ✔ House 3 → color 6 👉 Distance = |0 - 3| = 3 (maximum possible) 💡 Key Insight: ✔ We don’t need to check all pairs ❌ ✔ Just compare with first and last elements 👉 For every index i: If colors[i] != colors[0] → update answer with i If colors[i] != colors[n-1] → update answer with n-1-i 🔥 This guarantees the maximum distance ⚡ Approach: ✔ Traverse once from left to right ✔ Track max distance using first & last color ✔ No nested loops needed 📊 Complexity Analysis: ⏱ Time Complexity: O(n) 📦 Space Complexity: O(1) 🧠 What I Learned: ✔ Smart observation reduces brute force ✔ Edge comparison trick 💡 ✔ Optimization in array problems ✅ Day 34 Completed 🚀 Leveling up in Array Optimization + Greedy Thinking 💪 #Leetcode #DSA #ProblemSolving #BitManipulation #CodingJourney #InterviewPreparation #Consistency
To view or add a comment, sign in
-
-
🚀 #LeetCode Day Problem Solving 🚀 Day-29 📌 Problem: Given a circular array of strings and a target string, find the minimum distance needed to reach the target starting from a given index. You can move: ✔ One step forward (right) ✔ One step backward (left) Since the array is circular, moving beyond ends will wrap around. 🧠 Example: Input: words = ["hello","i","am","leetcode","hello"] target = "hello", startIndex = 1 ✅ Output: 1 📖 Explanation: From index 1, shortest way is: ➡ Move left 1 step → reach "hello" 💡 Key Insight: ✔ Since array is circular, distance = 👉 min(|i - start|, n - |i - start|) ✔ Check all indices where words[i] == target ✔ Take the minimum distance among them ✔ If target not found → return -1 ⚡ Approach: Traverse the array For every match with target: Compute circular distance Return the minimum 📊 Complexity Analysis: ⏱ Time Complexity: O(n) 📦 Space Complexity: O(1) 🧠 What I Learned: ✔ Handling circular arrays efficiently ✔ Optimizing distance using modular thinking ✔ Simple traversal with smart math 💡 ✅ Day 29 Completed 🚀 Sharpening skills in Arrays + Optimization Problems 💪 #Leetcode #DSA #ProblemSolving #BitManipulation #CodingJourney #InterviewPreparation #Consistency
To view or add a comment, sign in
-
-
🚀 LeetCode Day Problem Solving 🚀 Day-49 📌 Problem: Given a circular array of strings and a target string, find the minimum distance needed to reach the target starting from a given index. You can move: ✔ One step forward (right) ✔ One step backward (left) Since the array is circular, moving beyond ends will wrap around. 🧠 Example: Input: words = ["hello","i","am","leetcode","hello"] target = "hello", startIndex = 1 ✅ Output: 1 📖 Explanation: From index 1, shortest way is: ➡ Move left 1 step → reach "hello" 💡 Key Insight: ✔ Since array is circular, distance = 👉 min(|i - start|, n - |i - start|) ✔ Check all indices where words[i] == target ✔ Take the minimum distance among them ✔ If target not found → return -1 ⚡ Approach: Traverse the array For every match with target: Compute circular distance Return the minimum 📊 Complexity Analysis: ⏱ Time Complexity: O(n) 📦 Space Complexity: O(1) 🧠 What I Learned: ✔ Handling circular arrays efficiently ✔ Optimizing distance using modular thinking ✔ Simple traversal with smart math 💡 ✅ Day 49 Completed 🚀 Sharpening skills in Arrays + Optimization Problems 💪 #Leetcode #DSA #ProblemSolving #BitManipulation #CodingJourney #InterviewPreparation #Consistency #MilanSahoo 🚀
To view or add a comment, sign in
-
-
🚀 #LeetCode Day Problem Solving 🚀 Day-35 📌 Problem: You are given two arrays source[] and target[], along with a list of allowed swaps between indices. 👉 You can swap elements in source[] using the allowed pairs any number of times. 🎯 Goal: Minimize the Hamming Distance between source and target. 📌 Hamming Distance = Number of positions where source[i] ≠ target[i] 🛠 Key Idea: ✔ Swaps create connected components (groups) ✔ Within a group, elements can be rearranged freely ✔ So, match as many elements as possible with target 🧠 Approach: ✔ Use Disjoint Set (Union-Find) to group indices ✔ For each group: 👉 Count frequency of elements in source 👉 Try to match with target 👉 Reduce mismatches 💡 Example: Input: source = [1,2,3,4] target = [2,1,4,5] allowedSwaps = [[0,1],[2,3]] ✅ After swaps → [2,1,4,3] ❌ Only 1 mismatch → Answer = 1 📊 Complexity Analysis: ⏱ Time Complexity: O(n α(n)) (Union-Find) 📦 Space Complexity: O(n) 🧠 What I Learned: ✔ Power of Union-Find (DSU) in grouping ✔ Converting swap problems into component problems ✔ Frequency matching for minimizing mismatches ✅ Day 35 Completed 🚀 Improving in Graph + DSU + Greedy Thinking 💪 #Leetcode #DSA #ProblemSolving #BitManipulation #CodingJourney #InterviewPreparation #Consistency
To view or add a comment, sign in
-
-
🚀 Day 14 — Max Consecutive Ones Continuing my journey of improving problem-solving skills with consistency and discipline — one problem every day. Today’s problem was about identifying patterns in sequences — simple logic, but very useful in many real-world scenarios. 🧩 Problem Solved: • Max Consecutive Ones (LeetCode #485) 📚 Topic: Arrays + Traversal 💡 Key Insight: We can track consecutive 1’s using a counter and reset it whenever we encounter a 0 — keeping track of the maximum along the way. ⚡ Approach: • Initialize count = 0 and max_count = 0 • Traverse the array • If element is 1 → increment count • Update max_count • If element is 0 → reset count to 0 • Return max_count 🎯 Takeaway: Tracking patterns during traversal is a powerful technique for solving sequence-based problems. ⏱ Complexity: • Time → O(n) • Space → O(1) 💻 Example: Input → [1,1,0,1,1,1] → Output → 3 Input → [1,0,1,1,0,1] → Output → 2 Small patterns reveal big insights 🚀 #ProblemSolving #LeetCode #Arrays #CodingJourney #100DaysOfCode #Consistency #LearningInPublic
To view or add a comment, sign in
-
-
🚀 LeetCode Day Problem Solving 🚀 Day-55 📌 Problem: You are given two arrays source[] and target[], along with a list of allowed swaps between indices. 👉 You can swap elements in source[] using the allowed pairs any number of times. 🎯 Goal: Minimize the Hamming Distance between source and target. 📌 Hamming Distance = Number of positions where source[i] ≠ target[i] 🛠 Key Idea: ✔ Swaps create connected components (groups) ✔ Within a group, elements can be rearranged freely ✔ So, match as many elements as possible with target 🧠 Approach: ✔ Use Disjoint Set (Union-Find) to group indices ✔ For each group: 👉 Count frequency of elements in source 👉 Try to match with target 👉 Reduce mismatches 💡 Example: Input: source = [1,2,3,4] target = [2,1,4,5] allowedSwaps = [[0,1],[2,3]] ✅ After swaps → [2,1,4,3] ❌ Only 1 mismatch → Answer = 1 📊 Complexity Analysis: ⏱ Time Complexity: O(n α(n)) (Union-Find) 📦 Space Complexity: O(n) 🧠 What I Learned: ✔ Power of Union-Find (DSU) in grouping ✔ Converting swap problems into component problems ✔ Frequency matching for minimizing mismatches ✅ Day 55 Completed 🚀 Improving in Graph + DSU + Greedy Thinking 💪 #Leetcode #DSA #ProblemSolving #BitManipulation #CodingJourney #InterviewPreparation #Consistency #MilanSahoo 🚀
To view or add a comment, sign in
-
-
🚀 #LeetCode Day Problem Solving 🚀 Day-30 📌 Problem: You are given a circular array nums and an array queries. For each query index i, find the minimum circular distance to another index j such that: ✔ nums[j] == nums[queries[i]] ✔ If no such index exists → return -1 🧠 Example: Input: nums = [1,3,1,4,1,3,2] queries = [0,3,5] ✅ Output: [2, -1, 3] 📖 Explanation: Query 0 → value = 1 → nearest same value at index 2 → distance = 2 Query 1 → value = 4 → no duplicate → -1 Query 2 → value = 3 → nearest at index 1 (circular path) → distance = 3 💡 Key Insight: ✔ Since array is circular, distance = 👉 min(|i - j|, n - |i - j|) ✔ Store indices of each value using a map (value → list of indices) ✔ For each query: 👉 Use binary search to find nearest index efficiently 📊 Complexity Analysis: ⏱ Time Complexity: O(n + q log n) 📦 Space Complexity: O(n) 🧠 What I Learned: ✔ Handling circular arrays smartly ✔ Using binary search on index lists ✔ Optimizing brute force to efficient lookup ✅ Day 30 Completed 🚀 Leveling up in Arrays + Binary Search + Optimization 💪 #Leetcode #DSA #ProblemSolving #BitManipulation #CodingJourney #InterviewPreparation #Consistency
To view or add a comment, sign in
-
Explore content categories
- Career
- Productivity
- Finance
- Soft Skills & Emotional Intelligence
- Project Management
- Education
- Technology
- Leadership
- Ecommerce
- User Experience
- Recruitment & HR
- Customer Experience
- Real Estate
- Marketing
- Sales
- Retail & Merchandising
- Science
- Supply Chain Management
- Future Of Work
- Consulting
- Writing
- Economics
- Artificial Intelligence
- Employee Experience
- Workplace Trends
- Fundraising
- Networking
- Corporate Social Responsibility
- Negotiation
- Communication
- Engineering
- Hospitality & Tourism
- Business Strategy
- Change Management
- Organizational Culture
- Design
- Innovation
- Event Planning
- Training & Development