Power of Four Problem Solved on LeetCode with Java

🚀 Day 13 of My DSA Journey Today I solved the “Power of Four” problem on LeetCode using Java. 💡 Problem Statement Given an integer n, determine whether it is a power of four. A number is a power of four if: n=4xn = 4^xn=4xfor some integer x. Example: Input: 16 → Output: true (because 42=164^2 = 1642=16) Input: 5 → Output: false ⚙️ Approach I Used I used a division-based approach. Steps: 1️⃣ First check if n is less than or equal to 0 → return false 2️⃣ While n is divisible by 4, keep dividing it by 4 3️⃣ After the loop, check if the result becomes 1 If it becomes 1, the number is a power of four. 📊 Complexity Analysis Time Complexity: O(log₄ n) because we keep dividing the number by 4. Space Complexity: O(1) ⚡ Result ✅ Accepted ⚡ Runtime: 0 ms (Beats 100%) 📚 Key Learning This problem helped reinforce number patterns and mathematical thinking, which are very useful in algorithmic problem solving. Consistency is slowly building stronger DSA fundamentals. On to Day 14 tomorrow. 💪 #DSA #LeetCode #100DaysOfCode #Java #ProblemSolving #CodingJourney #Algorithms #LearningInPublic #Consistency

  • No alternative text description for this image

To view or add a comment, sign in

Explore content categories