🚀 Day 38 of my #100DaysOfCode Journey Today, I solved the LeetCode problem: Maximum Product of Two Elements in an Array. Problem Insight: Given an integer array, the goal is to find two elements such that: (nums[i] - 1) * (nums[j] - 1) is maximized Approach: • First, sort the array using Arrays.sort() • Use two nested loops to check all possible pairs • For each pair, calculate → (nums[i] - 1) * (nums[j] - 1) • Keep track of the maximum product Time Complexity: • O(n²) — due to nested loops Space Complexity: • O(1) — no extra space used Key Learnings: • Understanding operator precedence is very important in expressions • Sorting helps in simplifying many problems • Even simple problems can have optimized solutions beyond brute force Takeaway: Brute force helps in understanding the problem deeply, but optimization (like using the two largest elements directly) makes the solution efficient 🚀 #DSA #Java #LeetCode #100DaysOfCode #CodingJourney #ProblemSolving #Arrays
Solving LeetCode Maximum Product of Two Elements in Array
More Relevant Posts
-
🚀 Day 39 of my #100DaysOfCode Journey Today, I solved the LeetCode problem: Concatenation of Array Problem Insight: Given an integer array nums, the goal is to create a new array by concatenating the array with itself. Approach: • Created a new array of size 2 * nums.length • Used a single loop to iterate through the array • Stored elements at two positions: - result[i] = nums[i] - result[i + nums.length] = nums[i] • This avoids using extra loops and keeps the solution efficient Time Complexity: • O(n) — only one traversal required Space Complexity: • O(n) — new array is created Key Learnings: • Efficient index handling can simplify problems • Avoid unnecessary loops for better performance • Strong fundamentals make simple problems powerful Takeaway: Smart thinking beats brute force — even simple problems can be solved in an optimal and elegant way . #DSA #Java #LeetCode #100DaysOfCode #CodingJourney #ProblemSolving #Arrays
To view or add a comment, sign in
-
-
🚀 Day 43 of my #100DaysOfCode Journey Today, I solved the LeetCode problem: Sum of Unique Elements Problem Insight: Find elements in an array that appear exactly once and calculate their total sum. Approach: • Used a frequency array to count occurrences of each number • Traversed the array to build frequency • Added only those elements to sum whose frequency is exactly 1 Time Complexity: • O(n) Space Complexity: • O(1) (fixed size array used for constraints) Key Learnings: • Frequency array is faster than HashMap when range is fixed • Two-pass approach makes logic clear and simple • Always check constraints before choosing data structure Takeaway: Right data structure makes the solution simple and efficient 🚀 #DSA #Java #LeetCode #100DaysOfCode #CodingJourney #ProblemSolving
To view or add a comment, sign in
-
-
Day 90 of #100DaysOfCode – Unique Binary Search Trees II Today’s problem was all about generating all structurally unique BSTs for values from 1 to n. At first glance, it feels tricky because it's not just counting trees — we actually need to build every possible tree structure. 💡 Key Insight: Pick each number i as the root Recursively build: Left subtree from [1 ... i-1] Right subtree from [i+1 ... n] Combine every left & right subtree pair 🌳 This is a classic Recursion + Backtracking problem and is closely related to Catalan Numbers. 📌 Example: For n = 3, we get 5 unique BSTs ⚡ What I learned today: How recursion can generate combinations of structures Importance of base case (start > end → null) Combining subproblems effectively 💻 Time Complexity: Approximately O(4ⁿ / √n) Consistency is key — 90 days strong and still going 💪 Next target: 💯 #DSA #Java #LeetCode #Recursion #BinaryTree #CodingJourney #100DaysOfCode
To view or add a comment, sign in
-
-
🚀 Day 48 of my #100DaysOfCode Journey Today, I solved the LeetCode problem: Sort Array By Parity Problem Insight: Rearrange the array so that all even numbers come before odd numbers. The order of elements doesn’t matter. Approach: • Used the two-pointer technique (partition logic) • Maintained a pointer j to track the position for even numbers • Traversed the array using i • Whenever an even number is found, swapped it with index j and incremented j • This ensures all even elements move to the front in a single pass Time Complexity: O(n) Space Complexity: O(1) (in-place solution) Key Learnings: • Two-pointer technique is very useful for array partitioning problems • Swapping helps avoid extra space usage • This pattern is similar to problems like moving zeros or segregating positives/negatives Takeaway: Simple logic + optimal approach = clean and efficient solution. Consistency is making these patterns easier to recognize! #DSA #Java #LeetCode #100DaysOfCode #CodingJourney
To view or add a comment, sign in
-
-
🔥 Day 60 / 100 – LeetCode Challenge ✅ Solved: 160. Intersection of Two Linked Lists Today’s problem was all about understanding pointer behavior and memory references in linked lists. 💡 Key Insight: Instead of comparing values, we compare node references. If two linked lists intersect, they will share the same tail nodes. 🚀 Approach Used (Optimal): Two pointers (pA, pB) Traverse both lists When one reaches the end, switch to the other list They eventually meet at the intersection node (or null) 🧠 Why it works: Both pointers travel equal distance → LengthA + LengthB, aligning perfectly without extra space. ⏱ Complexity: Time: O(m + n) Space: O(1) 💻 Result: ✔️ Accepted (41/41 test cases) ⚡ Runtime: 1 ms (Beats 99.94%) 📌 Takeaway: Sometimes the smartest solution is not about extra data structures, but about clever traversal. #Day60 #100DaysOfCode #LeetCode #Java #DSA #LinkedList #CodingJourney
To view or add a comment, sign in
-
-
🚀 Day 34 of my #100DaysOfCode Journey Today, I solved the LeetCode problem: Maximum Product of Two Digits Problem Insight: Given a number, find the maximum product of any two digits in it. Approach: • Traverse the number digit by digit • Keep track of the two largest digits (large1 and large2) • Multiply the two largest digits to get the answer • No extra data structures required, simple comparison logic Time Complexity: • O(log n) — proportional to the number of digits Space Complexity: • O(1) — only a few variables used Key Learnings: • Simple comparison logic can replace sorting or extra arrays • Breaking down the number digit by digit is often sufficient • Edge cases like single-digit numbers should be considered Takeaway: Finding the largest values while iterating can simplify problems and reduce complexity. #DSA #Java #LeetCode #100DaysOfCode #CodingJourney #ProblemSolving #MathTricks
To view or add a comment, sign in
-
-
💡 Day 55 of LeetCode Problem Solved! 🔧 🌟21. Merge Two Sorted Lists🌟 🔗 Solution Code: https://lnkd.in/gU7m-4wH 🧠 Approach: • Recursive Merge • Checked for null node base cases to identify the ends of the lists. • Recursively compared the current node values of list1 and list2. • Attached the smaller node to point to the recursively merged result of the remaining nodes. ⚡ Key Learning: • Leveraging recursion drastically simplifies Linked List operations, turning complex pointer-splicing logic into an elegant and readable sequence! ⏱️ Complexity: Time: • O(n + m) — where n and m are the lengths of the two lists Space. • O(n + m) — due to the recursion stack #LeetCode #Java #DSA #ProblemSolving #Consistency #100DaysOfCode #CodingJourney #LinkedList #Recursion
To view or add a comment, sign in
-
-
🚀 Day 76 — Slow & Fast Pointer (Find the Duplicate Number) Continuing the cycle detection pattern — today I applied slow‑fast pointers to an array problem where the values act as pointers to indices. 📌 Problem Solved: - LeetCode 287 – Find the Duplicate Number 🧠 Key Learnings: 1️⃣ The Problem Twist Given an array of length `n+1` containing integers from `1` to `n` (inclusive), with one duplicate. We must find the duplicate without modifying the array and using only O(1) extra space. 2️⃣ Why Slow‑Fast Pointer Works Here - Treat the array as a linked list where `i` points to `nums[i]`. - Because there’s a duplicate, two different indices point to the same value → a cycle exists in this implicit linked list. - The duplicate number is exactly the entry point of the cycle (same logic as LeetCode 142). 3️⃣ The Algorithm in Steps - Phase 1 (detect cycle): `slow = nums[slow]`, `fast = nums[nums[fast]]`. Wait for them to meet. - Phase 2 (find cycle start): Reset `slow = 0`, then move both one step at a time until they meet again. The meeting point is the duplicate. 4️⃣ Why Not Use Sorting or Hashing? - Sorting modifies the array (not allowed). - Hashing uses O(n) space (not allowed). - Slow‑fast pointer runs in O(n) time and O(1) space — perfect for the constraints. 💡 Takeaway: This problem beautifully demonstrates how the slow‑fast pattern transcends linked lists. Any structure where you can define a “next” function (here: `next(i) = nums[i]`) can be analyzed for cycles. Recognizing this abstraction is a superpower. No guilt about past breaks — just another pattern mastered, one day at a time. #DSA #SlowFastPointer #CycleDetection #FindDuplicateNumber #LeetCode #CodingJourney #Revision #Java #ProblemSolving #Consistency #GrowthMindset #TechCommunity #LearningInPublic
To view or add a comment, sign in
-
🚀 Day 49 of my #100DaysOfCode Journey Today, I solved the LeetCode problem: Set Mismatch Problem Insight: Given an array containing numbers from 1 to n, one number is duplicated and one number is missing. The goal is to find both of them. Approach: • Used a frequency array to count occurrences of each number • Traversed the input array and updated frequency • Iterated from 1 to n: – If frequency is 2 → duplicate element – If frequency is 0 → missing element • Returned both values as the final result Time Complexity: O(n) Space Complexity: O(n) Key Learnings: • Frequency array is a simple and powerful technique for counting problems • Helps quickly identify missing and repeating elements • Clean and easy-to-understand approach for beginners Takeaway: Sometimes the simplest approach is the most effective. Mastering basic patterns like counting can solve many problems efficiently! #DSA #Java #LeetCode #100DaysOfCode #CodingJourney
To view or add a comment, sign in
-
-
60-Day LeetCode Challenge – Day 43 Solved Check If Two String Arrays are Equivalent on LeetCode. 📌 Approach: Concatenated both string arrays into two strings and compared them using .equals(). 🧠 Learning: Reinforced how string building and comparison works, especially when data is split across arrays. ⚡ Complexity: • Time Complexity: O(n + m) • Space Complexity: O(n + m) Simple logic, but a clean reminder that breaking a problem down makes it easier to solve. #LeetCode #DSA #Java #Strings #Consistency #ProblemSolving #LeetCode60
To view or add a comment, sign in
-
Explore related topics
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