🚀 𝗗𝗔𝗬 𝟭𝟭: 𝗡𝗘𝗦𝗧𝗘𝗗 𝗙𝗢𝗥 𝗟𝗢𝗢𝗣𝗦 – 𝗠𝗔𝗞𝗜𝗡𝗚 𝗦𝗘𝗡𝗦𝗘 𝗢𝗙 𝗣𝗔𝗧𝗧𝗘𝗥𝗡𝗦 🚀 Day 11 was all about Nested for Loops , and this topic changed how I look at pattern programs completely 💡 Earlier, I used to see patterns as something to memorise. Today I realized they are simply about understanding loop flow. 🔹 𝗪𝗛𝗔𝗧 𝗗𝗢𝗘𝗦 𝗡𝗘𝗦𝗧𝗘𝗗 𝗙𝗢𝗥 𝗟𝗢𝗢𝗣 𝗠𝗘𝗔𝗡? Nested for loop means writing one for loop inside another. • The outer loop decides how many times the structure repeats. • The inner loop decides what gets printed in each repetition. In simple words: 👉 One outer loop run = one complete inner loop run. 🔹 𝗨𝗡𝗗𝗘𝗥𝗦𝗧𝗔𝗡𝗗𝗜𝗡𝗚 𝗧𝗛𝗘 𝗙𝗟𝗢𝗪 A simple way to think: • Outer loop → number of lines • Inner loop → content inside each line If the outer loop runs 5 times and the inner loop runs 4 times, the inner code executes 20 times. 🔹 𝗣𝗥𝗔𝗖𝗧𝗜𝗖𝗔𝗟 𝗨𝗦𝗘 𝗖𝗔𝗦𝗘𝗦 Nested loops are commonly used when: • Printing star or number patterns • Working with tables • Handling matrix-style problems Whenever there is a row–column structure, nested loops are involved. 🔹 𝗟𝗘𝗦𝗦𝗢𝗡𝗦 𝗜 𝗟𝗘𝗔𝗥𝗡𝗘𝗗 𝗧𝗢𝗗𝗔𝗬 • Patterns don’t need memorization • Dry-running helps more than writing code directly • Understanding the loop flow saves a lot of confusion ✨ 𝗞𝗘𝗬 𝗧𝗔𝗞𝗘𝗔𝗪𝗔𝗬 Nested loops become easy once we stop focusing on symbols and start focusing on logic. One concept at a time. One step at a time 🚀 #Java #CoreJava #NestedLoops #ForLoop #JavaLearning #LearningInPublic #100DaysOfCode
Understanding Nested Loops in Java: Mastering Loop Flow
More Relevant Posts
-
🚀 𝗗𝗔𝗬 𝟭𝟮: 𝗔𝗣𝗣𝗟𝗬𝗜𝗡𝗚 𝗡𝗘𝗦𝗧𝗘𝗗 𝗙𝗢𝗥 𝗟𝗢𝗢𝗣𝗦 𝗧𝗢 𝗣𝗔𝗧𝗧𝗘𝗥𝗡𝗦 🚀 After understanding Nested for Loops on Day 11, today I focused on applying the concept through simple pattern programs ⭐ This is where the real clarity started. 🔹 𝗙𝗥𝗢𝗠 𝗖𝗢𝗡𝗖𝗘𝗣𝗧 𝗧𝗢 𝗖𝗢𝗗𝗘 💡 Yesterday I understood: • Outer loop → controls rows • Inner loop → controls columns Today I applied that knowledge to print a simple pattern like: * ** *** **** Instead of memorizing it, I asked myself: 👉 How many rows are required? 👉 How many stars in each row? 👉 When should the line break happen? Once these answers were clear, writing the nested loop became easy. 🔹 𝗪𝗛𝗔𝗧 𝗜 𝗥𝗘𝗔𝗟𝗜𝗭𝗘𝗗 🧠 Patterns are not about stars. They are about understanding how loops repeat work in a structured way. For every single iteration of the outer loop, the inner loop runs completely. That repetition creates the visual structure. ✨ 𝗞𝗘𝗬 𝗟𝗘𝗔𝗥𝗡𝗜𝗡𝗚 🚀 When the structure of loops is clear, patterns become logical instead of confusing. Small improvements every day lead to strong fundamentals. #Java #CoreJava #NestedForLoop #PatternPrograms #JavaLearning #LearningInPublic #100DaysOfCode
To view or add a comment, sign in
-
-
🚀 𝗗𝗔𝗬 𝟭𝟭: 𝗡𝗘𝗦𝗧𝗘𝗗 𝗙𝗢𝗥 𝗟𝗢𝗢𝗣𝗦 – 𝗨𝗡𝗗𝗘𝗥𝗦𝗧𝗔𝗡𝗗𝗜𝗡𝗚 𝗣𝗔𝗧𝗧𝗘𝗥𝗡 𝗟𝗢𝗚𝗜𝗖 🚀 Day 11 was all about Nested for Loops , and this topic finally helped me understand how pattern programs actually work 🧠✨ Earlier, nested loops looked confusing, but once I understood the responsibility of each loop, things became much clearer. 🔹 𝗪𝗛𝗔𝗧 𝗜𝗦 𝗔 𝗡𝗘𝗦𝗧𝗘𝗗 𝗙𝗢𝗥 𝗟𝗢𝗢𝗣? A nested for loop means having 👉 one for loop inside another for loop. • The outer loop controls how many times the overall block runs • The inner loop controls what happens inside each run In simple terms: 👉 For every single iteration of the outer loop, the inner loop runs completely. 🔹 𝗛𝗢𝗪 𝗡𝗘𝗦𝗧𝗘𝗗 𝗟𝗢𝗢𝗣𝗦 𝗪𝗢𝗥𝗞 It helps to think in terms of rows and columns: • Outer loop → rows • Inner loop → columns So if the outer loop runs 4 times and the inner loop runs 3 times, the inner logic executes 12 times in total. 🔹 𝗪𝗛𝗘𝗥𝗘 𝗗𝗢 𝗪𝗘 𝗨𝗦𝗘 𝗡𝗘𝗦𝗧𝗘𝗗 𝗟𝗢𝗢𝗣𝗦? Nested loops are commonly used in: • Pattern printing • Table generation • Matrix operations • Comparing multiple values Anytime a problem involves rows and columns, nested loops are involved. 🔹 𝗖𝗢𝗠𝗠𝗢𝗡 𝗕𝗘𝗚𝗜𝗡𝗡𝗘𝗥 𝗠𝗜𝗦𝗧𝗔𝗞𝗘𝗦 • Confusing the roles of outer and inner loops • Writing code without dry-running • Jumping to complex patterns too early Dry-running the loop flow step by step makes understanding much easier. ✨ 𝗞𝗘𝗬 𝗟𝗘𝗔𝗥𝗡𝗜𝗡𝗚 Nested loops are not difficult — they just need clear thinking and patience. Once the loop flow is understood, pattern programs become logical instead of confusing 🚀 #Java #CoreJava #NestedForLoop #JavaLoops #ProgrammingBasics #LearningInPublic #100DaysOfCode
To view or add a comment, sign in
-
-
🔥 Day 87 of #100DaysOfCode Today’s challenge: LeetCode: Implement Stack using Queues 📚🔁 📌 Problem Summary Implement a stack (LIFO) using only queue operations: push(x) pop() top() empty() You can only use standard queue operations (offer, poll, peek, isEmpty). 🧠 Approach: Two Queues Since stack is LIFO and queue is FIFO, we simulate stack behavior by rearranging elements during push. ⚙️ Strategy Used: Move all elements from q1 → q2 Add new element to q1 Move everything back from q2 → q1 Now: The newest element is always at the front of q1 pop() becomes simple → just q1.poll() 🔁 Key Idea Make push() costly Make pop() O(1) ⏱ Time Complexity push() → O(n) pop() → O(1) top() → O(1) empty() → O(1) 💾 Space Complexity: O(n) ⚡ Performance Runtime: 0 ms 100% faster than other submissions 🚀 💡 What I Learned Data structures can simulate each other. The trick is choosing where to handle the complexity. Understanding internal behavior matters more than memorizing code. Stack → Queue → Simulation mastery growing stronger 💪 On to Day 88 🔥 #100DaysOfCode #LeetCode #Stack #Queue #Java #DSA #InterviewPrep
To view or add a comment, sign in
-
-
📌 LeetCode Daily Challenge — Day 3 Problem: 1545. Find Kth Bit in Nth Binary String Topic: String, Recursion, Divide and Conquer 🧠 Approach (Simple Thinking): 🔹 Every string Sn is made of 3 parts: The previous string S(n-1) on the left A single "1" sitting right in the middle The flipped and reversed version of S(n-1) on the right 🔹 The string doubles in size every level by S20 you're looking at 1M+ characters. Building it fully? Not a chance. So instead, we recursively figure out which part position k falls into 🔹 If k is in the left half → it behaves exactly like S(n-1). Just recurse with the same k and go one level down 🔹 If k is exactly the middle → no need to recurse at all. The middle is always '1', guaranteed by construction 🔹 If k is in the right half → the right half is a mirror of the left half but with all bits flipped. So we calculate the mirrored position, recurse into the left half to get that bit, and then flip the answer 🔹 Base case → when n = 1, the string is just "0". Return it and start unwinding ⏱️ Time Complexity: Recurse at most n levels deep → O(n) n = the level of the binary string 📦 Space Complexity: Recursive call stack depth is n → O(n) No extra arrays or data structures created 📝 Put together a full walkthrough covering the approach, dry run, and code explanation. 👉 check it out here: https://lnkd.in/gGrReU9W Got a different way to tackle this? Always curious to see alternate approaches share it in the comments below! 🙌 Until the next one, happy coding! 🚀 #LeetCode #Java #SoftwareEngineer #ProblemSolving #BackendDeveloper
To view or add a comment, sign in
-
-
🚀 Day 523 of #750DaysOfCode 🚀 📌 Problem Solved: Check if Binary String Has at Most One Segment of Ones (LeetCode 1784) 🔎 Problem Summary: Given a binary string s (without leading zeros), we need to check whether the string contains at most one contiguous segment of 1s. If the 1s appear in more than one separate segment, we return false. (LeetCode) 💡 Key Insight: Since the string starts with 1, the only valid structure is: 111...000... If we ever encounter the pattern "01", it means a 1 appeared again after a 0, which creates multiple segments of ones. (AlgoMonster) ⚙️ Approach: Traverse the string or simply check if "01" exists. If "01" is found → more than one segment → return false. ⏱ Complexity: Time: O(n) Space: O(1) 📚 Takeaway: Sometimes string problems become much easier when we identify patterns instead of counting segments. Consistency > Motivation. On to Day 524 tomorrow. 💪 #LeetCode #Java #DataStructures #Algorithms #CodingChallenge #Consistency #ProblemSolving #100DaysOfCode #750DaysOfCode
To view or add a comment, sign in
-
-
🔥 Day 84 of #100DaysOfCode Today’s problem: LeetCode: Find K Closest Elements 🎯 📌 Problem Summary Given: A sorted array arr An integer k A target value x Return the k closest integers to x in the array. The result must be sorted in ascending order. Example: arr = [1,2,3,4,5], k = 4, x = 3 Output → [1,2,3,4] 🧠 Approach: Two Pointers Shrinking Window Since the array is already sorted, we can: Start with: l = 0 r = arr.length - 1 While the window size is bigger than k: Compare distances: |x - arr[l]| |x - arr[r]| Remove the element that is farther from x ⚙️ Core Idea: while (r - l >= k): if left is closer: r-- else: l++ Finally, return elements from l to r. ⏱ Time Complexity: O(n - k) 💾 Space Complexity: O(1) (excluding result list) 🚀 Performance ⚡ Runtime: 4ms (98% faster) Solid optimization using the sorted property of the array. 💡 What I Learned When array is sorted → always think Two Pointers. Instead of building result, shrink unnecessary elements. Cleaner logic often gives better performance. Consistency > Motivation. On to Day 85 🔥 #100DaysOfCode #LeetCode #TwoPointers #Java #DSA #CodingJourney #InterviewPrep
To view or add a comment, sign in
-
-
#100DaysOfLeetcode journey 🚀 Day 18/100 — Custom Sorting & Bit Counting! Today’s Problem: 1356. Sort Integers by The Number of 1 Bits 🔹 The Goal: Today’s challenge was about reorganizing data based on hidden properties. The task: sort an array in ascending order based on the number of set bits (1s) in their binary representation. If two numbers have the same count, their actual numerical value breaks the tie. 🔹 The Insight: This problem highlights the power of Custom Comparators. Instead of sorting by value alone, we peek "under the hood" at the binary level. In Java, Integer.bitCount(n) is the star of the show—it uses a highly optimized bit-masking algorithm (based on the Wegner estimate) to count 1s in $O(1)$ time. 🔹 The Efficiency: By using a custom comparator, we can achieve $O(N \log N)$ time complexity. While primitive int[] arrays don't support custom comparators directly, boxing them into Integer[] or using a mathematical trick (like weight = bitCount * 10001 + value) allows us to sort based on multiple criteria in a single pass. ✨ Achievement: Successfully bridged the gap between Sorting Algorithms and Bitwise Analysis. It’s a great reminder that how we represent data (Decimal vs. Binary) completely changes how we can organize it! 🔍 Steps followed: ✔ Bit Calculation: Used Integer.bitCount() for lightning-fast 1-bit tallying. ✔ Tie-Breaking Logic: Implemented a comparator that prioritizes bit count, then numerical value. ✔ Array Transformation: Managed the conversion between primitive and object types to leverage Java’s Arrays.sort(). 🔧 Complexity Analysis: Time Complexity: $O(N \log N)$ Space Complexity: $O(N)$ (for boxing/storing the result) Halfway through the third week! The more I dive into bitwise problems, the more I realize how much performance can be squeezed out of simple operations. 🛠️ #Java #DSA #LeetCode #100DaysOfCode #SoftwareEngineer #BitManipulation #SortingAlgorithms #CodingLife #CareerGrowth
To view or add a comment, sign in
-
-
🚀 Day 78 / 100 – LeetCode Daily Challenge 🧠 Problem: Triconic Subarray Maximum Sum 📅 Date: March 7, 2026 🏆 Runtime: 3 ms | Beats 99.94% 📦 Memory: 95.28 MB | Beats 46.16% 📝 Problem Insight Today’s challenge was to find the maximum sum of a triconic subarray – a sequence that first decreases, then increases, and finally decreases again. It’s like a "mountain" with two peaks and one valley in between, but in a specific order: down → up → down. This is a more complex variant of the classic mountain or bitonic subarray problems. It requires careful scanning of the array to detect valid triconic patterns and compute their sums efficiently. 💡 My Approach I used a two-pointer expansion method: Iterate through the array and treat each index as a potential peak or valley. Expand left and right while the pattern matches the triconic property. Keep track of the maximum sum encountered. Although the code snippet is incomplete here, the full solution involves: Precomputing left and right decreasing/increasing trends. Validating the three-phase pattern for each possible center. Avoiding redundant computations to keep the time complexity close to O(n). 📊 Results ✅ 861 / 861 test cases passed ⚡ Runtime: 3 ms (beats 99.94% of Java submissions) 📈 Memory: 95.28 MB (beats 46.16%) 🧠 Key Takeaway Pattern recognition problems like this one are great for sharpening your array traversal logic and understanding how to break down complex patterns into manageable checks. The challenge is not just in finding the sum, but in ensuring the pattern holds throughout. 🔗 Let’s Connect! I’m documenting my #100DaysOfCode journey every day – follow along for more problem-solving insights, optimizations, and LeetCode grind! 💻⚡ #LeetCode #Java #CodingChallenge #100DaysOfCode #Day78 #TriconicArray #ProblemSolving #TechJourney #SoftwareEngineering #Algorithms #DataStructures #CodeNewbie #DevCommunity #Programming
To view or add a comment, sign in
-
-
🚀 Day 521 of #750DaysOfCode 🚀 🔎 1582. Special Positions in a Binary Matrix (LeetCode - Easy) Today I solved a simple yet interesting matrix traversal problem. 🧠 Problem Summary We are given an m × n binary matrix. A position (i, j) is called special if: ✔ mat[i][j] == 1 ✔ All other elements in row i are 0 ✔ All other elements in column j are 0 Our task is to count how many such special positions exist. 💡 Key Idea Instead of checking the entire row and column every time, we can: 1️⃣ Count the number of 1s in every row 2️⃣ Count the number of 1s in every column Then a cell (i, j) is special if: mat[i][j] == 1 rowCount[i] == 1 colCount[j] == 1 ⏱ Time Complexity O(m × n) We traverse the matrix twice. 🧩 What I Learned ✔ Efficiently using row and column counting ✔ Simplifying matrix conditions with precomputation ✔ Writing cleaner solutions for matrix-based problems Step by step progress every day. Consistency is the real key. 🚀 #LeetCode #Java #MatrixProblems #ProblemSolving #DataStructures #CodingJourney #750DaysOfCode #Day521
To view or add a comment, sign in
-
-
🚀 Day 17/100 – Sorted Squares (Two Pointer Approach) Today I solved the “Sorted Squares” problem on LeetCode. 🔹 Problem: Given a sorted array (non-decreasing order), return an array of the squares of each number, also sorted. 🔹 Brute Force Idea: 1. Square each element. 2. Sort the array again. Time Complexity: O(n log n) ❌ (Not optimal because sorting takes extra time.) 🔹 Optimal Approach (Two Pointer Technique): Since the array is already sorted, the largest square will come from either: - The leftmost negative number - Or the rightmost positive number So I used: • left pointer at start • right pointer at end • filled the result array from the back At each step: Compare absolute values Place the larger square at the current index Move the corresponding pointer ⏱ Time Complexity: O(n) 📦 Space Complexity: O(n) ✨ Key Learning: Instead of blindly sorting again, understanding the pattern of negative numbers helped me optimize the solution. #Day17 #100DaysOfCode #LeetCode #Java #DataStructures #Arrays.
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