Day 14/30 – LeetCode streak Today’s problem: Concatenation of Consecutive Binary Numbers You need the decimal value of '1' + '2' + '3' + ... + 'n' written in binary back-to-back, all under mod (10^9 + 7). Core trick: treat “concatenate in binary” as shift + OR: * When you append 'i' to the right, you’re really shifting the current result left by 'bits(i)' and OR-ing 'i' into the free space. * The number of bits only increases when 'i' hits a power of two (1, 2, 4, 8, …), so you just track 'bits' and bump it whenever '(i & (i - 1)) == 0'. Day 14 takeaway: Once you see that “stick this binary to the right” is the same as “shift by its bit length and OR”, the whole problem becomes a clean for-loop plus the power-of-two trick—no string building or big integer juggling needed. #leetcode #dsa #java #bitmanipulation #consistency
LeetCode Streak Day 14: Binary Concatenation Solution
More Relevant Posts
-
Day 8 Today I solved “Check if Binary String Has at Most One Segment of Ones” on LeetCode. At first glance, the problem looks simple: Given a binary string, check whether it contains only one continuous segment of '1's. Example: "110" → Valid (one segment of 1s) "1001" → Invalid (two separate segments of 1s) While solving it, I realized an interesting observation: 👉 If a binary string has more than one segment of 1s, the pattern "01" must appear before another "1". So instead of counting segments explicitly, we can simply check whether '01' appears and is followed by another '1'. This turns the problem into a very efficient linear scan with O(n) time complexity. 💡 Key takeaway: Sometimes the simplest solution comes from recognizing patterns in the string rather than simulating the whole process. Also happy to see my solution running at 0 ms runtime (100% faster) 🚀 #LeetCode #DSA #ProblemSolving #Java #CodingJourney #BinaryStrings
To view or add a comment, sign in
-
-
Day 7/100 – LeetCode Challenge Problem: Palindrome Number Today’s problem focused on number manipulation without converting the integer into a string. Approach: If number is negative → return false Reverse the digits using modulo (% 10) and division (/ 10) Compare reversed number with original number Logic Used: Extract last digit: rem = x % 10 Build reversed number: rev = rev * 10 + rem Remove last digit: x = x / 10 Complexity: Time: O(log₁₀ n) Space: O(1) Key takeaway: Problems involving digits can often be solved mathematically without string conversion. #100DaysOfCode #LeetCode #DSA #Java #ProblemSolving #CodingJourney
To view or add a comment, sign in
-
-
🚀 Day 23/100 – LeetCode Challenge Today’s problem: Partitioning Into Minimum Number of Deci-Binary Numbers 🔹 Key Insight: The minimum number of deci-binary numbers required is equal to the maximum digit present in the string. 🔹 Approach: Traverse through each character in the string Convert it to integer (ch - '0') Track the maximum digit Return the maximum value 🔹 Time Complexity: O(n) 🔹 Space Complexity: O(1) ✨ Simple logic, but powerful observation! Instead of constructing numbers, we just analyze the digits. Consistency > Motivation 💪 #Day23 #100DaysOfCode #LeetCode #Java #ProblemSolving #CodingJourney
To view or add a comment, sign in
-
-
Day: 53/365 📌 LeetCode POTD: Concatenation of Consecutive Binary Numbers Medium Key takeaways/Learnings from this problem: 1. This problem nicely mixes math + bit manipulation, especially understanding how many bits each number contributes. 2. Instead of literally concatenating strings, shifting the current result left by the bit-length is way more efficient. 3. It reinforces the idea that log2 helps you find bit length quickly for each number. 4. Big takeaway: always think in terms of binary operations when the problem screams “binary” — strings are usually a trap here. #POTD #365DaysOfCode #DSA #Java #ProblemSolving #LearningInPublic #Consistency 🥷
To view or add a comment, sign in
-
-
Day 9/100 – LeetCode Challenge Problem: 3Sum Today’s challenge was to find all unique triplets in an array whose sum equals 0. Approach: Sort the array to efficiently apply the two-pointer technique. Fix one element nums[i] and use two pointers (left and right) to find the remaining two numbers. If the sum equals 0, store the triplet. Skip duplicate elements to ensure only unique triplets are included. Key Idea: Sorting + Two Pointers helps reduce the brute-force O(n³) approach to O(n²). Concepts Practiced: Sorting Two-pointer technique Duplicate handling Array traversal optimization #100DaysOfCode #LeetCode #DSA #Java #TwoPointers #ProblemSolving #CodingJourney
To view or add a comment, sign in
-
-
Day 6/100 – LeetCode Challenge 🚀 Problem: #189 Rotate Array Difficulty: Medium Language: Java Approach: Array Reversal Technique Time Complexity: O(n) Space Complexity: O(1) 🔍 Key Insight: Instead of shifting elements one by one, the array can be rotated efficiently using a three-step reversal strategy. Steps: 1️⃣ Reverse the entire array 2️⃣ Reverse the first k elements 3️⃣ Reverse the remaining elements This achieves the required rotation in-place with constant extra space. 🧠 Solution Brief: Calculated k % n to handle cases where k is greater than array length. Reversed the entire array first. Then reversed the first k elements and the remaining n-k elements. This sequence correctly rotates the array to the right. 📌 What I Learned: Understanding patterns like array reversal can simplify problems that initially seem complex. Optimizing from brute force shifting to an in-place O(n) solution improves efficiency. #LeetCode #Day6 #100DaysOfCode #Java #DSA #Arrays #RotateArray #ProblemSolving #CodingJourney
To view or add a comment, sign in
-
-
🚀 Day 531 of #750DaysOfCode 🚀 💻 LeetCode 1415 — The K-th Lexicographical Happy String of Length n Today’s problem was a great practice of Backtracking + Recursion + Lexicographical order generation. We are given a length n and a number k, and we need to find the k-th happy string in sorted order. 🔹 A happy string means: Only contains characters → a, b, c No two adjacent characters are same Example: Valid → "abc", "acb", "bab" Invalid → "aa", "abb", "bcc" The challenge was to generate all valid strings in lexicographical order and return the k-th one. 💡 Approach Used: Backtracking (DFS) Try characters in order → a, b, c Skip same adjacent character Stop early when k-th string found This problem improved my understanding of: ✔ Recursion ✔ Backtracking ✔ String generation ✔ Lexicographical traversal ✔ Early stopping optimization Consistency is the key — one problem every day, no excuses. 🔥 #750DaysOfCode #Day531 #LeetCode #Java #Backtracking #Recursion #DSA #CodingJourney #SoftwareEngineer #PlacementPreparation
To view or add a comment, sign in
-
-
Day 25/30 – LeetCode streak Problem: Complement of Base 10 Integer You need to flip all bits in 'n'’s binary representation, but only up to its most significant '1' (no leading zeros). Core idea * The complement should only flip bits within the bit-length of 'n'. For example, '5' is '101₂'; its complement is '010₂' = '2', not some infinite stream of ones. * Build a mask of the form '111...1' that has the same length as 'n' in binary. Then 'n ^ mask' flips exactly those bits. * Special case: if 'n == 0', binary is "0" and complement is "1", so return '1'. Day 25 takeaway: This is a clean bitmask + XOR pattern: instead of flipping bits one-by-one, construct a full '111...1' mask up to the MSB and XOR once to get the complement. #leetcode #dsa #java #bitmanipulation #consistency
To view or add a comment, sign in
-
-
🚀Day 11 of #Leetcode75 LeetCode #392 – Is Subsequence | Two Pointer Approach Today I solved “Is Subsequence” (Easy) and revised an important concept — the Two Pointer Technique. 🔎 Problem Summary: Given two strings s and t, determine whether s is a subsequence of t. A subsequence maintains the relative order of characters but doesn’t need to be continuous. 💡 Key Insight: Instead of using extra space or complex logic, we can solve this efficiently using two pointers: ✔ One pointer for s ✔ One pointer for t ✔ Move forward and match characters in order ⏱ Time Complexity: O(n) 📦 Space Complexity: O(1) This problem reinforces how powerful simple logic can be when applied correctly. Consistency > Complexity 💪 Excited to keep improving step by step! #LeetCode #DSA #Java #CodingPractice #ProblemSolving #100DaysOfCode #TechJourney
To view or add a comment, sign in
-
-
🚀 Day 37 / 100 | Smallest Pair With Different Frequencies -Intuition: -This problem is based on frequency counting and greedy selection. -So the idea is to count the frequency of each number and then check pairs in increasing order. -Approach: O(n log n) -First, store the frequency of each number using HashMap. -Then, store all distinct numbers in a list. -Sort the list so we can get the smallest values first. -Now, check every pair (x, y) where x < y: If freq(x) != freq(y), return that pair immediately. -Since the list is sorted, the first valid pair will be the answer. -Complexity: Time Complexity: O(n log n) Space Complexity: O(n) #100DaysOfCode #Java #DSA #LeetCode
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
Good explanation. Using bit shifts instead of building strings makes the solution much more efficient. These small bit manipulation tricks make a big difference in performance.