🚀 Day 49 of My Coding Journey Today I solved my second problem of the day: 👉 Triplet Sum Problem 🧠 Problem: Given an array and a target value, determine whether there exists a triplet whose sum equals the target. 💡 Example: arr = [1, 4, 45, 6, 10, 8], target = 13 ✔️ Output: true (Triplet: 1, 4, 8) ⚡ My Approach: At first, I considered the brute force method (checking all combinations), but that would take O(n³) time ❌ Then I optimized it using: 👉 Sorting + Two Pointer Technique 🔹 Steps: Sort the array Fix one element Use two pointers (left & right) to find the remaining sum ⏱️ Time Complexity: O(n²) ✅ 💡 Key Learning: Transforming a 3Sum problem into a 2Sum problem makes it much more efficient. 📈 Progress: This is my second problem on Day 49, and I’m improving my understanding of patterns like Two Pointer and optimization techniques. Small improvements every day lead to big results 💪 #Day49 #CodingJourney #Java #DSA #ProblemSolving #Consistency #Learning My way ===== class Solution { public boolean hasTripletSum(int arr[], int target) { int sum=0; Arrays.sort(arr); for(int i=0;i<arr.length;i++) { int left=i+1; int right=arr.length-1; while(left<right) { sum=arr[i]+arr[left]+arr[right]; if(sum==target) { return true; } else if(sum<target) { left++; } else { right--; } } } return false; } }
Triplet Sum Problem Solution with Two Pointer Technique
More Relevant Posts
-
🚀 Day 50 of My Coding Journey Today’s problem was simple but interesting: 👉 Replace all 0’s in a number with 5 Example: Input: 1004 Output: 1554 💡 Approach: Traverse each digit of the number Whenever a digit is 0, replace it with 5 Reconstruct the number 🧠 Key Learning: Even easy problems improve thinking about edge cases and number manipulation. Consistency matters more than difficulty. 🔥 50 days completed — not stopping here. Next target: Day 100 💯 #Day50 #CodingJourney #Java #ProblemSolving #Consistency #Learning My way ===== class Solution { int convertfive(int num) { int l=0,sum=0; String s=""+num,s1="",s2=""; for(int i=0;i<s.length();i++) { if(s.charAt(i)=='0') { s1=s1+'5'; } else { s1=s1+s.charAt(i); } } for(int i=0;i<s1.length();i++) { s2=""+s1.charAt(i); l=Integer.parseInt(s2); sum=(sum*10)+l; } return sum; } }
To view or add a comment, sign in
-
-
🚀 Day 49 of My Coding Journey Today I solved an interesting problem: Row with Maximum 1s in a Binary Matrix 🧠 Problem Insight: Given a 2D binary matrix where each row is sorted (0s first, then 1s), the goal is to find the index of the first row that contains the maximum number of 1s. ⚡ My Approach: At first, I thought of a brute force solution by counting 1s in every row. It works, but it's not efficient. Then I realized an important property: 👉 Since each row is sorted, once we find the first 1, all elements to the right are also 1. So instead of checking every element: Start from the top-right corner Move left if you find 1 Move down if you find 0 This reduces the time complexity to O(n + m) 🚀 💡 Key Learning: Understanding the structure of the data (sorted rows) can help optimize the solution drastically. 📌 Result: Successfully found the row index with maximum 1s efficiently. Every day, I’m getting better at recognizing patterns and optimizing solutions 💪 #Day49 #CodingJourney #Java #ProblemSolving #DataStructures #Algorithms #Learning #Consistency My way ===== // User function Template for Java class Solution { public int rowWithMax1s(int arr[][]) { int c=0,l=0,a=0; for(int i=0;i<arr.length;i++) { for(int j=0;j<arr[i].length;j++) { if(arr[i][j]==1) { c++; } } if(c>l) { l=c; a=i; } c=0; } if(l>0) { return a; } else { return -1; } } }
To view or add a comment, sign in
-
-
Day-3 of My Coding Journey Today I worked on the LeetCode problem: 👉 Find Numbers with Even Number of Digits What I learned: How to count digits in a number using String.valueOf(num).length() How to check if a number has even digits using % 2 == 0 ❌ My Mistake: I wrote: String.value(num).length % 2 == 0 ✔ Issues: Used value() instead of valueOf() Forgot () in length ✅ Fix: String.valueOf(num).length() % 2 == 0 After correcting this, my solution got Accepted! ✨ Takeaway: Small syntax mistakes can cause errors, but fixing them improves attention to detail and strengthens fundamentals. #Day3 #LeetCode #Java #CodingJourney #Learning 10000 Coders
To view or add a comment, sign in
-
-
🔥 Day 2 of my 50 Days Wild Coding Kickoff! 🔥 💡 Problem 2: Two Sum Given an array of integers nums and an integer target, return the indices i and j such that nums[i] + nums[j] == target and i != j. You may assume that every input has exactly one pair of indices i and j that satisfy the condition. Return the answer with the smaller index first. 🚀 Approach: Used a HashMap to store elements and efficiently find the complement in one pass (O(n) time complexity). 🧠 Key Idea: For each element nums[i], check if target - nums[i] already exists in the map. If yes → solution found! #100DaysOfCode #50DaysOfCode #CodingChallenge #JavaDeveloper #JavaProgramming #DataStructures #Algorithms #DSA #CodingJourney #CodeNewbie #ProgrammerLife #TechLearning #DeveloperLife #CodingDaily #InterviewPrep #LeetCode #ProblemSolving #SoftwareDeveloper #CodePractice #LearnToCode #TechSkills #FutureDeveloper #CodingMotivation #BuildInPublic #Consistency #GrowthMindset #DevelopersIndia #CodeWithMe #DailyCoding
To view or add a comment, sign in
-
-
🚀 Day 13 / 50 – Wild Coding Kickoff 🔥 Today’s problem looked like it wanted a complicated solution… but it ended up teaching me something much simpler. I read the question and instantly thought: “Okay, substring search… this might get tricky.” 👀 Loops, comparisons, edge cases… my mind started going there. But then I stopped and asked: 👉 “Is there a simpler way?” That’s when I used this: class Solution { public int strStr(String haystack, String needle) { int index = haystack.indexOf(needle); return index; } } 💡 What does indexOf() actually do? It searches for the first occurrence of needle inside haystack Returns: ✅ The starting index if found ❌ -1 if the substring is not present 🔥 Example haystack = "sadbutsad" needle = "sad" 👉 Output: 0 (because "sad" starts at index 0) 🧠 What I learned today Sometimes we try to prove we know everything… But real skill is knowing when to keep things simple. Using built-in methods effectively is also a skill. #Day13 #50DaysOfCode #WildCodingKickoff #LeetCode #Java #CodingJourney #Consistency #KeepItSimple #ProblemSolving
To view or add a comment, sign in
-
-
🚀 Day 2 of My Coding Challenge Improving my problem-solving skills step by step! Today I solved an important array problem from LeetCode. 🔹 Platforms: LeetCode & GeeksforGeeks 🔹 Problem: LeetCode #189 – Rotate Array 🔹 Problem Statement: Rotate the array to the right by k steps. 🔹 Approach: I used the Reversal Algorithm for an optimized solution: 1️⃣ Reverse the entire array 2️⃣ Reverse first k elements 3️⃣ Reverse remaining elements 🔹 Example: Input: [1,2,3,4,5,6,7], k = 3 Output: [5,6,7,1,2,3,4] 🔹 What I learned: ✔ Optimized approach from brute force to O(n) ✔ Importance of array manipulation techniques ✔ Handling edge cases like k > n 💻 Code: import java.util.Arrays; public class RotateArrayLeetCode189 { ``` public static void reverse(int[] arr, int start, int end) { while (start < end) { int temp = arr[start]; arr[start] = arr[end]; arr[end] = temp; start++; end--; } } public static void rotate(int[] arr, int k) { int n = arr.length; k = k % n; if (k < 0) { k = k + n; } // Step 1: Reverse entire array reverse(arr, 0, n - 1); // Step 2: Reverse first k elements reverse(arr, 0, k - 1); // Step 3: Reverse remaining elements reverse(arr, k, n - 1); } public static void main(String[] args) { int[] arr = {1, 2, 3, 4, 5, 6, 7}; int k = 3; rotate(arr, k); System.out.println(Arrays.toString(arr)); } ``` } 🔗 GitHub: https://lnkd.in/g-wNSrPq #Java #DSA #LeetCode #CodingChallenge #50DaysOfCode #LearningJourney #PlacementPreparation
To view or add a comment, sign in
-
DSA Journey Continues 🚀 Today I solved the “Product of Array Except Self” problem on LeetCode. This problem really helped me understand how to optimize without using division and extra space. Problem Goal: For each index, return the product of all elements except the current one. Step-by-Step Approach : 1. Left Product Pass Traverse from left to right Store product of all elements before current index Keep updating a variable left 2. Right Product Pass Traverse from right to left Multiply with product of all elements after current index Maintain another variable right Core Idea: First pass → store left products Second pass → multiply with right products Final array gives the result Why This is Efficient: No division used Time Complexity: O(n) Space optimized (no extra arrays, only output array) What I Learned Today: Prefix and suffix product concepts How to solve problems in two passes Writing optimized solutions without extra space Understanding patterns like this makes problem solving much easier over time. #Day4 #DSAJourney #Java #LeetCode #ProblemSolving #Coding #Learning #Consistency #100DaysOfCode 10000 Coders Pathulothu Navinder
To view or add a comment, sign in
-
🚀 Day 13/100 – LeetCode Journey 📌 Problem: Rotate Array (LeetCode 189) Today’s problem looked simple at first… but it taught me something deeper about how arrays and math work together. 🔹 Task: Rotate an array to the right by k steps. Example: [1,2,3,4,5,6,7], k = 3 → [5,6,7,1,2,3,4] --- 💡 Key Learning: Instead of shifting elements again and again (which is inefficient), I learned a smarter way using: 👉 "(i + k) % n" This single formula helped me: - Move elements to the correct position - Avoid index out-of-bounds - Understand how modulo (%) creates a circular effect --- 🧠 Biggest takeaway: At first, I struggled with: 👉 Why "3 % 7 = 3" 👉 Why we don’t use decimals in modulo Now it finally makes sense: ✔ Modulo gives the remainder after full division ✔ It helps wrap values within array limits ✔ Arrays can be treated like a circle 🔄 --- 💻 Approach Used: - Extra array to store rotated values - Place each element using "(i + k) % n" - Copy back to original array --- ✨ What I realized: Sometimes the problem isn’t coding… It’s understanding the math behind it. --- #Day13 #100DaysOfCode #LeetCode #Java #DSA #CodingJourney #ProblemSolving
To view or add a comment, sign in
-
-
🚀 Day 6 of LeetCode Problem Solving Solved today’s problem — LeetCode #1480: Running Sum of 1d Array 💻🔥 ✅ Approach: Prefix Sum (Running Sum) ⚡ Time Complexity: O(n) 📊 Space Complexity: O(n) The task was to compute the running sum of an array where each element represents the sum of all previous elements including itself. 👉 Example: Input: [1,2,3,4] Output: [1,3,6,10] 💡 Key Idea: Keep adding elements as you traverse the array and store the cumulative sum. 👉 Core Logic: Initialize sum = 0 Traverse array Add current element → sum += nums[i] Store in result array 💡 Key Learning: Simple problems help build strong fundamentals — mastering basics like prefix sum is very important for advanced problems. Grateful to my mentor Pulkit Aggarwal — your guidance is helping me strengthen my fundamentals every day 🙌 Consistency is the key — small steps lead to big results 🚀 #Day6 #LeetCode #DSA #Java #CodingJourney #ProblemSolving #100DaysOfCode
To view or add a comment, sign in
-
-
Day 24 of #100DaysOfCode I stared at this problem for 40 minutes trying every possible target value. Then one insight — borrowed from basic statistics — made it click instantly. 🧩 The Problem: Minimum Operations to Make a Uni-Value Grid (LeetCode 2033 — Medium) Given a 2D grid and a fixed step value x, transform every element to the same value using the minimum number of add/subtract operations. My first instinct? Try every possible target. Loop through everything. Classic overthinking. 😅 💡 The Key Insight — Why the Median? The median minimizes the sum of absolute differences. This is a well-known statistics fact — and it maps perfectly to this problem. Instead of brute-forcing every target, the strategy is simple: → Flatten the 2D grid into a 1D array → Sort it → Pick the middle element (median) as the target → Count total operations using the absolute difference divided by x No guessing. No unnecessary loops. Just math doing the heavy lifting. ⚠️ The Constraint Check Nobody Talks About Before applying any operations — check if all elements share the same remainder when divided by x. If they don't, it's mathematically impossible to make the grid uni-value. Return -1 immediately and save the computation entirely. This "fail fast" mindset is just as important as the algorithm itself. 📈 Complexity Breakdown → Flatten + Sort → O(n log n) → Single pass to count operations → O(n) → Space → O(n) for the flattened array Simple, clean, and efficient. 🧠 What This Problem Reinforced ✅ Greedy thinking with median optimization ✅ Handle impossible cases before computation — fail fast ✅ Transform 2D problems into simpler 1D forms ✅ Let math do the heavy lifting before writing a single loop The best solutions often come from asking: "What does math already know about this?" On to the next challenge 💪 👇 What's a problem where a simple insight saved you from overcomplicating things? Drop it in the comments — would love to learn from your experience! #100DaysOfCode #LeetCode #DSA #Java #ProblemSolving #CodingJourney #SoftwareEngineering #Programming
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