🔥 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
Check if a Number is Power of 2 with Bit Manipulation
More Relevant Posts
-
🔥 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
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
-
🚀 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
-
-
🚀 Day 46 of My DSA Journey Today I solved a problem that looked simple but had an interesting twist — constrained swapping in strings. 🔍 Problem: Check if two strings can be made equal using swaps where the index difference is exactly 2. 💡 Key Learning: Instead of actually performing swaps, I understood that: Only specific indices can interact (0↔2 and 1↔3) This divides the string into independent groups We just need to compare characters within these groups ✨ This problem reinforced an important concept: 👉 When operations are restricted, think in terms of groups rather than brute-force simulation. #DSA #ProblemSolving #Java #CodingJourney #Consistency #LeetCode
To view or add a comment, sign in
-
-
DSA Practice Update Today I solved: #141 – Linked List Cycle Learned how to detect a cycle in a linked list using Floyd’s Cycle Detection Algorithm (fast and slow pointers). Understood how moving pointers at different speeds helps identify if a loop exists without using extra space. Key Learnings: • Fast and slow pointer technique • Detecting cycles efficiently in O(n) time • Solving problems with constant space complexity This problem strengthened my understanding of pointer-based approaches, which are commonly used in linked list interview questions. Continuing to stay consistent and improve problem-solving skills step by step. #DSA #LeetCode #CodingJourney #Java #SoftwareDevelopment #PlacementPreparation
To view or add a comment, sign in
-
-
🚀 Day 31/200 – DSA Journey Solved: Contains Duplicate (LeetCode) 🔍 Approach Used: - Sorted the array - Compared adjacent elements - If nums[i] == nums[i-1], duplicate exists ⚡ Complexity: - Time: O(n log n) - Space: O(1) 💡 Key Learning: Brute force approach (O(n²)) is simple but not efficient. Using sorting helps optimize the solution and reduces time complexity. 📈 Progress Insight: Learning to move from brute force to optimized solutions step by step. Consistency > Motivation #DSA #LeetCode #Java #ProblemSolving #CodingJourney #SoftwareEngineering
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 8 of #100DaysOfDSA Today’s problem: Longest Substring Without Repeating Characters 🧠 Problem Statement: Given a string, find the length of the longest substring without repeating characters. 💡 Approach: Sliding Window Technique Used two pointers (left & right) Maintained a HashSet to track characters Expanded window when unique Shrunk window when duplicate found ⚡ Key Learning: Efficient use of the sliding window helps reduce time complexity to O(n), which is crucial for optimizing string-based problems. 💻 Example: Input: "abcabcbb" Output: 3 ("abc") 🔥 This problem strengthened my understanding of: ✔️ Two-pointer technique ✔️ Hashing (Set/Map) ✔️ Optimizing brute force solutions Consistency is the key — small steps every day lead to big results 💪 #DSA #Java #CodingJourney #LeetCode #ProblemSolving #100DaysOfCode 🍁 Saidhanya Sree
To view or add a comment, sign in
-
-
😎 Day 39 of Solving dsa -->longest consecutive sequence. 🚀 Approach First, add all elements of the input array nums into a hash set for efficient O(1) lookups. Initialize a variable longest_streak to 0. Iterate through the array nums again. For each number num: Check if num - 1 exists in the hash set. If it does, num is part of a longer sequence that will be processed later, so we skip it. If num - 1 does not exist in the hash set, num is the start of a consecutive sequence. Start a nested loop from num, incrementing by one, and check if each subsequent number exists in the set. Count the length of this sequence. Update longest_streak with the maximum length found so far. Return longest_streak. 📈 Complexity Time complexity: O(n) — Although there is a nested loop, each element in the hash set is visited at most twice: once during insertion and once during a streak check. Space complexity: O(n) — We store all n elements in the hash set. #programming #solving #java #dsa #100 days challenge #logic #leetcode #striversheeet #tuf #daily challenge
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
-
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