🔥 DSA Challenge – Day 136/360 🚀 📌 Topic: Bit Manipulation 🧩 Problem: Number of 1 Bits (Hamming Weight) Problem Statement: Given an integer, return the number of set bits (1’s) present in its binary representation. 🔍 Example: Input: 6 Output: 2 Explanation: Binary of 6 → 110 → 2 set bits 💡 Optimized Approach (Brian Kernighan’s Algorithm): Instead of checking each bit one by one, we use a smart trick: n & (n - 1) removes the rightmost set bit in each iteration This reduces the number of operations to the number of set bits only ⚡ Time Complexity: O(k) (k = number of set bits) ⚡ Space Complexity: O(1) 🚀 Key Takeaway: Using bit manipulation can drastically optimize performance compared to brute force approaches. Always look for patterns in binary operations! #DSA #Coding #Java #BitManipulation #136DaysOfCode #LeetCode #ProblemSolving #SoftwareEngineering
Bit Manipulation: Number of 1 Bits (Hamming Weight)
More Relevant Posts
-
🔥 DSA Challenge – Day 135/360 🚀 📌 Topic: Bit Manipulation 🧩 Problem: Check if a Number is Power of 2 Problem Statement: Given an integer n, determine whether it is a power of 2 or not. 🔍 Example: Input: n = 4 Output: true Input: n = 6 Output: false 💡 Approach: Optimized (Bit Manipulation) 1️⃣ Step 1 – If n <= 0, return false (powers of 2 are always positive) 2️⃣ Step 2 – Use bit trick: n & (n - 1) removes the lowest set bit 3️⃣ Step 3 – If result becomes 0, then only one set bit was present ✔ This avoids looping and works in constant time ⏱ Complexity: Time: O(1) Space: O(1) 📚 Key Learning: A number is a power of 2 if it has exactly one set bit in binary representation. #DSA #Java #Coding #InterviewPrep #ProblemSolving #TechJourney #135DaysOfCode #BitManipulation #LeetCode
To view or add a comment, sign in
-
-
🧠 Day 41 / 100 – DSA Practice Solved Count and Say on LeetCode 🔢🗣️✅ 🔹 Problem: Generate the nth term of the count-and-say sequence, where each term is derived by describing the previous term. 🔹 Approach: Used an iterative + string building approach: Start with base case "1" For each iteration, read the previous string Count consecutive characters Append count + character to form next term 🔍 Key Insight: This problem is essentially Run-Length Encoding (RLE) applied repeatedly on strings 🔹 Complexity: ⏱ Time → O(n × m) (m = length of generated string) 📦 Space → O(m) 💯 Result: ✔️ All test cases passed ⚡ Runtime: 8 ms (Beats 62%) Great problem to improve string manipulation & pattern recognition 🚀 #Day41 #100DaysOfCode #LeetCode #Java #DSA #Strings #RLE #Algorithms #CodingJourney
To view or add a comment, sign in
-
-
📘 DSA Journey — Day 35 Today’s focus: Binary Search with index patterns. Problem solved: • Single Element in a Sorted Array (LeetCode 540) Concepts used: • Binary Search • Index parity (even/odd pattern) • Search space reduction Key takeaway: The array is sorted and every element appears twice except one. A key observation: Before the single element, pairs start at even indices After the single element, this pattern breaks. Using binary search: • If mid is even and nums[mid] == nums[mid + 1], the single element lies on the right side • Else, it lies on the left side (including mid) By leveraging this pattern, we can find the answer in O(log n) time and O(1) space. Continuing to strengthen binary search intuition and consistency in problem solving. #DSA #Java #LeetCode #CodingJourney
To view or add a comment, sign in
-
-
📊 DSA Progress Update – Week 5 Over the past few days, I’ve been practicing array problems and improving my approach step by step. What I learned: • Starting with a brute force solution helps in understanding the problem clearly • Optimization becomes easier once the pattern is identified • Key techniques : - Two Pointers - Sliding Window - Prefix Sum - Kadane’s Algorithm Something new: Started learning Binary Search and understanding its basic approach. Plan: Continue practicing and get more comfortable with these concepts. Taking it one step at a time. #DSA #Java #LeetCode #Consistency #CodingJourney
To view or add a comment, sign in
-
#100DaysOfCode – Day 2 Today I worked on a DSA problem based on arrays: Check if an array is sorted and rotated 🔍 Approach: Instead of finding the exact rotation point, I focused on identifying a pattern: In a sorted and rotated array, the order should break at most once. So, I checked how many times an element is greater than the next element while traversing the array in a circular manner. ✔️ If the count of such breaks is 0 or 1 → valid ❌ If it’s more than 1 → not a sorted rotated array 🧠 Key Takeaway: This problem taught me how pattern observation can simplify logic and avoid unnecessary complexity. Sometimes the best solution is not the most obvious one! 📈 Staying consistent and improving step by step 💪 #100DaysOfCode #DSA #DataStructures #Algorithms #Java #CodingJourney #ProblemSolving #LeetCode #Consistency
To view or add a comment, sign in
-
-
🔥 Day 58 of DSA Journey Solved the classic 3Sum (LeetCode 15) problem today. 💡 Key Learnings: Sorting simplifies the problem structure Fix one element and apply the two-pointer approach Handling duplicates correctly is crucial to avoid redundant results ⏱️ Complexity: Time: O(n²) Space: O(1) (excluding output) 📊 Result: Runtime: 33 ms (Beats 73.75%) Strengthened understanding of two-pointer pattern This problem reinforced an important pattern: 👉 Reduce 3Sum → 2Sum using two pointers On to the next challenge 🚀 #DSA #LeetCode #CodingJourney #Java #ProblemSolving #Consistency
To view or add a comment, sign in
-
-
🚀 Day 48 of My DSA Journey Solved: Roman to Integer (LeetCode #13) Today’s problem focused on understanding how Roman numerals translate into integers using mapping and pattern recognition. 🔍 Key Learnings: • Learned how to use a HashMap for value mapping • Understood the concept of subtractive notation (like IV = 4, IX = 9) • Improved my ability to handle string traversal with conditions • Strengthened logic-building for edge cases 💡 Approach: Instead of simply adding values, I checked if the current symbol is smaller than the next one. If yes → subtract logic If no → add normally 📌 This problem helped me realize how small conditions can change the entire logic flow. #Day48 #DSA #LeetCode #Java #CodingJourney #ProblemSolving #100DaysOfCode
To view or add a comment, sign in
-
-
DSA Day 20 – Frequency Counting with Strings Today I solved the “Ransom Note” problem on LeetCode. Problem: Check whether the ransomNote string can be constructed using letters from the magazine string, where each letter can be used only once. Approach Used: -> Counted frequency of characters in ransomNote -> Counted frequency of characters in magazine -> Compared required characters with available characters -> If any required count was greater than available count → false Time Complexity: -> O(n + m) Space Complexity: -> O(1) (Only lowercase English letters / limited character set) Key Learning: -> Frequency counting works not only for arrays, but also for strings -> HashMap helps compare required vs available resources efficiently -> Always check constraints, because they can reduce space complexity What I Realized: Many string problems are actually counting problems in disguise. The better you recognize patterns, the faster you solve problems. Thanks to Pulkit Aggarwal sir for guiding me in DSA and helping me understand problem-solving patterns. Consistency continues, one problem every day. #DSA #LeetCode #Java #ProblemSolving #CodingJourney #100DaysOfCode
To view or add a comment, sign in
-
-
🔥 Day 364 – Daily DSA Challenge! 🔥 Problem: 🎯 Find K Closest Elements Given a sorted array arr, an integer k, and a target x, return the k closest elements to x in ascending order. 💡 Key Insight — Binary Search on Window Instead of checking each element, we binary search the starting index of a window of size k. Search space: Possible windows → [0 ... n - k] 🧠 Decision Logic At index mid, compare: Distance of left boundary → x - arr[mid] Distance of right boundary → arr[mid + k] - x We choose direction based on: ⚡ Algorithm Steps ✅ Initialize: left = 0, right = n - k ✅ Binary search: If left side is farther → shift right Else → move left ✅ Final window starts at left ✅ Collect k elements ⚙️ Complexity ✅ Time Complexity: O(log(n - k) + k) (binary search + result building) ✅ Space Complexity: O(k) 💬 Challenge for you 1️⃣ Why do we search on window positions instead of elements? 2️⃣ Can you solve this using a heap approach? 3️⃣ What if array was unsorted? #DSA #Day364 #LeetCode #BinarySearch #SlidingWindow #Arrays #Java #ProblemSolving #KeepCoding
To view or add a comment, sign in
-
-
🚀 DSA Preparation 💪 Solved a basic yet important Array + Two Pointer problem. Focused on rearranging elements by grouping even and odd numbers efficiently. Great for improving in-place operations and partitioning logic 🔥 🧠 Problem 🔎 Sort Array By Parity Given an integer array nums, move all even integers to the beginning followed by all odd integers. 👉 Return any array that satisfies this condition. Example Input: nums = [3,1,2,4] Output: [2,4,3,1] Input: nums = [0] Output: [0] ⚡ Key Learning 📌 Use two pointers / partition approach 📌 Efficiently rearrange elements in one pass 📌 Time Complexity: O(n) → traverse the array once 📌 Space Complexity: O(1) → in-place rearrangement (no extra space) Improving DSA with strong fundamentals 🚀 #DSA #LeetCode #Arrays #TwoPointers #Java #ProblemSolving #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