🚀 Day 102/160 – GFG Challenge Today’s problem: Cycle Detection in an Undirected Graph 🔁 🔍 Key Learning: Learned how to detect cycles using BFS traversal Used a queue with (node, parent) to track traversal path Understood why checking neighbor != parent is crucial to avoid false cycle detection 🧠 Concept Breakdown: Convert edge list → adjacency list Traverse all components (important when graph is disconnected) If a visited node is reached again and it's not the parent, a cycle exists 💡 Takeaway: Graph problems become much easier when you: Identify the input format (edge list vs adjacency list) Choose the right traversal (BFS/DFS) Track parent properly in undirected graphs ⏱️ Time Complexity: O(V + E) Consistency is key 🔥 102 days down, 58 more to go! #GFG #100DaysOfCode #DSA #Java #GraphTheory #CodingJourney
Cycle Detection in Undirected Graph using BFS
More Relevant Posts
-
🔥🚀 DAY 48/60 – Target Sum to Subset Sum! #geekstreak60 Challenge Powered by NPCI Day 48 completed ✅ Today’s problem was Count number of ways to achieve a target sum using + and - signs. Key insight 💡 Converted the problem into: 👉 Count subsets with sum = (target + totalSum) / 2 Approach used: 1D Dynamic Programming (Subset Sum Count) ✔ Calculated total sum of array ✔ Transformed into subset sum problem ✔ Used DP to count number of ways ✔ Iterated backwards to avoid overwriting This reduces the problem to a well-known DP pattern. ⏱ Time Complexity: O(n × target) 💾 Space Complexity: O(target) Today’s takeaway: Many complex problems become simple when you transform them into known DP patterns. 🧠⚡ 48 Days strong. DP mastery getting better 🔥 #geekstreak60 #DSA #Java #DynamicProgramming #SubsetSum #ProblemSolving #CodingJourney #Consistency 🚀
To view or add a comment, sign in
-
-
Day 86 – Binary Tree Level Order Traversal Worked on traversing a binary tree level by level using a queue-based approach (Breadth-First Search). Key Learnings: Learned to use a queue (FIFO) for level-wise traversal of trees Understood how to process nodes layer by layer using queue size Strengthened understanding of tree traversal patterns and data structure usage #DSA #Java #BinaryTree #BFS #Queue #ProblemSolving #CodingPractice
To view or add a comment, sign in
-
-
🚀 Solved Binary Tree Level Order Traversal on LeetCode Today I practiced another important Binary Tree problem using Queue + BFS Traversal in Java. 🌳 🔹 Level Order Traversal Order: Node by Node, Level by Level 🔹 Approach Used: • Used Queue data structure • Inserted root node first • Processed one level at a time • Stored each level in separate list • Added left and right child nodes step by step 📊 Result: ✔️ All test cases passed (35/35) ✔️ Runtime: 1 ms ✔️ Beats 95.37% submissions 💡 Key Learning: DFS helps in depth traversal, but Queue + BFS is perfect when we need level wise output. Understanding when to use Stack, Recursion, or Queue is what makes problem solving stronger. 🔥 Currently improving Trees, Graphs, Binary Search, HashMap patterns, and DP step by step. #DSA #Java #LeetCode #BinaryTree #BFS #Queue #ProblemSolving #CodingJourney #Placements
To view or add a comment, sign in
-
-
**Day 117 of #365DaysOfLeetCode Challenge** Today’s problem: **01 Matrix (LeetCode 542)** We need to find the distance of every cell to the **nearest 0** in a binary matrix. This is a perfect example of **Multi-Source BFS** 💡 **Core Idea:** Instead of running BFS from every `1` (too slow ) We start BFS from **all 0s simultaneously** Why? Because every `0` is already at distance `0`, and BFS expands layer by layer to fill shortest distances. 📌 **Approach:** 1️⃣ Put all cells containing `0` into queue 2️⃣ Mark all `1`s as unvisited (`-1`) 3️⃣ Run BFS: * Explore 4 directions * If neighbor unvisited: * distance = current + 1 * push into queue 💡 This guarantees the first time we reach a cell = shortest path to nearest 0. ⚡ **Time Complexity:** O(m × n) ⚡ **Space Complexity:** O(m × n) **What I learned today:** When shortest distance is needed from **multiple starting points**, think: 👉 Multi-Source BFS This pattern appears in many graph + matrix problems. 💭 **Key Takeaway:** Sometimes reversing the perspective makes the problem easier. Instead of finding nearest zero for every one... 👉 Let all zeros find every one. #LeetCode #DSA #BFS #GraphTheory #Matrix #Java #CodingChallenge #ProblemSolving #TechJourney #Consistency
To view or add a comment, sign in
-
-
Day 78/100 | #100DaysOfDSA 🧩🚀 Today’s problem: Maximum Level Sum of a Binary Tree A classic BFS (level-order traversal) problem on trees. Problem idea: Find the level of the tree that has the maximum sum of node values. Key idea: Level-order traversal using a queue. Why? • We need to process nodes level by level • BFS naturally groups nodes by levels • Easy to compute sum for each level How it works: • Use a queue to perform BFS • For each level: → Process all nodes in current queue size → Calculate the sum of that level • Track the maximum sum and corresponding level • Return the smallest level with maximum sum Time Complexity: O(n) Space Complexity: O(n) Big takeaway: Whenever a problem involves level-wise processing in trees, BFS is the go-to approach. 🔥 Day 78 done. 🚀 #100DaysOfCode #LeetCode #DSA #Algorithms #BinaryTree #BFS #LevelOrder #Java #CodingJourney #ProblemSolving #InterviewPrep #TechCommunity
To view or add a comment, sign in
-
-
🚀 Day 15 / 85 Days of DSA Challenge Today’s problem was about applying range-based updates with a twist — stepping through the array using a custom interval (k) and applying modular multiplication. 💡 Key Learnings: Handling queries with non-contiguous updates (i += k pattern) Importance of modulo arithmetic to prevent overflow Final aggregation using bitwise XOR 🔍 Approach: For each query [l, r, k, v]: Start from index l Jump k steps each time Multiply elements by v (mod 1e9+7) Finally, compute XOR of the modified array. ⚡ This problem highlights how brute-force works but may need optimization for large constraints. Consistency > Perfection 💯 85 Days Challenge — Let’s go! 🔥 #DSA #CodingChallenge #Java #ProblemSolving #100DaysOfCode #LearningJourney
To view or add a comment, sign in
-
-
Day 77/100 🚀 | #100DaysOfDSA Solved LeetCode 349 – Intersection of Two Arrays today. The problem was to find the intersection of two arrays, returning only unique elements present in both. Approach: Used HashSet for efficient lookup and uniqueness. • Stored all elements of nums1 in a HashSet • Iterated through nums2 and checked if elements exist in the set • If yes, added them to a result HashSet (to avoid duplicates) • Converted the result set into an array This ensures only common and unique elements are included . Time Complexity: O(n + m) Space Complexity: O(n) Key takeaway: HashSet is very useful when dealing with uniqueness + fast lookup, especially in intersection-type problems. #100DaysOfDSA #LeetCode #DSA #Java #HashSet #Arrays #ProblemSolving #Consistency
To view or add a comment, sign in
-
-
🎉🎉 Day-37 of #60DaysOfDSA 🎉🎉 Problem: Count Increasing Subarrays 👉 Problem Statement: Given an array, count the number of strictly increasing subarrays of size ≥ 2. 👉 Approach Used (Greedy + Math Insight): ✔️ Traverse the array and track length of increasing subarray ✔️ If current element > previous → extend sequence ✔️ Otherwise → calculate subarrays from previous sequence ✔️ Formula used: For a sequence of length k → number of subarrays = k*(k-1)/2 👉 Steps: 1️⃣ Initialize count = 1 2️⃣ Traverse array 3️⃣ If increasing → count++ 4️⃣ Else → add (count*(count-1)/2) to result and reset 5️⃣ Add remaining sequence result 👉 Time Complexity: O(n) #problemsolving #java #dsa #geekstreak60
To view or add a comment, sign in
-
-
Today I solved LeetCode 958 – Check Completeness of a Binary Tree. 🧩 Problem Summary: Given the root of a binary tree, determine if it is a complete binary tree. A complete binary tree is defined as: All levels are completely filled except possibly the last level In the last level, nodes are as far left as possible 💡 Key Concepts Used: Binary Trees Breadth First Search (BFS) Queue data structure Tree properties 🧠 Approach: Use BFS (level order traversal) with a queue. Traverse nodes level by level. Once a null node is encountered: All following nodes must also be null. If a non-null node appears after a null, the tree is not complete. Otherwise, the tree satisfies completeness. ⏱ Time Complexity: O(N) — Each node is visited once. 📚 What I Learned: Understanding the properties of a complete binary tree. Using BFS to validate structural constraints. Handling null conditions carefully during traversal. Strengthening queue-based tree problem-solving. Building strong fundamentals in tree structures Consistency is key to mastering DSA #LeetCode #DSA #BinaryTree #CompleteBinaryTree #BFS #Queue #Java #CodingJourney #ProblemSolving #100DaysOfCode #TechGrowth
To view or add a comment, sign in
-
-
Yesterday's matrix problem was solved using traversal. Today's matrix problem was solved using Binary Search. Same topic. Different thinking. 🚀 Day 95/365 — DSA Challenge Solved: Search a 2D Matrix Key observation: The matrix is not just row-wise sorted. The first element of each row is greater than the last element of previous row. This means the matrix behaves like a sorted 1D array. So we can apply Binary Search. We convert index → row, col: row = mid / cols col = mid % cols And perform normal binary search. ⏱ Time: O(log(m * n)) 📦 Space: O(1) Day 95/365 complete. 💻 270 days to go. Code: https://lnkd.in/dad5sZfu #DSA #Java #LeetCode #BinarySearch #Matrix #LearningInPublic
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