🚀 Solved: Missing & Repeating Number Problem (DSA) Today I solved a classic and highly asked interview problem on arrays — finding the missing and repeating number in O(n) time. 💡 Key Learnings: There are multiple approaches: 🧠 Math Formula (Sum & Square Sum) ⚡ XOR Method (Best for avoiding overflow) 📊 Hashing (Simple but extra space) The trick is converting the problem into equations: Difference → (Repeating - Missing) Sum → (Repeating + Missing) ⚠️ Big Mistake I Faced: Integer overflow while calculating formulas like: n(n+1)/2 👉 Fixed by casting before multiplication → (long)n * (n+1) ✅ Finally got: 1111 / 1111 test cases passed 100% accuracy 📈 What I realized: It’s not just about solving problems — it’s about understanding why your solution fails and fixing edge cases. 🔥 Next Goal: Master XOR approach Solve more pattern-based DSA problems daily 💬 If you're learning DSA: Consistency + Debugging = Growth #DSA #Coding #Java #ProblemSolving #Learning #GeeksforGeeks #100DaysOfCode #Programming #InterviewPrep
Missing & Repeating Number Problem Solved in O(n) Time
More Relevant Posts
-
Post 2 — Memoization Concept 🎯 DSA Concept: Memoization in Dynamic Programming 🎯 Today I learned Memoization, a top-down DP approach. What I practiced: • Converting recursion into optimized recursion • Storing intermediate results • Reducing time complexity drastically Key Learning: Caching results can transform exponential solutions into polynomial time solutions. Reflection: Optimization is often about avoiding repeated computation. Next: Explore Tabulation approach. #DSA #DP #Memoization #ProblemSolving #CodingPractice 🚀
To view or add a comment, sign in
-
Lately, I have been working on the problem "Flatten Binary Tree to Linked List." The problem is to convert a binary tree into a linked list in-place by maintaining the same order as the preorder traversal of the binary tree. Initially, the problem is quite obvious when solved using a recursive approach. However, I wanted to optimize the solution by avoiding extra space. Therefore, I decided to solve the problem using an iterative approach by employing the "Morris Traversal" technique. Approach: To solve the problem using the Morris Traversal technique, we follow the following approach: - If the node does not have a left child, we move to the right child. - If the node has a left child, we find the rightmost node of the left subtree. - We connect the rightmost node of the left subtree to the right subtree of the current node. - Then, we move the left subtree to the right of the current node and set the left child of the current node to null. One interesting thing I discovered is the usage of the Morris Traversal technique to solve the problem. It is quite interesting to see how the Morris Traversal technique is being adapted to solve the problem. Time Complexity: O(N) Space Complexity: O(1) This problem was a good reminder that sometimes the optimal solution is not the most obvious one, but comes from rethinking how we traverse the structure. #DSA #BinaryTree #Algorithms #Coding #Programming
To view or add a comment, sign in
-
-
🚀 Day 105 of DSA Problem Solving Solved today’s problem: 👉 Shortest Distance to Target String in a Circular Array 💡 Problem Idea: Given a circular array, we need to find the minimum steps to reach a target string from a starting index. We can move left or right, and the array wraps around. 🧠 Key Learning: The main trick is handling the circular nature of the array. Instead of simulating movement step-by-step, we use: 👉 min(direct distance, circular wrap distance) This makes the solution clean and efficient. 🛠 Concepts Practiced: Circular Array Handling Modular Arithmetic Greedy Minimum Distance Clean Code Optimization ⏱ Time Complexity: 👉 O(n) 📦 Space Complexity: 👉 O(1) 🔥 Real Journey Behind the Solution: At first, I thought this required simulating left and right movement step-by-step (true brute force). But then I realized we can directly calculate distance mathematically. Also learned an important lesson: 👉 Sometimes “optimized” doesn’t mean faster, it means cleaner and smarter thinking. 💭 Takeaway: Problems may look easy, but they test your ability to identify patterns like: Circular traversal Minimum distance logic These patterns appear again in harder problems too. #DSA #Java #CodingJourney #LeetCode #ProblemSolving #Learning 🚀
To view or add a comment, sign in
-
-
😎 Day 38 of Solving DSA — >Next Permutation : 🚀 Today I solved one of the most elegant array problems out there: Next Permutation. ❓ Problem: Given an array of integers, find the next lexicographically greater permutation. If none exists, return the smallest (sorted ascending). 💡 Key Insight — 3 simple steps: Step 1: Find the first decreasing element from the right (called the "pivot") Step 2: Swap it with the smallest element to its right that is still greater than it Step 3: Reverse everything to the right of the pivot Input: [1, 2, 3] → Output: [1, 3, 2] Input: [3, 2, 1] → Output: [1, 2, 3] Input: [1, 1, 5] → Output: [1, 5, 1] ⚡ Time: O(n) Space: O(1) What makes this beautiful? No extra space, no brute-force generating all permutations. Just pure pattern recognition on the array structure. 🧠 The moment I understood WHY we reverse the suffix instead of sorting it — it all clicked. Reversing works because the suffix is already in descending order after the swap! Drop a 🔥 if you've solved this one before! #DSA #CodingInterview #Java #Arrays #LeetCode #100DaysOfCode #Programming
To view or add a comment, sign in
-
-
🎯 DSA Journey Update: Starting Dynamic Programming 🎯 Continuing my DSA journey, today I started learning Dynamic Programming (DP) — a powerful technique used to optimize recursive solutions by storing previously computed results. What I learned today: • Overlapping subproblems • Optimal substructure • Memoization vs Tabulation Key Insight: Dynamic Programming transforms exponential solutions into efficient ones by avoiding repeated calculations. Learning Outcome: ✅ Better understanding of optimization techniques ✅ Transition from recursion → optimized algorithms ✅ Strong foundation for advanced interview problems Next Step: Solve beginner DP problems and understand state transitions. #DSA #DynamicProgramming #Algorithms #CodingJourney #DailyLearning 🚀
To view or add a comment, sign in
-
Day-28 Solved the Count and Say problem today and it turned out to be a really good exercise in string manipulation. 👉 The idea is simple: start with "1" and keep generating the next term by describing the previous one. 👉 You only count consecutive digits, not overall frequency. 👉 Example: "1" → "11" → "21" → "1211" 💡 What I learned: Always think in terms of patterns Handle edge cases carefully (especially index bounds ⚠️) Small bugs like accessing i+1 without checking can break everything 🛠️ Concepts used: Strings Loops to_string() It looked confusing at first, but once the pattern clicked, it became straightforward. #leetcode #cpp #dsa #coding #learning
To view or add a comment, sign in
-
-
Day 67 of My DSA Journey Today’s problem: Decode Ways (LeetCode 91) Problem Summary: Given a string of digits, determine how many different ways it can be decoded using the mapping: 1 → A, 2 → B, ..., 26 → Z Key Insight: At every step, we have two choices: ✔ Take one digit (if it’s between 1 and 9) ✔ Take two digits (if it forms a valid number between 10 and 26) This makes it a perfect Dynamic Programming problem. 🧠 Approach: Build the solution step by step Keep track of the number of ways to decode up to each position Carefully handle edge cases like: ❌ Strings starting with ‘0’ ❌ Invalid combinations like “06” ⚡ Key Learnings: ✔ Small decisions at each step can build into a complete solution ✔ Handling edge cases is crucial in string problems ✔ This problem follows a pattern similar to Fibonacci 📈 Complexity: Time: O(n) Space: Optimizable to O(1) 🔥 Consistency is the key — one problem closer to mastering DSA! #Day67 #DSA #LeetCode #DynamicProgramming #CodingJourney #ProblemSolving
To view or add a comment, sign in
-
-
🚀 Day 1/15 – DSA Challenge 🔹 Topic: Arrays Today I solved multiple beginner-level array problems: 1. Find Maximum Element 2. Second Largest Element 3. Check if Array is Sorted 4. Remove duplicates from Sorted array 5. Left Rotate Array by One 6. Left Rotate Array by K Places 7. Move Zeros to End 8. Linear Search 9. Union of two sorted arrays 10. Find missing number 11.Maximum Consecutive Ones 12.Find the number that appears once, and other numbers twice. 13.Longest subarray with given sum K(positives) 14.Longest subarray with sum K 🧠 Approach: Worked on basic traversal and comparison logic to strengthen fundamentals. Today helped me build a strong base in array operations and improved my understanding of loops and conditions. #DSA #Coding #CPlusPlus #Learning #Challenge #Consistency
To view or add a comment, sign in
-
🚀 DSA Practice Continues — Learning Something New Every Day! 📅 Today’s Problem: Union of Two Sorted Arrays This problem focused on efficiently merging two sorted arrays while maintaining unique elements in sorted order. 🔹 Approach Used: Two Pointers Technique Traversed both arrays simultaneously and added elements in sorted order while avoiding duplicates. ⏱ Time Complexity: O(n + m) 💾 Space Complexity: O(n + m) 💡 Key Learning: When working with sorted arrays, using the two-pointer technique helps avoid unnecessary operations, such as sorting again, making the solution more efficient. Also learned how to handle duplicates smartly during merging — a small detail that makes a big difference. 🔥 Another step forward in strengthening problem-solving and DSA fundamentals! takeUforward Striver #DSA #DataStructures #Algorithms #Coding #Programming #Java #ProblemSolving #SoftwareEngineering #Consistency
To view or add a comment, sign in
-
-
🚀 Strengthening my foundation in Data Structures & Algorithms Explored key concepts like Sorting & Searching Algorithms, Dynamic Programming, Graph Algorithms, and more. From Binary Search & Merge Sort to Dijkstra’s Algorithm & DFS/BFS, this journey is all about improving problem-solving skills and writing efficient code. 💡 Consistency + Practice = Growth All credit goes to the original creater of the material. Feel free to Repost and Follow Himansh S. for more Helpful resources. DM me for more resources. #DSA #CodingJourney #ProblemSolving #Programming #SoftwareDevelopment #Learning #TechSkills
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