🚀 DSA Consistency – Day 48 Today’s problem: Concatenation of Consecutive Binary Numbers 🔹 Problem Idea For a given n, concatenate the binary representations of numbers from 1 → n and return the decimal value modulo (10^9 + 7). Example: 1 → 1 2 → 10 3 → 11 Concatenation → 11011 💡 Key Intuition Instead of converting numbers to strings, we can use bit manipulation: • When a number becomes a power of 2, its binary length increases by 1 • Left shift the current result by the number of bits needed • Append the current number using addition Formula used: res = ((res << bits) + i) % mod ⚡ Why this works Every time we encounter a power of 2, the binary representation grows by one bit. We track this using: (i & (i - 1)) == 0 which efficiently checks if i is a power of two. ⏱ Complexity • Time: O(n) • Space: O(1) Consistency compounds. 48 days of showing up and solving. 💪 #DSA #LeetCode #Java #BitManipulation #Consistency #ProblemSolving
Day 48 DSA Consistency: Binary Concatenation Problem
More Relevant Posts
-
Imagine finding the longest number streak in an unsorted array. For example: [100, 4, 200, 1, 3, 2] Can you spot the longest consecutive sequence? 🚀 Day 67/365 — DSA Challenge Solved: Longest Consecutive Sequence The goal: Find the length of the longest sequence of consecutive numbers. Example: Input [100,4,200,1,3,2] Consecutive sequence: [1,2,3,4] Output: 4 💡 My Approach I solved it in two main steps. Step 1 — Sort the array Since the numbers are unsorted, I first sorted the array using bubble sort. Example after sorting: [1,2,3,4,100,200] Step 2 — Count consecutive numbers Then I looped through the array: • If current number = previous + 1 → increase count • If numbers are equal → skip duplicates • Otherwise → reset count While traversing, I kept track of the longest sequence length. This problem reminded me: Sometimes sorting first makes pattern detection much easier. Day 67/365 complete. Code 👇 https://lnkd.in/dad5sZfu #DSA #Java #LeetCode #LearningInPublic #ProblemSolving #Consistency
To view or add a comment, sign in
-
-
🚀 Day 43 of DSA |Check if Binary String Has at Most One Segment of Ones. Given a binary string without leading zeros, we need to check whether it contains at most one contiguous segment of '1's. Example Input: "1001" → Output: false Input: "110" → Output: true 💡 Key Insight If a '0' appears after the first segment of '1's and we encounter another '1', it means a new segment started. 🧠 Approach • Traverse the string • Once '0' appears after '1', mark it • If another '1' appears afterwards → return false ⏱ Time Complexity: O(n) 📦 Space Complexity: O(1) This problem highlights how pattern observation can simplify string traversal problems. #DSA #Java #LeetCode #CodingPractice #ProblemSolving
To view or add a comment, sign in
-
-
🔥 Day 356 – Daily DSA Challenge! 🔥 Problem: ⚡ Max Consecutive Ones III Given a binary array nums and an integer k, return the maximum number of consecutive 1s if you can flip at most k zeros. 💡 Key Insight — Sliding Window We maintain a window where the number of zeros is at most k. Core condition: 🧠 How It Works 🔹 Expand the window using right 🔹 Count zeros in the window 🔹 If zeros exceed k → shrink from left 🔹 At every step, window length is a valid answer ⚡ Algorithm Steps ✅ Initialize: left = 0, zero = 0, max = 0 ✅ Traverse with right: If nums[right] == 0 → increment zero While zero > k: shrink window from left Update max length ⚙️ Complexity ✅ Time Complexity: O(n) (each element processed once) ✅ Space Complexity: O(1) 💬 Challenge for you 1️⃣ Why does this work only for binary arrays? 2️⃣ How would you modify this for longest substring with at most k distinct characters? 3️⃣ What if we want to flip exactly k zeros instead of at most k? #DSA #Day356 #LeetCode #SlidingWindow #TwoPointers #Arrays #Java #ProblemSolving #KeepCoding
To view or add a comment, sign in
-
-
🚀 Day 79 – DSA Preparation 💪 Practiced an Array + Sliding Window problem focused on finding maximum average efficiently. Learned how to avoid recalculating sums using a fixed window technique. Great for improving optimization and window-based problem-solving 🔥 🧠 Problem 🔎 Maximum Average Subarray I (LeetCode 643) Given an integer array nums and an integer k, find a contiguous subarray of length k that has the maximum average value. 👉 Return the maximum average. 👉 Answers within 10⁻⁵ precision are accepted. Example Input: nums = [1,12,-5,-6,50,3], k = 4 Output: 12.75 Input: nums = [5], k = 1 Output: 5.0 Day 79 🔥 Getting better at sliding window and array optimization 🚀 #Day79 #DSA #LeetCode #SlidingWindow #Arrays #Java #ProblemSolving #Consistency
To view or add a comment, sign in
-
-
🚀 Day 62/365 — DSA Challenge Today's problem was simple in statement... but powerful in concept. Solved: 242. Valid Anagram The task: Given two strings s and t, return true if t is an anagram of s. An anagram means: Same characters. Same frequency. Different order allowed. 💡 My Approach Instead of sorting both strings, I used a frequency counter array of size 26. Steps: • If lengths differ -> return false • Increment count for each character in s • Decrement count for each character in t • If all values return to zero -> it's an anagram ⚡ Why this approach? Sorting -> O(n log n) Frequency count -> O(n) Time Complexity: O(n) Space Complexity: O(1) (fixed 26 letters) What I learned today Sometimes the optimal solution is just about counting correctly. Not every string problem needs sorting. Clean. Efficient. Intentional. Code 👇 https://lnkd.in/dad5sZfu #DSA #Java #LeetCode #LearningInPublic #Consistency #ProblemSolving
To view or add a comment, sign in
-
-
🧠 Day 168 — Alternating Binary String 🔁⚡ Today solved a clean string pattern problem: Minimum Changes to Make Alternating Binary String. 📌 Problem Goal Given a binary string, determine the minimum number of flips needed to make it alternating. Valid alternating patterns can only be: 010101... 101010... So the trick is simply to check both possibilities. 🔹 Approach ✔️ Traverse the string once ✔️ Count mismatches assuming pattern 0101... ✔️ Count mismatches assuming pattern 1010... ✔️ Return the minimum of the two counts 🧠 Key Learning ✔️ Many string problems reduce to pattern validation ✔️ Always check all possible valid patterns ✔️ Simple logic + observation can replace complex algorithms Sometimes the easiest problems still sharpen thinking. 🚀 Momentum Status: Consistency intact. Small problems, clear logic, steady progress. On to Day 169. #DSA #Strings #LeetCode #ProblemSolving #CodingJourney #Java #ConsistencyWins
To view or add a comment, sign in
-
-
🔥 Day 353 – Daily DSA Challenge! 🔥 Problem: 🔍 Single Element in a Sorted Array Given a sorted array where every element appears twice except one, find that single element in O(log n) time. 💡 Key Insight — Binary Search on Pairs In a perfect paired array: Pairs start at even indices Pattern breaks at the single element We use binary search to detect where this pattern breaks. 🧠 Core Observation Before the single element: pairs → (even, odd) After the single element: pairs shift → (odd, even) So we normalize mid: if mid is odd → make it even ⚡ Algorithm Logic ✅ Find mid ✅ Make mid even ✅ Compare: If nums[mid] == nums[mid+1] → move right Else → move left This narrows down to the single element. ⚙️ Complexity ✅ Time Complexity: O(log n) ✅ Space Complexity: O(1) 💬 Challenge for you 1️⃣ Why do we force mid to be even? 2️⃣ How would you solve this using XOR in O(n)? 3️⃣ What if elements appear thrice except one? #DSA #Day353 #LeetCode #BinarySearch #Arrays #Optimization #Java #ProblemSolving #KeepCoding
To view or add a comment, sign in
-
-
📘 DSA Journey — Day 10 Today’s focus: Prefix Sum and Sliding Window techniques. Problems solved: • Binary Subarrays With Sum (LeetCode 930) • Maximum Sum of Distinct Subarrays With Length K (LeetCode 2461) Concepts used: • Prefix Sum with HashMap • Sliding Window technique • Frequency tracking for distinct elements Key takeaway: In Binary Subarrays With Sum, the goal is to count the number of subarrays whose sum equals a given target. This can be solved efficiently using the prefix sum technique combined with a HashMap. As we iterate through the array, we keep track of the running sum and check how many times (currentSum - goal) has appeared before. This allows us to count valid subarrays in O(n) time. In Maximum Sum of Distinct Subarrays With Length K, a fixed-size sliding window is used. While moving the window across the array, we maintain a frequency map to ensure all elements in the window are distinct. If the window contains exactly k unique elements, we update the maximum sum. These problems highlight how recognizing the right pattern—prefix sums for counting subarrays and sliding windows for fixed-length constraints—can significantly reduce brute-force complexity. Continuing to strengthen pattern recognition and consistency in solving DSA problems. #DSA #Java #LeetCode #CodingJourney
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 42 of DSA 🚀 | Alternating Binary String Today’s problem: Minimum Changes To Make Alternating Binary String 🔹 Task: Convert a binary string into an alternating string (0101... or 1010...) using the minimum number of flips. 💡 Key Insight: Only two valid patterns exist: 010101... 101010... Count mismatches with one pattern and compute the other using: min(count, n - count) 📌 Example s = "0100" → Output = 1 ⏱ Time Complexity: O(n) 💾 Space Complexity: O(1) Small problem, but a great reminder that recognizing patterns can simplify the solution drastically. #DSA #Java #LeetCode #ProblemSolving #LearningInPublic #Consistency
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