🔥 Day 147/360 – Stop Thinking in Functions… Start Thinking in Bits ⚡ 📌 Topic: Bit Manipulation 🧩 Problem: Power Function (xⁿ) 📝 Problem Statement: Calculate x raised to the power n efficiently 🔍 Example: Input: x = 2, n = 10 Output: 1024 💡 Approach: Binary Exponentiation (Bit-Based Thinking) ✔ Step 1 – Convert power into binary form ✔ Step 2 – Square the base at each step ✔ Step 3 – Multiply only when current bit is 1 ⏱ Complexity: Time → O(log n) Space → O(1) 📚 What I Learned: The biggest optimization comes from changing how you think, not just how you code #Day147 #DSA #Java #Coding #ProblemSolving #InterviewPrep #LeetCode #TechJourney #360DaysOfCode
Efficiently Calculate x Raised to Power n with Binary Exponentiation
More Relevant Posts
-
🔥 Day 148/360 – Learning Something New Today 🚀 This looks tricky at first… but it’s actually very simple 👀 Let’s break it down 👇 📌 Topic: Bit Manipulation 🧩 Problem: Reverse Bits 📝 Problem Statement: Reverse all 32 bits of a given integer 🔍 Example: Input: 43261596 Output: 964176192 💡 Approach: Bit Manipulation (Bitwise Operators) ✔ Step 1 – Extract last bit using (n & 1) ✔ Step 2 – Shift result left and add bit ✔ Step 3 – Shift input right and repeat 32 times ⚡ Key Idea: Build the reversed number bit by bit by shifting and appending the least significant bit ⏱ Complexity: Time → O(1) Space → O(1) #DSA #Java #Coding #ProblemSolving #InterviewPrep #LeetCode #TechJourney #148DaysOfCode
To view or add a comment, sign in
-
-
🚀 Day 10 — Transitioning from Sliding Window to Binary Search Today’s practice was focused on strengthening previously learned sliding window patterns and starting Binary Search fundamentals. Solved/revisited: Longest Repeating Character Replacement Fruit Into Baskets Max Consecutive Ones III Binary Search Search Insert Position 📌 Biggest learning: Sliding Window and Binary Search require completely different ways of thinking. Sliding Window → managing valid ranges dynamically Binary Search → eliminating impossible search space efficiently Also worked on understanding: left/right boundary movement mid calculation insertion position logic Really enjoying the shift from simply implementing patterns to actually understanding why the logic works. #DSA #Java #BinarySearch #SlidingWindow #Consistency #LearningInPublic
To view or add a comment, sign in
-
Day 19 LeetCode #88 — Merge Sorted Array Most people try to merge from the front. That’s where the bug begins. The real trick is thinking backwards. Since nums1 already has empty space at the end, we can start filling from the back using 3 pointers: i → last valid element in nums1 j → last element in nums2 k → last index of final merged array Whichever value is bigger goes at position k. This avoids shifting elements and keeps the solution: ✅ In-place ✅ O(m + n) time ✅ O(1) space A small problem, but a powerful lesson: Sometimes the cleanest solution comes from reversing the direction of thought. #LeetCode #DSA #Java #TwoPointers #CodingInterview #ProblemSolving #SoftwareEngineering #100DaysOfCode
To view or add a comment, sign in
-
-
🚀 Day 88/100 Today’s problem was all about logic simplification 🔥 💡 Problem: Given an array of strings, we can move characters between strings. The goal is to check if we can make all strings equal. 🧠 Key Insight: Instead of thinking about operations, think globally! 👉 Combine all characters 👉 Check if each character can be evenly distributed across all strings ✅ If every character frequency is divisible by number of strings → TRUE ❌ Otherwise → FALSE 📌 What I Learned: - Always try to simplify the problem - Focus on final state, not operations - Frequency counting is 🔑 in many problems 💻 Approach: - Count frequency of all characters - Check divisibility with number of strings ⏱ Time Complexity: O(N * M) 📦 Space Complexity: O(1) Small problem, but powerful thinking shift 💯 #Day88 #CodingJourney #Java #DSA #ProblemSolving #Consistency #100DaysOfCode
To view or add a comment, sign in
-
-
🚀 Day 32 of #LeetCode Challenge Problem: 50. Pow(x, n) Difficulty: Medium 🔍 Approach: Fast Exponentiation (Binary Exponentiation) Instead of multiplying x repeatedly, used an optimized approach: ➡️ If power is even → square the base ➡️ If power is odd → multiply result with base ➡️ Reduce power by half each step Handled negative powers by converting: Also used long to avoid overflow for large values of n 🚀 ⏱️ Time Complexity: O(log n) 📦 Space Complexity: O(1) 💡 Key Learning: Binary Exponentiation drastically improves performance from O(n) → O(log n) 🔥 #Day32 #LeetCode #DSA #Java #BinaryExponentiation #CodingJourney #Consistency
To view or add a comment, sign in
-
-
---------------LeetCode Progress Update---------------- Solved 67 Add Binary *Approach Used two pointers starting from the end of both strings and simulated binary addition step by step. *How it works Traverse from right to left Add corresponding bits along with carry Store result using remainder when divided by 2 Update carry using division by 2 Reverse the final result *Key learning No need for built in conversion functions. Direct simulation is simple and efficient. #LeetCode #DSA #Java #ProblemSolving
To view or add a comment, sign in
-
-
🚀 Day 9 — #100DaysOfLeetCode Today’s problem: Reveal Cards In Increasing Order (LeetCode 950) This problem was a good reminder that simulation problems become much easier when we focus on positions instead of values. 💡 Approach: -> Sorted the deck -> Used a queue to simulate indexes ->Applied the reveal + move-to-bottom process The most interesting part was: queue.offer(queue.poll()) which perfectly simulates moving the top card to the bottom. ⏱️ Complexity: Time: O(n log n) Space: O(n) #LeetCode #100DaysOfLeetCode #DSA #Java #CodingJourney #ProblemSolving
To view or add a comment, sign in
-
-
🚀 Day 17 of #30DaysOfCode Today’s problem: 15. 3Sum 💡 Problem Summary: Find all unique triplets in an array whose sum equals 0. 🧠 Approach: First sort the array Fix one element and use two pointers for the remaining part Move pointers based on the current sum Skip duplicates to avoid repeated triplets ✅ ⚡ Key Learning: Sorting simplifies complex problems Two-pointer technique helps reduce time complexity Handling duplicates carefully is very important 🔥 Progress: Learning to solve medium-level problems step by step 💪 #Day17 #LeetCode #Java #DSA #CodingJourney #30DaysOfCode
To view or add a comment, sign in
-
-
Day 52 – #100DaysOfLeetCode Today I solved LeetCode 796 – Rotate String. Problem Overview: Given two strings s and goal, check if s can become goal after performing any number of rotations. Rotation means shifting the first character to the end. Example: "abcde" → "bcdea" → "cdeab" ... Approach – String Trick (Concatenation): 🔹 Key Insight: If goal is a rotation of s, then it must be a substring of s + s. ✔️ First, check if lengths are equal ✔️ Concatenate s + s ✔️ Check if goal exists inside it This avoids manually rotating strings and keeps the solution efficient. Complexity: Time Complexity: O(n) Space Complexity: O(n) Key Learning: This problem showed how a simple observation can replace brute-force logic. Instead of simulating rotations, we transform the problem using string properties. Reflection: Sometimes the best solutions are not complex they just require a shift in perspective. Problems like this remind me to think smarter, not harder. #Day52 #100DaysOfLeetCode #LeetCode796 #RotateString #Java #ProblemSolving #CodingJourney #Consistency
To view or add a comment, sign in
-
-
🚀 Day 19 of #30DaysOfCode Today’s problem: 20. Valid Parentheses 💡 Problem Summary: Check whether a string containing brackets (), {}, [] is valid or not. A valid string must: close brackets in the correct order match the same bracket type 🧠 Approach: Use a stack data structure Push opening brackets into stack When closing bracket appears, check top element If mismatch occurs → invalid ❌ Stack should be empty at the end ✅ ⚡ Key Learning: Stack works perfectly for balanced bracket problems LIFO concept is very important in DSA Small logic mistakes can break matching order 🔥 Progress: Consistency is turning into confidence 💪 #Day19 #LeetCode #Java #DSA #CodingJourney #30DaysOfCode
To view or add a comment, sign in
-