🚀 Day 110 of #LeetCode Challenge! Problem: Duplicate Zeros 💡 My Approach: The task is to duplicate every zero in the array in-place, shifting the remaining elements to the right. First, count the total number of zeros to determine the “virtual” extended array size. Then, use two pointers — one (i) for the original array and another (j) for the extended index. Traverse backward: Copy each element from i to j. If it’s a zero, write an additional zero (when within bounds). This avoids overwriting elements and keeps the solution in O(1) extra space. ✨ Example: Input: [1,0,2,3,0,4,5,0] Output: [1,0,0,2,3,0,0,4] ⏱ Time Complexity: O(N) — single pass from the end 💾 Space Complexity: O(1) — done in-place without extra memory 👨💻 GitHub Link: https://lnkd.in/gCuFGw-c #LeetCode #Array #InPlaceAlgorithm #TwoPointers #ProblemSolving #DuplicateZeros #Day110 #DSA #CodingChallenge #C++
"Day 110: Duplicate Zeros in Array with In-Place Algorithm"
More Relevant Posts
-
💡 Day 78 of #100DaysOfCode 💡 🔹 Problem: Find Closest Number to Zero – LeetCode ✨ Approach: Traversed through the array while keeping track of the number closest to zero using absolute difference comparison. Handled ties by preferring the positive number — because even in code, positivity wins 😉 📊 Complexity Analysis: ⏱ Time Complexity: O(n) — single pass through the array 💾 Space Complexity: O(1) — no extra space used ✅ Runtime: 3 ms (Beats 44.80%) ✅ Memory: 46.87 MB 🔑 Key Insight: Even the smallest differences matter — especially when you’re finding what’s closest to zero! ⚡ #LeetCode #100DaysOfCode #DSA #ProblemSolving #CodingChallenge #JavaProgramming #AlgorithmDesign #CodeJourney #StayPositive
To view or add a comment, sign in
-
-
🚀 Day 122 of #LeetCode Challenge! Problem: Subtree of Another Tree 💡 My Approach: The goal is to check whether one tree (subRoot) is a subtree of another (root). Here’s how I approached it: Traverse the main tree (root) recursively. At each node, check if the subtree starting there is identical to subRoot. If not, keep checking in the left and right subtrees. For the comparison, a helper function isSameTree() ensures both trees have identical structure and node values. 🧠 Key Idea A tree subRoot is a subtree of root if there exists a node in root such that the subtree rooted at that node is structurally identical to subRoot. ✨ Example Input: root = [3,4,5,1,2] subRoot = [4,1,2] Output: ✅ true Explanation: The subtree starting at node 4 in root matches subRoot. ⏱ Complexity TypeValueTimeO(M × N) — for each node in root, we may compare with all nodes in subRootSpaceO(H) — recursion stack (H = height of the tree) 📎 GitHub Link: https://lnkd.in/gv3w8RFe #LeetCode #BinaryTree #Recursion #C++ #ProblemSolving #Day122 #CodingChallenge
To view or add a comment, sign in
-
-
🚀 Day 80 of #100DaysOfCode 🚀 🧩 Problem: Merge Sorted Array — LeetCode 💡 Approach: Started from the end of both arrays, comparing elements and placing the larger one at the last position. This in-place merging avoids extra space and ensures efficiency — a clean, optimal two-pointer solution! ⚡ 📊 Complexity Analysis: ⏱ Time Complexity: O(m + n) 💾 Space Complexity: O(1) ✅ Runtime: 0 ms (Beats 100% 🚀) ✅ Memory: 43.82 MB ✨ Key Insight: Sometimes, the best way forward is to start from the end — both in arrays and in problem-solving. 😉 #LeetCode #100DaysOfCode #JavaProgramming #ProblemSolving #DSA #CodingChallenge #AlgorithmDesign #TechJourney #CodeEveryday
To view or add a comment, sign in
-
-
🔹 Day 69 of #100DaysOfLeetCodeChallenge 🔹 🚀 Problem: Subsets II 🔑 Topic: Backtracking 🧠 Approach: The task is to find all unique subsets of an array that may contain duplicates. Here’s the step-by-step logic 👇 Sort the array first to bring duplicates together. Use backtracking to explore every possible subset. Skip duplicates by checking if the current element equals the previous one at the same recursion depth. Add every subset (including empty) to the result list. ⏳ Time Complexity: O(2^n) 💾 Space Complexity: O(n) (recursion stack + temp list) 📌 Example: Input: nums = [1,2,2] Output: [[], [1], [1,2], [1,2,2], [2], [2,2]] ✅ 🎯 Takeaway: Sorting and skipping duplicates isn’t just for combinations — it’s the secret weapon for clean subset generation too! ⚡ #LeetCode #DSA #Backtracking #Subsets #ProblemSolving #CodingChallenge #100DaysOfLeetCodeChallenge 🚀
To view or add a comment, sign in
-
-
🚀 Day 81 of 100 Days of Leetcode Challenge - Problem 3354: Make Array Elements Equal to Zero 🔍 Problem Overview: In today's challenge, we are tasked with finding the number of valid selections in an integer array nums. The goal is to select a starting position, curr, where nums[curr] = 0, and choose a direction (left or right) to move, while following specific conditions. 🌟 Solution Walkthrough: We start by selecting a valid initial position and direction. The movement continues, updating the position and reversing direction based on the values at the current position. We track the possible valid selections that lead to all elements becoming zero. 🔧 Dry Run Example: For the input nums = [1, 0, 2, 0, 3]: We test multiple initial positions and directions. The possible valid selections are identified, leading to the final result. 💡 Brute Force Approach: We iterate through all possible initial positions and directions, but this results in suboptimal time complexity. ⚡ Optimal Approach: Using efficient iteration and tracking mechanisms, we can achieve a more optimal solution that reduces the overall time complexity. ⏱️ Time Complexity: O(n) 📦 Space Complexity: O(1) ✅ Key Learnings: Handling edge cases is crucial. A good understanding of direction reversal and boundary checks is essential. 🔑 Edge Cases: Arrays with all zeros. Handling large arrays efficiently. 🔝 Hashtags & Keywords: #100DaysOfLeetcode #LeetcodeChallenge #CodingJourney #ProblemSolving #AlgorithmDesign #TechieLife #LeetcodeSolutions #LeetcodeProblems #TimeComplexity #SpaceComplexity #CodingCommunity #TechCareer #CodingIsLife #BruteForceVsOptimal #CompetitiveProgramming
To view or add a comment, sign in
-
-
Day 64 of #100DaysOfCode was a battle with a single, very challenging problem: "Maximum Frequency of an Element." This one really put my skills to the test. The problem seems simple on the surface, but the optimal solution is a beautiful combination of several different patterns: 1. Frequency Counting to group identical numbers. 2. Prefix Sums to instantly calculate the "cost" of making all elements in a subarray equal. 3. Sliding Window to efficiently find the largest possible subarray that meets the cost constraint. It took a lot of time and debugging to handle all the edge cases and get the logic just right. It's one of those problems where each part is a puzzle, and you have to put them all together in the right order. Today's lesson: The most challenging problems are often the most rewarding. They force you to look beyond a single algorithm and instead compose a solution from multiple techniques in your toolkit. It's just past midnight, but solving this one was a huge confidence boost! #100DaysOfCode #Day64 #DataStructures #Algorithms #LeetCode #ProblemSolving #SlidingWindow #PrefixSum #SoftwareEngineering #Cpp
To view or add a comment, sign in
-
-
Day 5 of 30 Solving Leetcode ✅ Problem : Count the Number of Good Partitions- O(n*logn) Approach : If we have Duplicate element of a particular integer . We have to take all duplicate element in a one subarray and we can find this using a map , left most & right most index of a element . Example : arr = [1,2,4,3,2,3] We have to take elements of subarray arr[1,4] and subarray arr[3,5] at once . And arr[1,4] and arr[3,5] overlap , so we have to take arr[1,5] at once Now we can easily find ans = 1<<len , where len represent length of subarray that we have to take at once . Here len is 2 - arr[0,0] & arr[1,5] #leetcode #dsa #problemsolving #leetcodechallenge
To view or add a comment, sign in
-
-
🚀 Day 125 of #LeetCode Challenge! Problem: Binary Tree Preorder Traversal 💡 My Approach: The goal is to perform a preorder traversal of a binary tree — visiting nodes in the order: Root → Left → Right Here’s the step-by-step logic: Start from the root node. Visit (record) the root value first. Recursively traverse the left subtree, then the right subtree. Store values in a vector as you go. ✨ Example Input: [1, null, 2, 3] Output: [1, 2, 3] 🧠 Key Idea Preorder traversal is useful when you need to copy or serialize a tree — it captures the structure starting from the root. ⏱ Complexity TypeValueTimeO(N) — each node visited onceSpaceO(H) — recursion stack (H = height of tree) 📎 GitHub Link: https://lnkd.in/gZ6GeXzm #LeetCode #BinaryTree #PreorderTraversal #Recursion #DSA #C++ #ProblemSolving #CodingChallenge #Day125
To view or add a comment, sign in
-
-
🚀 Day 127 of #LeetCode Challenge! Problem: Binary Tree Level Order Traversal 💡 My Approach: This problem involves traversing the binary tree level by level — also known as Breadth-First Search (BFS) traversal. Here’s the step-by-step breakdown: Use a queue to store nodes level by level. For each level, store all node values in a temporary list. Push their left and right children into the queue. After finishing one level, add it to the result vector. ✨ Example Input: [3,9,20,null,null,15,7] Output: [[3], [9,20], [15,7]] 🧠 Key Idea This traversal captures the hierarchical structure of the tree perfectly — often used in problems involving level-based processing, like Zigzag traversal or average of levels. ⏱ Complexity TypeValueTimeO(N) — each node visited onceSpaceO(N) — for queue and result storage 📎 GitHub Link: https://lnkd.in/gntMrKbU #LeetCode #BinaryTree #BFS #LevelOrderTraversal #DSA #ProblemSolving #C++ #CodingChallenge #100DaysOfCode #Day127
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
Keep up the consistency