Day 17/30 – LeetCode streak Today’s problem: Find Kth Bit in Nth Binary String (1545). 'S₁ = "0"', and 'Sₙ = Sₙ₋₁ + "1" + reverse(invert(Sₙ₋₁))'. Length of 'Sₙ' is always '2ⁿ - 1', with a '1' sitting exactly in the middle. The structure looks like this: Sₙ = [Sₙ₋₁] + '1' + reverse(invert(Sₙ₋₁)) So for any position 'k': - Let 'mid = 2^(n-1)' (the middle index, 1-based). - If 'k == mid', answer is '1'. - If 'k < mid', it’s in the left half, which is exactly 'Sₙ₋₁' → recurse: 'findKthBit(n-1, k)'. - If 'k > mid', it’s in 'reverse(invert(Sₙ₋₁))'. Mirror it back into 'Sₙ₋₁' at 'mirror = 2*mid - k' (same as '2ⁿ - k'), recursively get that bit from 'Sₙ₋₁', then flip it. Day 17 takeaway: This is one of those problems where drawing the recursive construction once (left + 1 + reversed/inverted left) makes the “mid / mirror / invert” recursion feel natural—and you never have to actually build the string. #leetcode #dsa #java #recursion #consistency
LeetCode Day 17: Find Kth Bit in Nth Binary String
More Relevant Posts
-
Day 22/30 – LeetCode streak Problem: Find Unique Binary String You are given 'n' distinct binary strings of length 'n' and must return any binary string of length 'n' that doesn’t appear in the array. Core idea (Cantor’s diagonal argument) * Think of 'nums' as an 'n × n' matrix of characters. * Look at the diagonal positions: character 'i' of string 'nums[i]'. * Build a new string where the 'i'-th character is the flip of 'nums[i].charAt(i)' ('0' → '1', '1' → '0'). * This constructed string differs from 'nums[0]' at index '0', from 'nums[1]' at index '1', …, from 'nums[i]' at index 'i', so it cannot be equal to any string in 'nums'. * Time complexity: 'O(n)', space: 'O(n)' for the output string. Day 22 takeaway: This problem is a neat, direct application of Cantor’s diagonalization: by flipping the diagonal bits, you guarantee a new string that is different from every given one in at least one position, without any searching or backtracking. #leetcode #dsa #java #strings #consistency
To view or add a comment, sign in
-
-
Classical Backtracking Problem with a Classical Approach Solved Word Search (LeetCode 79) using a clean and intuitive DFS + Backtracking strategy Approach (Simple & Clear): •Start from every cell that matches the first character of the word. •Use DFS (Depth-First Search) to explore all 4 directions (up, down, left, right). •Mark the current cell as visited (by replacing it temporarily) to avoid revisiting. •If the full word is matched → return true immediately. •Backtrack by restoring the cell when the path doesn’t work. Key Insight: We explore all possible paths, but prune early when characters don’t match — this keeps the solution efficient. ⏱️ Time Complexity: 👉 O(m × n × 4^L) •m × n → starting points •4^L → exploring 4 directions for each character of length L 📌 Space Complexity: 👉 O(L) (recursion stack) 🔥 Clean recursion + smart backtracking = problem cracked! #DSA #Backtracking #LeetCode #CodingInterview #Java #ProblemSolving
To view or add a comment, sign in
-
-
Day 33 of Daily DSA 🚀 Solved LeetCode 58: Length of Last Word ✅ Problem: Given a string s, return the length of the last word (a sequence of non-space characters). Approach: First, trim() the string to remove trailing spaces Traverse the string from the end Count characters until a space is encountered 💡 Key Insight: Instead of splitting the string (which uses extra space), we can simply iterate backwards for an efficient solution. ⏱ Complexity: • Time: O(n) • Space: O(1) 📊 LeetCode Stats: • Runtime: 0 ms (Beats 100%) ⚡ • Memory: 43.17 MB A simple yet important problem to strengthen string traversal techniques. #DSA #LeetCode #Java #Strings #ProblemSolving #CodingJourney #Consistency
To view or add a comment, sign in
-
-
🔥 Day-11 — Longest Substring Without Repeating Characters 🧩 Problem: Longest Substring Without Repeating Characters 💻 Platform: LeetCode (#3) This problem forced me to truly understand the Sliding Window pattern. Brute force checks every substring. That’s inefficient. Optimal approach: ✔ Use two pointers (left & right) ✔ Expand the window ✔ Shrink when duplicate appears ✔ Track max window size Time Complexity: O(n) Space Complexity: O(n) Big takeaway: If a problem mentions “longest substring” + “no repetition” → Sliding Window should be your first thought. Strings aren’t new logic. They’re just arrays of characters. Day-11 complete. Moving deeper into string patterns 🚀 #30DaysOfCode #LeetCode #DSA #Java #SlidingWindow #Algorithms #ProblemSolving #Developers
To view or add a comment, sign in
-
-
#100DaysOfCode 🚀 👉Solved: LeetCode 75 – Sort Colors The goal was to sort an array containing only three values: 0 (Red), 1 (White), and 2 (Blue) At first glance this looks like a simple sorting problem. But the twist: ❌ No built-in sort ✔ One pass ✔ Constant space The solution uses the famous **Dutch National Flag Algorithm**. Instead of sorting normally, we maintain three pointers: • low → for 0s • mid → current element • high → for 2s Rules: • If nums[mid] == 0 → swap with low • If nums[mid] == 1 → move mid forward • If nums[mid] == 2 → swap with high This allows sorting the array in a single pass. 📌 Key Points: ✔ In-place sorting ✔ One pass algorithm ✔ Constant space usage ✔ Classic three-pointer technique ⏱ Time Complexity: O(n) 📦 Space Complexity: O(1) Problems like this show how powerful pointer techniques can be. Day 15✅ #DSA #Java #LeetCode #Algorithms #ProblemSolving #100DaysOfCode #CodingJourney #LearnInPublic
To view or add a comment, sign in
-
-
📌 LeetCode Daily Challenge — Day 26 Problem: 3548. Equal Sum Grid Partition II Topic: Array, Matrix, Prefix Sum, HashSet, Greedy 📌 Quick Problem Sense: You're given an m × n integer grid. Make one straight cut, horizontal or vertical to split it into two non-empty parts with equal sums. The twist? You're allowed to remove at most one border element (right at the cut edge) from either side to balance the sums. Return true if such a partition is possible! 🧠 Approach (Simple Thinking): 🔹 Compute the total sum of the grid upfront 🔹 Scan row by row, maintain a running top sum, derive bottom = total - top 🔹 The imbalance is diff = top - bottom 🔹 A cut is valid if: diff == 0 → already perfectly balanced ✅ diff equals a corner cell of the top section ✅ diff equals the left-border cell of the current bottom row ✅ diff exists in a HashSet of all values seen so far ✅ 🔹 Use a HashSet to track all border values already scanned, O(1) lookup to check if removing one element fixes the imbalance 🔹 For vertical cuts → simply transpose the matrix and reuse the same horizontal cut logic! 🔹 Also try reversed row order to cover cuts scanned from the opposite direction, 4 passes total, all reusing the same function 🔄 ⏱️ Time Complexity: 4 passes through the grid (original, reversed, transposed, transposed+reversed) → O(m × n) Single scan per pass, HashSet lookups in O(1) — clean and efficient! 📦 Space Complexity: HashSet of seen values → O(m × n) worst case Transpose grid → O(m × n) Overall → O(m × n) I wrote a full breakdown with dry run, real-life analogy, and step-by-step code walkthrough here 👇 https://lnkd.in/g2FHaT4S If you solved it by explicitly enumerating only the border cells, or used a different balance-check trick, drop it in the comments, always curious to see how others think about it 💬 See you in the next problem 👋 #LeetCode #DSA #CodingChallenge #Java #ProblemSolving #Programming
To view or add a comment, sign in
-
-
𝐃𝐚𝐲 𝟓𝟒 – 𝐃𝐒𝐀 𝐉𝐨𝐮𝐫𝐧𝐞𝐲 | 𝐀𝐫𝐫𝐚𝐲𝐬 🚀 Today’s problem focused on finding the minimum element in a rotated sorted array using binary search. 𝐏𝐫𝐨𝐛𝐥𝐞𝐦 𝐒𝐨𝐥𝐯𝐞𝐝 • Find Minimum in Rotated Sorted Array 𝐀𝐩𝐩𝐫𝐨𝐚𝐜𝐡 • Used binary search to reduce the search space • Compared the middle element with the rightmost element Logic: • If nums[mid] > nums[right] → minimum lies in the right half • Else → minimum lies in the left half (including mid) • Continued until left meets right 𝐊𝐞𝐲 𝐋𝐞𝐚𝐫𝐧𝐢𝐧𝐠𝐬 • Rotated arrays require a modified binary search • Comparing with boundary elements helps identify the sorted half • Binary search is not only for exact matches • Reducing the search space is the core idea 𝐂𝐨𝐦𝐩𝐥𝐞𝐱𝐢𝐭𝐲 • Time: O(log n) • Space: O(1) 𝐓𝐚𝐤𝐞𝐚𝐰𝐚𝐲 Binary search is about asking the right question — not just finding the middle. 54 days consistent 🚀 On to Day 55. #DSA #Arrays #BinarySearch #LeetCode #Java #ProblemSolving #DailyCoding #LearningInPublic #SoftwareDeveloper
To view or add a comment, sign in
-
-
🚀 DSA Practice – Longest Subarray with Sum = K (Positive Numbers) Today I practiced a classic Sliding Window / Two Pointer problem. Problem: Find the longest subarray whose sum equals K, when the array contains only positive numbers. 💡 Key Idea: * Since all numbers are positive, we can use the sliding window technique: Expand the window by moving the right pointer. * If the sum becomes greater than k, shrink the window using the left pointer. Whenever sum == k, update the maximum length. ⚡ Time Complexity: O(n) ⚡ Space Complexity: O(1) Example: arr = [1, 2, 3, 1, 1, 1, 1, 4, 2, 3] k = 3 Longest subarray: [1, 1, 1] Length = 3 This problem is a great example of how understanding constraints (positive numbers) allows us to replace complex approaches like HashMap + Prefix Sum with a simpler and more efficient Sliding Window technique. #DSA #Algorithms #Java #SlidingWindow #LeetCode #SoftwareEngineering
To view or add a comment, sign in
-
-
🧠 Day 181 — Count Submatrices with Top-Left Constraint 📊⚡ Today solved a very interesting 2D Prefix Sum problem involving matrices. 📌 Problem Goal Count the number of submatrices that: ✔️ Always include the top-left element (0,0) ✔️ Have a sum ≤ k 🔹 Core Idea Since every valid submatrix must include the top-left corner, the problem simplifies to: 👉 Compute sum of submatrices from (0,0) to (i,j) This can be efficiently done using a 2D Prefix Sum array. 🔹 Prefix Sum Thinking Each cell represents: Sum of all elements from (0,0) to (i,j) This allows us to compute submatrix sums in O(1) after preprocessing. 🔹 Transition Idea To compute prefix sum at a cell: Top + Left − Overlap + Current Cell This ensures we don’t double-count overlapping areas. 🔹 Approach 1️⃣ Build the prefix sum matrix 2️⃣ For every cell (i,j), calculate sum from (0,0) to (i,j) 3️⃣ If sum ≤ k → increment count 🧠 Key Learning ✔️ 2D Prefix Sum is powerful for submatrix sum queries ✔️ Fixing one corner simplifies the problem significantly ✔️ Avoid recomputation using preprocessed values This concept is widely used in: • Range sum queries • Matrix problems • Image processing • Competitive programming 💡 Big Realization When a problem involves fixed boundaries, always think: 👉 “Can prefix sum reduce complexity?” It often converts a complex problem into a simple traversal. 🚀 Momentum Status: Matrix + Prefix Sum concepts getting strong now. On to Day 182. #DSA #PrefixSum #Matrix #LeetCode #Java #CodingJourney #ProblemSolving #ConsistencyWins
To view or add a comment, sign in
-
-
Day 38 of Daily DSA 🚀 Solved LeetCode 1897: Redistribute Characters to Make All Strings Equal ✅ Problem: Given an array of strings, determine if you can make every string equal by moving any character from one string to any position in another string. Rules: * You can pick two distinct indices i and j and move any character from words[i] to any position in words[j] * Any number of operations are allowed * Return true if all strings can be made equal, false otherwise Approach: Used a HashMap to count the total frequency of every character across all strings. If every character's count is divisible by the number of strings, equal redistribution is possible. Steps: 1. Concatenate all strings into one using StringBuilder 2. Count the frequency of each character using a HashMap 3. For every character count, check if it is divisible by the number of words 4. If any count is not divisible → return false 5. Otherwise → return true ⏱ Complexity: • Time: O(n * m) — n = number of words, m = average word length • Space: O(1) — at most 26 characters in the map 📊 LeetCode Stats: • Runtime: 12 ms (Beats 17.93%) • Memory: 46.63 MB A brilliant insight — redistribution is just a divisibility check! No complex simulation needed. #DSA #LeetCode #Java #HashMap #Strings #CodingJourney #ProblemSolving
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