🚀 Day 63 of #100DaysOfCode Today I solved a C++ problem: 👉 Find K Pairs with Smallest Sums 🧩 Problem Summary: Given two sorted arrays arr1 and arr2, and an integer k, the task is to find k pairs (a, b) where a belongs to arr1 and b belongs to arr2, such that their sum is among the smallest possible. 🧠 Approach: I used a min-heap (priority_queue with greater comparator) to always extract the smallest pair sum efficiently. Start by pushing the smallest possible pairs from arr1 and the first element of arr2. Each time we pop the smallest pair, we add the next possible pair from the same row. Continue until we find k pairs. ⚙️ Key Concepts: Priority Queue (Min-Heap) Pair Sum Optimization Efficient use of STL 💡 Complexity: Time: O(k log k) Space: O(k) 👨💻 Language: C++ (STL-based) #100DaysOfCode #Day63 #CPlusPlus #STL #CodingChallenge #DSA #ProblemSolving #LeetCode #CodeNewbie #Programmer #LearnCoding #CompetitiveProgramming #cpp #DataStructures #Algorithms
Solved C++ problem: Find K Pairs with Smallest Sums using Min-Heap
More Relevant Posts
-
🟩 Day 56 of Leetcode 150 days challenge – Add Two Numbers (LeetCode #2) #LeetCode150 #Day56 #AddTwoNumbers #LinkedList #DataStructures #Algorithms #ProblemSolving #LeetCode #CodingChallenge #CPP #Programming #150DaysOfCode #LearnDSA #SDEPrep #CodingJourney 🔹 Problem: Given two non-empty linked lists representing two non-negative integers. The digits are stored in reverse order, and each node contains a single digit. Add the two numbers and return the sum as a linked list. Example: Input: (2 -> 4 -> 3) + (5 -> 6 -> 4) Output: 7 -> 0 -> 8 Explanation: 342 + 465 = 807 🧠 Approach: We perform the addition just like we do on paper: Initialize a carry as 0. Traverse both lists simultaneously. Add the digits from both lists + carry. Store the last digit in a new node. Update carry = sum / 10. Continue until both lists are done and carry is 0. 🕒 Time Complexity: O(max(M, N)) 💾 Space Complexity: O(max(M, N)) 🏁 Key Takeaway: Linked list addition is a classic problem that strengthens your understanding of pointer manipulation, carry handling, and edge cases like unequal list lengths. 💡
To view or add a comment, sign in
-
-
🌟 Day 78 of #100DaysOfCode Today I solved the “Lemonade Change” 🍋 problem — a great exercise in Greedy Algorithms and logical decision-making. 🧩 The Problem: You’re running a lemonade stand where each glass costs $5. Customers pay with $5, $10, or $20 bills — and you must give the correct change for every transaction in order. 💡 The Approach: Maintain counts of $5 and $10 bills. For each customer: If they pay with $5 → keep it. If they pay with $10 → give one $5 as change. If they pay with $20 → try to give one $10 + one $5 (prefer larger denominations first), else give three $5 bills. If at any point change can’t be given → return false. ⚙️ Concept Used: ➡️ Greedy Algorithm — always make the optimal local decision (use larger bills first) to ensure global success. ✅ Takeaway: A simple problem that strengthens understanding of conditional logic, greedy thinking, and resource tracking in real-world scenarios. 💻✨ #DSA #Coding #CPlusPlus #ProblemSolving #GreedyAlgorithm #Programming #CodeNewbie #100DaysOfCodeChallenge
To view or add a comment, sign in
-
-
📅 Day 50 of #100DaysOfCode Problem: Simplify Path (LeetCode 71) Approach: 1️⃣ Used a stringstream to split the path by '/'. 2️⃣ Ignored empty tokens and "." since they don’t change the directory. 3️⃣ For "..", popped the last directory from the stack (if any). 4️⃣ Rebuilt the canonical path from the stack in the correct order. #100DaysOfCode #LeetCode #DSA #ProblemSolving #Stacks #Cplusplus #CodingChallenge #Algorithms #Programming #DeveloperLife #CodingJourney #CodeNewbie #DailyDSA #SoftwareEngineering #KeepLearning #GrowthMindset #Motivation #TechCommunity #CleanCode
To view or add a comment, sign in
-
-
📅 Day 24 of #100DaysOfCode 🔥 Problem: Split Linked List in Parts (LeetCode 725) Approach: ->First, count the total length of the linked list. ->Determine partSize = n / k (minimum size of each part). ->Distribute the remainder nodes (n % k) among the first few parts. ->Cut and store each part by adjusting the next pointer. #100DaysOfCode #LeetCode #DSA #LinkedList #ProblemSolving #CodingChallenge #Cplusplus #DataStructures #CodeWithClarity #Algorithms #ProgrammingJourney #CodingCommunity #DailyDSA #SoftwareEngineer #DeveloperLife #TechCommunity #GrowthMindset #ContinuousLearning #Discipline #Consistency
To view or add a comment, sign in
-
-
🚀 Day 62 of #100daysChallenge 🚀 📌 Today’s Problem: Add Two Numbers (Linked List) ✅ Results: Runtime: 0 ms (Beats 💯%) Memory: 12.66 MB (Beats 74.14%) All 1563 test cases passed successfully! Key Takeaway: Today’s problem reinforced how crucial it is to understand linked list traversal and pointer manipulation. By reversing the lists first and performing digit-wise addition, we can elegantly handle carry propagation. This challenge truly strengthens both logic and precision — two pillars of clean problem-solving. 🚀 #100DaysOfLeetCode #CProgramming #LinkedList #ProblemSolving #CodingChallenge #DSA #DeveloperJourney #Consistency #KeepLearning #LeetCode
To view or add a comment, sign in
-
-
🧠 LeetCode #1526 — Minimum Number of Increments on Subarrays to Form a Target Array Difficulty: Hard 🔴 Language: C++ 💻 💡 Approach: Instead of simulating subarray increments, observe that each time the array value surpasses the previous one, a corresponding operation is required. To calculate the total operations needed, sum all positive variances between adjacent elements along with the initial element. ✅ Time Complexity: O(n) ✅ Space Complexity: O(1) 📈 Result: Accepted (0 ms runtime) 💬 Simple logic, efficient solution! 🔥 Keep solving, keep growing 💪 #LeetCode #CPlusPlus #DSA #Coding #ProblemSolving #HardProblems #100DaysOfCode
To view or add a comment, sign in
-
-
📅 Day 31 of #100DaysOfCode Problem: Reverse Pairs (LeetCode 493) Approach: 1️⃣ Divide the array using merge sort to count pairs efficiently. 2️⃣ Count cross-pairs where nums[i] > 2 * nums[j] while merging. 3️⃣ Merge the halves back in sorted order to maintain correct comparisons. 4️⃣ Sum up counts from left, right, and cross halves for the final result. #100DaysOfCode #LeetCode #DSA #ProblemSolving #Cplusplus #CodingChallenge #Algorithms #Programming #DeveloperLife #CodeNewbie #DailyDSA #SoftwareEngineering #CodingJourney #LearningInPublic #TechCommunity #KeepLearning #DivideAndConquer #GrowthMindset #Motivation
To view or add a comment, sign in
-
-
🚀 Day 22/30 – 30DaysOfDSAChallenge 🚀 🧠 Topic: Subsets (Power Set Generation) 📘 Concepts Covered: Recursion | Backtracking | Power Set Today I solved the Subsets problem, where the goal is to generate all possible combinations (subsets) of a given array. This problem is a classic example of recursive backtracking and helps in building a solid foundation for combinatorial logic. 🔹 Approach – Recursive Backtracking 💡 Logic: At each recursive step, decide whether to include or exclude the current element. When we reach the end of the array, one subset is complete and added to the result. This method explores 2ⁿ possible subsets efficiently. ⚙️ Time Complexity: O(2ⁿ) ⚙️ Space Complexity: O(2ⁿ * n) (for storing all subsets) ✅ Result: Accepted ✅ | Runtime: 0 ms 🟢 | Beats: 100% 🚀 ✨ Key Takeaway: Recursion becomes powerful when you break a problem into smaller, self-similar parts. Every inclusion or exclusion step leads you toward mastering backtracking and decision trees. #Day22 #30DaysOfDSAChallenge #LeetCode #Recursion #Backtracking #PowerSet #Subsets #ProblemSolving #CPlusPlus #CodingChallenge #DSA #Programming #CodeEveryday #DeveloperJourney #Algorithms
To view or add a comment, sign in
-
-
📅 Day 44 of #100DaysOfCode Problem: Minimum Number of Operations to Make Array Continuous (LeetCode 2009) Approach: 1️⃣ For each element, assumed it to be the starting point of the range. 2️⃣ Calculated what the range should end at → start + n - 1. 3️⃣ Iterated over all numbers, counted how many don’t fit in this range (those need to be replaced). 4️⃣ Kept track of the minimum replacements across all possible ranges. #100DaysOfCode #LeetCode #DSA #ProblemSolving #Cplusplus #Algorithms #CodingChallenge #Programming #DeveloperLife #CodeNewbie #DailyDSA #SoftwareEngineering #CodingJourney #TechCommunity #KeepLearning #GrowthMindset #Motivation #LearningInPublic
To view or add a comment, sign in
-
-
💻 Day 34 of #100DaysOfLeetCode Today’s Challenge: 83. Remove Duplicates from Sorted List 🔹 Problem: Given the head of a sorted linked list, delete all duplicates such that each element appears only once. Return the linked list sorted as well. Key Insight: Because the list is already sorted, duplicates will always be adjacent—this allows us to remove them in a single pass using pointer manipulation. 🔍 Approach: Traverse the list using a pointer. Compare current node with the next node. If values are equal → skip the next node by linking to the next of next. Otherwise → move forward. 🕒 Time Complexity: O(n) 📦 Space Complexity: O(1) ✨ Key Takeaway: This problem reinforces the importance of pointer manipulation and understanding how memory references work in linked lists. A simple check can eliminate duplicates efficiently without using extra space. Link:[https://lnkd.in/gsjVxHXM] #100DaysOfLeetCode #Day34 #LeetCode #ProblemSolving #DSA #Algorithms #LinkedList #CodingChallenge #CodeNewbie #InterviewPreparation #SoftwareEngineering #Programming #CodingCommunity #TechCareers #CareerGrowth #ArjunInfoSolution
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