Day 9/100 – LeetCode Challenge Problem Solved: Pow(x, n) Today I worked on implementing a power function that calculates x raised to the power n. The challenge lies in handling edge cases such as negative exponents and integer overflow scenarios, especially when n equals Integer.MIN_VALUE. The core idea was to carefully manage the exponent: If n is negative, convert the base to its reciprocal. Convert the exponent to a positive value for computation. Handle extreme integer boundaries safely. The straightforward implementation multiplies the base repeatedly, but this approach runs in linear time with respect to n. While it works for smaller inputs, it highlights an important lesson about optimization and algorithmic efficiency. Time Complexity: O(n) Space Complexity: O(1) #100DaysOfLeetCode #Java #ProblemSolving #Algorithms #Consistency #Learning
Implementing Pow(x, n) with Efficient Algorithm
More Relevant Posts
-
Day 22 of Daily DSA 🚀 Solved LeetCode 66: Plus One ✅ Approach: Treat the array as a number and simulate addition from the last digit: • If the last digit is not 9, increment and return • Handle carry by setting 9 → 0 and moving left • If all digits are 9, create a new array with leading 1 This avoids converting the array into an integer and handles large numbers safely. ⏱ Complexity: • Time: O(n) • Space: O(1) (extra array only when overflow happens) 📊 LeetCode Stats: • Runtime: 0 ms (Beats 100%) ⚡ • Memory: 43.58 MB (Beats 38.49%) A simple problem that tests edge-case thinking 💡 #DSA #LeetCode #Java #ProblemSolving #DailyCoding #Consistency
To view or add a comment, sign in
-
-
Day 16/100 – LeetCode Challenge Problem Solved: Binary Tree Level Order Traversal Today’s problem focused on traversing a binary tree level by level. Instead of visiting nodes in depth-first order, the goal is to group nodes according to their depth in the tree. The idea behind the solution is to track the level of each node while traversing the tree. Starting from the root at level 0, each recursive call increases the level for its child nodes. When visiting a node, we check whether a list for that level already exists. If not, we create one; otherwise, we append the node value to the existing level list. By doing this, the traversal naturally organizes nodes into separate lists representing each level of the tree. Time Complexity: O(n) Space Complexity: O(n) Key takeaway: Tree problems often become easier when you track additional context during traversal. In this case, maintaining the current level allows the recursion to build a structured level-by-level result. Day 16 completed. Continuing the consistency streak and strengthening tree traversal concepts. #100DaysOfLeetCode #Java #BinaryTree #TreeTraversal #Algorithms #ProblemSolving
To view or add a comment, sign in
-
-
🌱 Day 11 of my #100DaysOfCode Journey Today I solved LeetCode Problem – Contains Duplicate. The problem asks us to determine if an integer array contains any duplicate values. If any value appears more than once, we return true; otherwise, false. The approach I used today was to sort the array first and then check adjacent elements for equality. This helps detect duplicates efficiently in a single pass. 🔹 What I practiced today: ✅ Array manipulation and sorting ✅ Comparing adjacent elements to find duplicates ✅ Thinking about time vs. space trade-offs 📊 Complexity Analysis: • Time Complexity: O(n log n) — due to sorting • Space Complexity: O(1) — in-place array check A simple yet practical problem that strengthens array handling and logical thinking in algorithm problems. #LeetCode #Java #DSA #100DaysOfCode #CodingJourney #ContainsDuplicate
To view or add a comment, sign in
-
-
🚀 Day 14/100 – #100DaysOfCode Challenge 🔍 Problem Solved: Single Number (LeetCode 136) Today’s problem was about finding the element that appears only once in an array where every other element appears twice. 💡 Key Insight: Used the power of XOR (^) bit manipulation Same numbers cancel out → a ^ a = 0 XOR with 0 gives the number → a ^ 0 = a 👉 By XOR-ing all elements, duplicates vanish, leaving the unique number! ⚡ Approach: Traverse the array once Apply XOR on each element Result = single number ⏱️ Time Complexity: O(n) 📦 Space Complexity: O(1) 🔥 What I Learned: Bit manipulation can simplify problems drastically XOR is super powerful for pairing problems #Day14 #CodingChallenge #Java #DataStructures #Algorithms #BitManipulation #LeetCode #100DaysOfCode #CodingJourney
To view or add a comment, sign in
-
-
Day 82/100 – LeetCode Challenge ✅ Problem: #43 Multiply Strings Difficulty: Medium Language: Java Approach: Manual Multiplication with Result Array Time Complexity: O(n × m) Space Complexity: O(n + m) Key Insight: Multiply digits from right to left (least significant first). Store intermediate results in array where index i + j + 1 holds current digit. Handle carry by adding to previous index. Solution Brief: Edge case: if either number is "0", return "0". Created result array of size n1 + n2 (max possible digits). Nested loops multiply each digit of num1 with each digit of num2. Accumulated results with proper carry handling. Built final string skipping leading zeros. #LeetCode #Day82 #100DaysOfCode #Math #String #Java #Algorithm #CodingChallenge #ProblemSolving #MultiplyStrings #MediumProblem #Multiplication #Array #DSA
To view or add a comment, sign in
-
-
Day 29 of Daily DSA 🚀 Solved LeetCode 287: Find the Duplicate Number ✅ Problem: Given an array containing n + 1 integers where each number is in the range [1, n], find the duplicate number. Approach: Used the index marking technique. Key Idea: Treat the value as an index Convert the value to absolute (Math.abs) Mark the visited index by making the number negative If we encounter an index that is already negative, that value is the duplicate This allows us to detect duplicates efficiently without extra space. ⏱ Complexity: • Time: O(n) • Space: O(1) 📊 LeetCode Stats: • Runtime: 4 ms (Beats 91.67%) ⚡ • Memory: 82.75 MB A clever trick that uses the array itself as a visited map. #DSA #LeetCode #Java #ProblemSolving #Algorithms #CodingJourney #Consistency
To view or add a comment, sign in
-
-
Day 18/100 – LeetCode Challenge Problem Solved: Reverse Linked List Today’s problem focused on reversing a singly linked list. While it looks simple, it’s a fundamental concept that tests pointer manipulation and understanding of linked structures. The idea is to iterate through the list and reverse the direction of each node’s pointer. At every step, we keep track of three things: the current node, the previous node, and the next node. We reverse the link by pointing the current node to the previous one, then move all pointers one step forward. By the end of the traversal, the previous pointer will be pointing to the new head of the reversed list. Time Complexity: O(n) Space Complexity: O(1) Performance: Runtime 0 ms Key takeaway: Linked list problems are all about pointer control. Mastering how pointers move and change direction is essential for solving more complex problems efficiently. Day 18 completed. Staying consistent and reinforcing core data structure fundamentals. #100DaysOfLeetCode #Java #LinkedList #Algorithms #ProblemSolving #Consistency
To view or add a comment, sign in
-
-
Today I solved LeetCode 951 – Flip Equivalent Binary Trees. 🧩 Problem Summary: Given the roots of two binary trees root1 and root2, check whether they are flip equivalent. Two trees are flip equivalent if they are the same after any number of flip operations, where a flip operation swaps the left and right children of a node. 💡 Key Concepts Used: Binary Trees Recursion Depth First Search (DFS) Tree comparison 🧠 Approach: If both nodes are null, return true. If one is null or values differ, return false. Recursively check two possibilities: Without flip → left with left AND right with right With flip → left with right AND right with left If either case is true, the trees are flip equivalent. 📚 What I Learned: How to handle multiple recursive possibilities. Comparing trees with flexible structure (not fixed order). Strengthening understanding of DFS in tree problems. Writing clean recursive conditions for complex checks. Leveling up my tree problem-solving skills Consistency and practice make all the difference #LeetCode #DSA #BinaryTree #FlipEquivalent #DFS #Recursion #Java #CodingJourney #ProblemSolving #100DaysOfCode #TechGrowth
To view or add a comment, sign in
-
-
Day 100/100 – LeetCode Challenge ✅ Problem: #41 First Missing Positive Difficulty: Hard Language: Java Approach: In-Place Index Marking Time Complexity: O(n) Space Complexity: O(1) Key Insight: Place each positive integer in its correct position (index i should contain i+1). Mark presence using negative values without extra space. Solution Brief: First pass: replace numbers outside range [1, n] with n+1 (ignore them) Second pass: treat each number as index, mark that index negative Third pass: first positive index = missing number If all marked, answer is n+1 #LeetCode #Day100 #100DaysOfCode #Array #Java #Algorithm #CodingChallenge #ProblemSolving #FirstMissingPositive #HardProblem #InPlace #Optimization #DSA
To view or add a comment, sign in
-
-
🚀 Day 62 — LeetCode Practice 🚀 ✅ Problem Solved: Sum of Squares of Special Elements 💡 What I learned today: • Understood the concept of 1-based indexing in problems • Learned how to check if an index divides the length of the array • Practiced working with conditions inside loops • Improved clarity on array traversal and mathematical conditions 📊 Approach: • Traverse the array using loop • Convert index to 1-based (i + 1) • Check if n % (i + 1) == 0 • If true → add square of that element to sum ⏱ Time Complexity: O(n) 📦 Space Complexity: O(1) Small logic, but helps build strong fundamentals in arrays and indexing 💪✨ #Day62 #LeetCode #DSA #Java #CodingJourney #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