💡 Day 19 of My LeetCode Journey — 3Sum Closest Today’s problem was “3Sum Closest”, a slight twist on the classic 3Sum challenge. 🧩 Problem Statement: Given an integer array nums and a target value, find three integers whose sum is closest to the target. Return the sum of those three numbers. Example: Input: nums = [-1, 2, 1, -4], target = 1 Output: 2 Explanation: (-1 + 2 + 1 = 2) ⚙️ Approach: Sort the array for easy pointer movement. Use a for loop to fix one element at a time. Apply the two-pointer technique to find the closest sum. Compare differences using Math.abs() to track which combination is nearer to the target. 📘 Key Learning: 🔹 Sorting simplifies pointer logic. 🔹 Math.abs() is crucial for measuring closeness. 🔹 Sometimes, you don’t need the exact answer — just the closest one. #30DaysOfCode #LeetCode #Java #CodingJourney #ProblemSolving #DataStructures #Algorithms #TwoPointers #TechLearning
Solved 3Sum Closest problem on LeetCode using Java and two pointers
More Relevant Posts
-
🚀 Day 8 of #45DaysOfLeetCode Challenge 😎 📌Today's problem: Palindrome Number (LeetCode #9) 💡 🔹 Concept: Check whether a given integer reads the same backward and forward — without converting it to a string! 🔹 Logic Used: Instead of reversing the entire number, I reversed only half of it to improve efficiency. This avoids unnecessary computation and eliminates integer overflow risks. 🔹 Key Learnings: ✅ Optimized logic using mathematical manipulation ✅ Improved understanding of number reversal techniques ✅ Importance of edge case handling (negative numbers, trailing zeros) ⚙️ Result: 💻 Runtime: 4 ms (Beats 100%) 💾 Memory: 44.84 MB 😎Small optimizations make a big difference in performance! 💪 📌Problem: https://lnkd.in/ef6AC2j6 🔥Each day is a step closer to writing cleaner and more optimized code. ✨ Let’s keep pushing forward and refining our problem-solving skills! 💻🔥 #LeetCode #Day8 #100DaysOfCode #ProblemSolving #Java #CodingChallenge #PalindromeNumber #DataStructures #Algorithms #LearningEveryday
To view or add a comment, sign in
-
-
🚀 Problem 2: Reverse Integer 💡 Leetcode Problem No: 7 Today, I solved an interesting Leetcode-style problem that involves reversing an integer — while handling tricky cases like integer overflow 🔄 ✨ Challenge: Given an integer x, reverse its digits and return the reversed number. If reversing x causes the value to go outside the signed 32-bit integer range, return 0. 💻 Key Learnings: 🔹 Used Math.abs(x) to handle negatives elegantly 🔹 Applied overflow check using: if (rev > (Integer.MAX_VALUE - d) / 10) 🔹 Mastered modulus (%) and division (/) logic for digit extraction 🔹 Ensured both positive and negative numbers are correctly reversed 💙 Tip: Always consider edge cases and overflow conditions when working with integer manipulation problems. #Java #Coding #LeetCode #ProblemSolving #JavaDeveloper #ProgrammingChallenge #LeetcodeCoding
To view or add a comment, sign in
-
-
🌟 Day 44 – LeetCode Practice Problem: Product of Array Except Self (LeetCode #238) 📌 Concept: Given an integer array, create a new array where each element equals the product of all other elements except itself, without using division. 🧠 My Approach: First pass → compute prefix products (multiply everything before current index) Second pass → compute suffix products (multiply everything after current index) Combine both to get the result for each index efficiently No extra multiplication array used — optimized and clean logic ⚡ This avoids division and runs in linear time — exactly what the problem demands ✅ 📈 Result: ✅ Accepted ⚡ Runtime: 2 ms (Beats ~87%) 📦 Memory: Efficient usage 💡 Key Learning: This problem reinforces the power of prefix & suffix computation — a common trick in array manipulation + interview favorite. Great way to improve problem-solving without relying on brute force or division. --- 🚀 Growing stronger every day in DSA! #LeetCode #DSA #Java #ProblemSolving #PrefixSuffix #CodingJourney #ArrayProblems #LearningMindset
To view or add a comment, sign in
-
-
Day 55 of My DSA Challenge Problem: Count the number of triplets in an array that can form a valid triangle. Concept Recap: For any three sides to form a triangle, the sum of any two sides must be greater than the third side. Optimized Approach: Instead of checking every triplet (which takes O(N³)), I sorted the array and used a two-pointer technique to bring it down to O(N²): Sort the array. Fix the largest side (arr[k]) and use two pointers (i, j) to find pairs where arr[i] + arr[j] > arr[k]. If the condition holds, all elements between i and j can form valid triangles. Count and move pointers accordingly. Time Complexity: O(N²) Space Complexity: O(1) Takeaway: Sorting combined with the two-pointer approach often transforms brute-force solutions into elegant and efficient ones. #Day55 #DSAChallenge #TwoPointerTechnique #Sorting #Optimization #Java #ProblemSolving #DataStructures #Algorithms #CodingChallenge #GeeksforGeeks #LeetCode #100DaysOfCode #ProgrammersJourney #TechLearning
To view or add a comment, sign in
-
-
🚀 Day 47 of My LeetCode Journey 🚀 Problem: Sum of Square Numbers Topic: Math / Two Pointers 🧠 Approach: To check if a number c can be expressed as the sum of squares of two integers (a² + b² = c), we use the two-pointer technique: Start a = 0 and b = √c Calculate sum = a² + b² If sum == c → ✅ return true If sum < c → increase a If sum > c → decrease b Repeat until a <= b. ⏱️ Complexity: Time: O(√c) Space: O(1) This problem beautifully combines mathematical logic with an efficient two-pointer approach — clean and elegant! 💡 #100DaysOfCode #LeetCode #Java #CodingChallenge #ProblemSolving #DSA
To view or add a comment, sign in
-
-
#Day_27 Today’s problem was an interesting twist on binary search — “Find Peak Element” 🔍 Given an array of numbers, the task is to find an index i such that nums[i] is greater than its neighbors — basically, a peak element. At first glance, it seems like a simple linear scan could solve it in O(n). But I wanted to do better — and that’s where binary search comes in 💡 🧠 Approach We observe that: If nums[mid] > nums[mid + 1], it means we are on a descending slope, so the peak lies on the left side (including mid). Otherwise, we’re on an ascending slope, so the peak lies on the right side (after mid). Using this logic, we can shrink our search space by half in every iteration — a classic divide and conquer move 🔥 ⏱ Complexity Time: O(log n) Space: O(1) This is a great example of how binary search isn’t just for sorted arrays — it can also be applied to problems involving patterns or directional decisions. 💬 Every day of this challenge reinforces that problem-solving isn’t just about writing code — it’s about recognizing patterns, making logical deductions, and applying optimized thinking. #CodingJourney #LeetCode #BinarySearch #ProblemSolving #100DaysOfCode #Java #LearnByDoing
To view or add a comment, sign in
-
-
Anagrams and Minimum Steps: A Frequency Counting Solution! Just solved the "Minimum Steps to Make Two Strings Anagram" problem! When strings 's' and 't' are the same length, the key is efficiently determining how many characters in 't' need to be replaced to match the required character distribution of 's'. My approach is simple and highly efficient: 1. Count Requirements: Use a HashMap to store the exact frequency of every character in string 's'. 2. Match & Mismatch: Iterate through string 't'. If a character is needed (count > 0), we use it up. If it's not needed, it represents one step (one character in 't' that must be changed). This lands us in optimal O(N) time complexity and O(1) space complexity (since the alphabet size is constant). Frequency counting is a powerhouse technique for any string problem! #Algorithms #DataStructures #StringManipulation #LeetCode #CodingChallenge #Java #100daysofcode
To view or add a comment, sign in
-
-
🚩 Problem: 41. First Missing Positive 🔥 Day 54 of #100DaysOfLeetCode 🔍 Problem Summary: Given an unsorted integer array nums, return the smallest positive integer that is missing. You must solve it in O(n) time and O(1) extra space. This is one of the most elegant and clever array manipulation problems. 🧠 Intuition: Key observations: The answer must be in the range [1, n+1] We want each number x (1 ≤ x ≤ n) to be placed at index x - 1 This leads to the index marking / cyclic placement trick: Iterate through the array Place each number x into its correct position: At index x - 1 By swapping until the number is either out of range or already in correct place After rearrangement, the first index i where nums[i] != i + 1 gives the answer If all numbers are in correct places → answer is n + 1 Simple idea, extremely powerful.⚙️ Performance: ⏱️ Runtime: 2 ms 🚀 💪 Beats: 99.8% of Java solutions 💾 Memory: 56 MB ⚡ (Beats 95% of users) 📊 Complexity: Time Complexity: O(n) Space Complexity: O(1) ✨ Key Takeaway: This problem teaches the Cyclic Sort pattern — an essential technique for problems involving: Missing numbers Duplicates Correct placement In-place array rearrangement Mastering this trick gives you an edge in advanced array manipulation question Link:[https://lnkd.in/gV8y8dhc] #100DaysOfLeetCode #Day54 #Problem41 #FirstMissingPositive #CyclicSort #IndexMapping #Algorithms #DSA #Java #LeetCode #ProblemSolving #CodingChallenge #InterviewPreparation #CrackingTheCodingInterview #SoftwareEngineering #CodingCommunity #DataStructures #DeveloperJourney #ArjunInfoSolution #TechCareers #CareerGrowth #Programming #CodeNewbie #ComputerScience #LearnToCode #ZeroToHero #CodingIsFun #CodeLife #JavaDeveloper #GameDeveloper #Unity #AI #MachineLearning
To view or add a comment, sign in
-
-
🌳 Day 60 of 100: Maximum Depth of Binary Tree 🌳 Today’s challenge was LeetCode 104 – Maximum Depth of Binary Tree 🌲 The task? Given the root of a binary tree, find the maximum depth — the number of nodes along the longest path from the root to a leaf node 🍃 💡 Intuition: A binary tree’s depth can be thought of as its “height”. To find it, we just need to know the depth of the left and right subtrees — and the answer is the greater of the two, plus one for the current node. This is a classic example of recursion done right — breaking a big problem into smaller ones that mirror the original. ⏱ Time Complexity: O(n) – visit every node once 🗂 Space Complexity: O(h) – h is the height of the tree (recursion stack) ✨ Key Takeaway: This problem beautifully highlights the power of divide and conquer — by solving smaller subproblems (left and right trees), we can elegantly solve the bigger one. #100DaysOfCode #Day60 #LeetCode #Java #CodingJourney #BinaryTree #Recursion #DataStructures #CodingPractice #ProblemSolving #WomenWhoCode #CodeNewbie #LearnToCode
To view or add a comment, sign in
-
-
✅ Day 68 of LeetCode Medium/Hard Edition Today’s challenge was “Number of Ways to Form a Target String Given a Dictionary” — a brilliant Dynamic Programming and Combinatorics problem that tests precision in transitions and precomputation logic 🧩⚙️ 📦 Problem: You’re given an array of equal-length strings words and a target string target. You must form target from left to right by picking characters from the columns of words under these rules: 1️⃣ Once you use column k from any word, all columns ≤ k in every word become unusable. 2️⃣ You can use multiple characters from the same word, respecting column progression. Return the number of ways to form target modulo 1e9 + 7. 🔗 Problem Link: https://lnkd.in/gns9CwWa ✅ My Submission: https://lnkd.in/g7bsgZq9 💡 Thought Process: This problem is a clever mix of frequency compression and DP memoization. We precompute a frequency table freq[26][m], where each cell represents how many words contain a given letter at column m. Then, using recursion with memoization: 🎯 At each step (i, j) Either use freq[target[i]][j] if it exists → multiply by ways for the next position (i+1, j+1) Or skip the current column → move to (i, j+1) The recurrence relation: dp[i][j] = freq[target[i]][j] * dp[i+1][j+1] + dp[i][j+1] All computations are done modulo 1e9 + 7. ⚙️ Complexity: ⏱ Time: O(26 * n + t * m) — efficient due to frequency precomputation 💾 Space: O(t * m) — for memoized DP table 💭 Takeaway: This challenge reinforced how precomputation and state optimization can transform a seemingly exponential recursion into an elegant polynomial-time solution 🚀 #LeetCodeMediumHardEdition #100DaysChallenge #DynamicProgramming #Combinatorics #ProblemSolving #Java #CodingChallenge #DSA
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